perl 5.8.8

Version of implementation Perl of programming language Perl

Perl 5.8.8 is the eighth maintenance release of Perl 5.8, incorporating various minor bugfixes and optimizations.

Examples:

Hello, World! - Perl (63):

print "Hello, world!\n";

Factorial - Perl (151):

This example uses the reduce function from the List::Util module. Note that we have to put an extra 1 in the beginning, so that it will work when 1 .. $n is empty (i.e. when $n is 0).

use List::Util qw(reduce);
sub fact {
  my $n = shift;
  reduce { $a * $b } 1, 1 .. $n
}

foreach my $i (0..16) {
    print "$i! = ", fact($i), "\n";
}

Factorial - Perl (152):

This example uses recursive factorial definition.

sub fact {
  my $n = shift;
  $n == 0 ? 1 : $n*fact($n-1);
}

foreach my $i (0..16) {
    print "$i! = ", fact($i), "\n";
}

Factorial - Perl (153):

This example uses iterative factorial definition.

sub fact {
  my $n = shift;
  my $result = 1;
  foreach my $i (1 .. $n) {
    $result *= $i;
  }
  $result
}

foreach my $i (0..16) {
    print "$i! = ", fact($i), "\n";
}

Fibonacci numbers - Perl (155):

This example uses recursive definition of Fibonacci numbers.

sub fibonacci {
  my $n = shift;
  $n < 3 ? 1 : fibonacci($n-1) + fibonacci($n-2)
}

foreach (1..16) {
  print fibonacci($_), ", ";
}
print "..."

Quadratic equation - Perl (233):

Note that Perl 6 has no backwards compatibility; this example won’t be valid in Perl 6.

$A = <>;
if ($A == 0) {
    print "Not a quadratic equation.";
}
else {
    $B = <>;
    $C = <>;
    $D = $B * $B - 4 * $A * $C;
    if ($D == 0) {
        print "x = ", -0.5 * $B / $A;
    }
    else {
        if ($D > 0) {
            print "x1 = ", 0.5*(-$B + sqrt($D))/$A, "\nx2 = ", 0.5*(-$B - sqrt($D))/$A
        }
        else {
            print "x1 = (", -0.5*$B/$A, ",", 0.5*sqrt(-$D)/$A, ")\nx2 = (", -0.5*$B/$A, ",", -0.5*sqrt(-$D)/$A, ")\n"
        }
    }
}

CamelCase - Perl (365):

my $text = <STDIN>;
$text = join('', map(ucfirst, split(/[^a-z]+/, lc $text)));
print $text, "\n";

CamelCase - Perl (371):

This is similar to the previous example, except that instead of splitting on non-alphabetical characters, we match on runs of alphabetical characters.

my $text = <STDIN>;
$text = join('', map(ucfirst, lc($text) =~ /[a-z]+/g));
print "$text\n";