Top
Posted: April 19, 2010

Notes on Perl

Regular Expressions

if ($line =~ /http:/) { ..do something.. }
checks $line for the substring “http:”, and returns true if it exists
/[^:]+:/ makes a match to any string that has a ‘:’ and does not have a ‘:’ preceding it
/\d{5,7}/ makes a match if the string is 5 to 7 digits
\b matches a word boundary
/i in match means case insensitive
next LINE if $line =~ /^#/ skips line if the line starts with the comment sign

The most basic string comparison is
$string =~ m/sought_text/;
The above returns true if string $string contains substring “sought_text”, false otherwise. If you want only those strings where the sought text appears at the very beginning, you could write the following:
$string =~ m/^sought_text/;

Similarly, the $ operator indicates “end of string”. If you wanted to find out if the sought text was the very last text in the string, you could write this:
$string =~ m/sought_text$/;

Now, if you want the comparison to be true only if $string contains the sought text and nothing but the sought text, simply do this:
$string =~ m/^sought_text$/;

Now what if you want the comparison to be case insensitive? All you do is add the letter i after the ending delimiter:
$string =~ m/^sought_text$/i;

if ($string =~ m/^\s{2}rem/i) { print “yes\n”; } # print if $string start with exactly two space characters then rem in either upper- or lowercases (i)
if($string =~ m/(A|E|I|O|U|Y|a|e|i|o|u|y)/) { print “String contains a vowel!\n”; }

#the ‘or’ function used in a group

if($string =~ /[^AEIOUYaeiouy]/){print “This string contains a non-vowel”} #here ^ means ‘not’
if($string =~ /^[AEIOUYaeiouy]/){print “This string begins with a vowel”} #here ^ means ‘at the beginning of the string’

Running Programs

Show perl version in Terminal: perl -V
./program.pl to run program
#!/usr/bin/perl -w
-w‘ adds additional debugging support

Operators

$a = 123
$b = 3
$a + $b = 126
$a . $b = 1233
$a * $b = 369
$a ** $b = 123^3
$a x $b = 123123123

Comparisons

numbers strings
== q
!= ne
< lt
> gt
<= le
>= ge
<=> cmp # returns 0 if equal, 1 if gt, and -1 if lt

$a or $b # like ||
$a and $b # like &&
($x, $y) = ($y, $x); this is possible!
my ($a, $b) = (7, 12);

Array Manipulation

pop, push, shift, unshift, map, splice, join, sort, reverse

Hash functions

delete, undef, keys, values, sort, exists, defined
@key = sort { $a <=> $b } keys %array; # (’3′, ’5′, ’11′)
@value = sort { $a cmp $b } values %array; # (‘apple’, ‘banana’, ‘orange’)

Sort an Array

sort { $a <=> $b } @list; # ascending numerical order
sort { $b <=> $a } @list; # descending numerical order
sort { $a cmp $b } @list; # ascending lexicographical order (default)
sort { $b cmp $a } @list; # descending lexicographical order

case-insensitive ascending lexicographical order
sort { lc($a) cmp lc($b) } @list; # or use uc()

Number of elements in an Array

scalar(@Array) or
$size = @Array
while ($num < @Array) { … }
$#Array the last subscript item in @Array
$#Array + 1 thus gives (after a loop) the # of elements in @Array
Array[-1] to get last element, as does Array[$#Array]

Print Array

$, = ” “; output field separator (use in the line before print)
@arr = (’1′, ’2′, ’3′, ’4′);
print @arr;
for (@arr) { print “$_\n”; }
foreach $v (@arr) { print “$v\n”; }

Comments

# or =pod … =cut
open FILE, “file.txt” or die “Can’t open file.txt – $!\n”;
open (FILE, “file.txt”) || die “Can’t open file.txt – $!\n”;
$! is the error message
-e “/usr/bin/perl” or warn “Perl is improperly installed”;
-e checks if file exists
see also -r -w -d -f -T
use elsif instead of elseif!
unless executes only if not true

Loops

while, until, for, foreach
foreach $user (@users) { ..do something.. }
foreach $key (sort keys %hash) { ..do something.. }
next like continue in C++
last like break in C++
chomp($num = <STDIN>);
assigns input to $num, then chops off the \n if there is one

Show Typeglob Symbol Table

for (keys %main::) {
print $_, ” => “, $main::{$_}, “\n”;
}