Perl

Appeared in:
1987
Influenced by:
Influenced:
Paradigm:
Typing discipline:
File extensions:
.pl .pm
Versions and implementations (Collapse all | Expand all):
Programming language

Perl is a high-level, general-purpose, interpreted, dynamic programming language.

Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language.

Perl borrows features from other programming languages including C, Unix shell, AWK, and sed. The language provides powerful text processing facilities without the arbitrary data length limits of many contemporary Unix tools, facilitating easy manipulation of text files. It is also used for graphics programming, system administration, network programming, applications that require database access and CGI programming on the Web.

Elements of syntax:

Inline comments #
Case-sensitivity Yes
Variable identifier regexp [_a-zA-Z0-9]*
Function identifier regexp [_a-zA-Z][_a-zA-Z0-9]*
Variable assignment =
Variable declaration my $var; or my @arr; or my @hash;
Variable declaration with assignment my $var = value;
Comparison < > <= >= <=> (numbers) lt gt le ge cmp (strings)
Function definition sub f { ... }
Function call f(a, b, ...)
Function call with no parameters f() or f (if pre-declared)
If - then if (condition) { ... } or ... if condition;
If - then - else if (condition) { ... } else { ... }
Loop forever while (1) { ... } or ... while 1;
While condition do while (condition) { ... } or ... while condition;
Do until condition do { ... } until condition;
For each value in a numeric range, 1 increment foreach (1 .. 10) { ... (do something with $_) }

Examples:

Hello, World!:

Example for versions perl 5.8.8, rakudo-2010.08
print "Hello, world!\n";

Factorial:

Example for versions perl 5.8.8

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:

Example for versions perl 5.8.8

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:

Example for versions perl 5.8.8

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:

Example for versions perl 5.8.8

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:

Example for versions perl 5.12.1, perl 5.8.8

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:

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

CamelCase:

Example for versions perl 5.12.1, perl 5.8.8

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";

Fibonacci numbers:

Example for versions Perl 6 (alpha), rakudo-2010.08

Not the shortest possible implementation, but perhaps the easiest to read and understand.

sub fib { 1,1, {$^x + $^y}  ... * }

fib[^16], '...' ==> join(', ') ==> say;

Factorial:

Example for versions rakudo-2010.08

This version uses the reduction metaoperator to multiply every number from 1 to N (the leading [*] means “apply the * operator between each element of the following list”).

Note that the say statement could also be written as:

say "$i! = &fact($i)";

with the function call interpolated directly into the string being printed.

sub fact($n) { [*] 1..$n }

for 0..16 -> $i {
    say "$i! = ", fact($i);
}

Hello, World!:

Example for versions rakudo-2010.08

say command is available only in Perl 6. The semicolon at the end is optional.

say 'Hello, World!';

Quadratic equation:

Example for versions rakudo-2010.08

This example differs from one in Perl 5 in two major ways: the way user input is read and results are printed, and the way all variables have to be predeclared with my keyword before using them. Note that explicit declaration of variable type stays optional.

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

CamelCase:

Example for versions rakudo-2010.08

The first line reads the string to process, the second one — declares the variable which will store the result.

The most interesting things happen in the third line. The regular expression <[a..zA..Z]>+ finds all words in the string, and for each found word the built-in code { $cc ~= $0.capitalize; } is executed. It translates the word to the required case and appends it to the result.

my $A = $*IN.get;
my $cc = "";
$A ~~ s:g /(<[a..zA..Z]>+) { $cc ~= $0.capitalize; } //;
print $cc;