"PERL" stands for "Practical Extraction and Report Language"
Alternatively, there is also "Pathologically Eclectic Rubbish Lister"
It was created by Larry Wall and became widely known in the 1990s
It was available both from ucbvax and via Usenet
Perl is released under the Artistic License and under the Gnu GPL.
#!/usr/bin/perl -w # this is a comment use strict; print "Hello World!\n"; exit 0;
Scalars represent a single value:
my $var1 = "some string"; my $var2 = 23;
Scalars are strings, integers, or floating point numbers.
There also "magic" scalars. The most common one is $_, which means the "default" variable, such as when you just do a print with no argument, or are looping over the contents of a list. The "current" item would be referred to by $_.
Both integers and floating point numbers are actually stored as a double precision values — unless you invoke the "use integer" pragma:
#!/usr/bin/perl -w use strict; use integer; my $w = 100; my $x = 3; print "w / x = " . $w/$x . "\n"; [langley@sophie]$ ./prog w / x = 33
12345.6789 123456789e-4 123.456789E2
0 -99 1001
2_333_444_555_666
0xff12 0x991b
01245 07611
0b1010111
Operator | Meaning |
= | Assignment |
+ - * / % | Arithmetic |
& | << >> | Bitwise operators |
> < >= <= | Relationals returning "boolean" values |
&& || ! | Logicals returning "boolean" values |
+= -= *= | Binary assignment |
++ -- | Increment/Decrement |
? : | Ternary |
, | Scalar binary operator that takes on the rhs value |
Also, see man perlop
Operator | Meaning |
** | Exponetiation |
<=> | Numeric comparison |
x | String repetition |
. | String concatenation |
eq ne lt gt ge le | String relations |
cmp | String comparison |
=> | Like the comma operator, but forces the the first left word to be a string |
Again, see man perlop
Strings are a base type in Perl.
Strings can either be quoted to allow interpolation (both metacharacters and variables), or quoted so as not to be. Double quotes allow interpolation, single quotes prevent it.
You can specify special characters in double quoted strings easily:
print "This is an end of line\n"; print "there \t tabs \t embedded \t here\n"; print "embedding double quotes \" are easy \n"; print "that costs \$1000\n"; print "the variable \$variable\n";
"abc " x 4yields
"abc abc abc abc "
Perl will silently convert numbers and strings where appropriate.
"5" x "10" yields "5555555555"
"2" + "2" yields 4
"2 + 2" . 4 yields "2 + 24"
my $x; my ($x,$y); my $x="value"; my ($x,$y) = ("x","y");
You can use the special form ${VARIABLENAME} when you need to interpolate a variable surrounded by non-whitespace:
[langley@sophie]$ perl $a = 12; print "abc${a}abc\n"; abc12abc
However, if you run Perl with the -w option, the interpreter will also alert you that an undef variable is being evaluated.
You can remove a newline from a string with chomp:
$line = <STDIN> chomp($line); chomp($line = <STDIN>);
$ perl chomp($line =); print $line; abcdefghijk abcdefghijk $
The string relational operators are eq, ne, gt, lt, ge, and le.
Examples:
100 lt 2 "x" le "y"
You can use the length function to give the number of characters in a string.
Man of Perl's control structures look for a boolean value. Perl doesn't have an explicit "boolean" type, so instead we use the following "typecasting" rules for scalar values:
Note that both elsif and else are optional, but curly brackets are never optional, even if the block contains only one statement:
if (COND) { } [elsif { }]* [else { }]
if($answer == 12) { print "Right -- one year has twelve months!\n"; }
if($answer == 12) { print "Right -- one year has twelve months!\n"; } else { print "No, one year has twelve months!\n"; }
if($answer < 12) { print "Need more months!\n"; } elsif($answer > 12) { print "Too many months!\n"; } else { print "Right -- one year has twelve months!\n"; }
if-elsif-elsif example:
if($answer eq "struct") { } elsif($answer eq "const") { } elsif($answer ne "virtual") { }
You can test to see if a variable has a defined value with defined():
if(!defined($x)) { print "Use of undefined value is not wise!\n"; }
while(<boolean>) { <statement list> }
As with if-elsif-else, the curly brackets are not optional.
while(<STDIN>) { print; }
You might note that we are using the implicit variable $_ in this fragment.
until(<boolean>) { <statement list> }
The until construction is the opposite of the while construction since it executes the <statement list> until the <boolean> test becomes true.
#!/usr/bin/perl -w use strict; my $line; until (! ($line=<STDIN>) ) { print $line; }
for(<init>; <boolean test>; <increment>) { <statement list> }
for($i = 0; $i < 10; $i++) { print "$i * $i = " . $i*$i . "\n"; }
( <scalar1>, <scalar2> ... )
Examples:
(0,1,5) # a list of three scalars that are numbers ('abc','def') # a list of two scalars that are strings (1,'abc',3) # a list of mixed values ($x,$y) # values can be determined at runtime () # empty list
You can also use the "quoted words" (qw) synax to specify list literals:
('apples', 'oranges', 'bananas') qw/ apples oranges bananas / qw! apples oranges bananas ! qw( apples oranges bananas ) qw< apples oranges bananas >
(0..5) # (0.1 .. 5.1) # same since truncated (not floor()!) (5..0) # evaluates to empty list (1,0..5,'x' x 10) # can use with other values ($m .. $n) # can be evaluated at runtime
my @arr; my @arr = ('a', 'b', 'c');