You are on page 1of 8

Perl Quick Reference v1.

0
Basics The $#array variable holds the last index value in an array.
Intel Perl scripts start with the line: #!/usr/intel/bin/perl Using $#array + 1 effectively computes the length of the array
Comments start with a # symbol. since indices are zero based.

Good Programming Practices $length = $#array + 1; # calc length of @array


Use the -w switch on the shebang line. This flag will cause Perl
to prints warnings that help avoid bugs, such as when scalar @array = (1 .. 5); # range operator, set to (1,2,3,4,5)
variables are used before being initialized, file handles you @array = (1.2 .. 3.2); # @array set to (1.2,2.2,3.2)
attempt to write to but are opened in read mode, using an array as @array = (a .. e); # @array set to (a, b, c, d, e)
a scalar variable, etc
@array = (0, @array); # add 0 to the front of @array
#!/usr/intel/bin/perl w @array = (@array, 4, 5); # add 4 & 5 to the end of @array
@array = @array[2..$#array]; # remove first two elements
Require that variables be defined as lexically scoped throughout a @array = @array[0..$#array-2]; # remove last two elements
program by using the use strict; statement.
push(@list,4,5,6); # add 4, 5, & 6 to the end
Standard Output/Input $last = pop(@list); # remove last element & store in $last
print Whats your name?\n # prompt for users name unshift(@list,4,5,6); # add 4, 5, & 6 to the front of the
$name = <STDIN>; # grab name, store in $name $first = shift(@list); # remove first element & store in $first
print # x 10; # print # ten times
@merge = (@array1, @array2); # merge arrays
Scalars @rev = reverse(@array); # reverse an array
Scalars are the basic variable type. They store numbers & strings.
Scalars are preceded by the $ symbol. chomp(@array); # chomp newlines from elements

$var1 = 5; # $var1 is set equal to 5 Use the while, foreach or for control structure to loop over the
$var2 = Hello there!; # $var2 is set to a string elements of an array.
$var3 = `pwd`; # $var3 is set to pwd output
$var4 = $var1; # $var4 is set to value of $var1 while ($x <= $#array) {
print $var2\n; # print contents of $var2 print $array[$x++];
print $var2 x 10; # print contents of $var2 ten times }
$len = length $var2; # obtain length of $var2 value
$con = $var1. $var2; # concatentate variable values together foreach $element (@array) {
chomp($var2); # remove newlines from $var2 print $element;
}
Arrays
for($index = 0; $index <= $#array; $index++) {
An array holds a list of scalar elements that are organized by a
print $array[$index];
numeric index. Arrays are preceded by the @ sign.
}
@array1 = (1, 2, $three); # set to 2 integers & 1 scalar
Use the split function to make an array out of a scalar string.
@array2 = @array1; # copy @array1 to @array2
@array3 = (one, two, three); # set to three strings
@fields = split(/\//, $path); # split $path at forward slashes
@array4 = qw(one two three); # qw construct usage
@chars = split(//, $word); # split each character of $word
print @array1; # print contents of @array1
@words = split; # same as @words = split(/\s+/, $_);
Access single elements of an array by prefacing the name of an
Use the join function to make a scalar string out of an array.
array with $ followed by the index of the particular element in
square brackets, [ ]. Each element of an array has a numeric
$path = join("/", @fields); # join each element with /
index, starting at 0, which is used to access that element. Array
elements are preceded with the $ sign since individual array
elements are scalars. Hashes
A hash is an unordered collection of key-value pairs. Each key
$first = $array[0]; st
# get 1 element of @array name must be unique. Hash variables are preceded by the percent
$second = $array[1]; # get 2nd element of @array symbol, %. Hashes are useful when fast retrieval of values is
$array[0] = 0; # change 1st element value required & are easy to use when handling chucks of key/value
@array[0,1] = @array[1,0]; # swap 1st & 2nd elements pairs (e.g. username => ssn).

Mark Travnikoff -i-


Perl Quick Reference v1.0
The following two hashes are created with fruit, sandwich & ** Evaluated second from right to left.
drink as the keys and apple, burger, & coke as their respective *, /, % Evaluated third from right to left.
values. +, - Evaluated last from right to left.

%hash1 = ( Short Hand Assignment Operators Expanded Code


fruit => apple, += $a += 3; $a = $a + 3;
sandwich => burger, -= $a -= 3; $a = $a - 3;
drink => coke, *= $a *= 3; $a = $a * 3;
); /= $a /= 3; $a = $a / 3;
%= $a %= 3; $a = $a % 3;
$hash2{fruit} = apple; **= $a **= 3; $a = $a ** 3;
$hash2{sandwich} = burger;
$hash2{drink} = coke; Increment/Decrement Operators
++ ++$a Preincrement. Increment $a by 1, then
Assigning a value to a new key in a hash automatically creates a use new value in expression.
new element in that hash. Individual hash elements are accessed ++ $a++ Postincrement. Use $a current value in
in a similar manner to arrays, except that the index is now the key expression, then increment $a by 1.
surrounded by curly braces. -- --$a Predecement. Decrement $a by 1, then
use new value in expression.
print $hash{key}; # print a keys value -- $a-- Postdecrement. Use $a current value
delete($hash{key}); # remove a key-value pair in expression, then decrement $a by 1.
%hash2 = %hash1; # copy %hash1 into %hash2
%merge = (%hash1, %hash2); # merge hashes Arithmetic operators can be applied to individual array or hash
elements just like scalar variables.
Use the foreach control structure to loop over a hash. The keys
function returns a list of all the keys in a hash. The values $array[1]++; # add 1 to 1st array element
function returns a list of all the values in a hash. The each $array[2] += 2; # add 2 to 2nd array element
function returns one key-value pair of a hash as a list in which the $layerNums{ndiff} += 3; # add 3 to ndiff key value
first element is the key and the second element is the value. $layerNums{poly}--; # minus 1 from poly key val

foreach $key (keys %hash) { Round Numbers


print $key -> $hash{$key}\n"; # print keys & values Use the sprintf function to round numbers.
}
$round = sprintf "%.0f", 1243.532; # $round = 1244
foreach $value (values %hash) { $round = sprintf "%.0f", 1243.432; # $round = 1243
print $value\n"; # print the values $round = sprintf "%.2f", 1243.532; # $round = 1243.53
}
Main Escape Characters
while(($key, $value) = each(%hash)) { \n # newline
print "$key = > $value\n"; # print keys & values \t # tab
} \\ # backslash
\" # double quote
The exists function checks if a hash element already exists. \ # single quote
\$ # dollar sign
unless(exists($hash1{fruit})) { \@ # commercial at symbol
$hash1{fruit} = apple; \% # percent sign
}
Control Structures
Arithmetic Control structures test whether something is true or not. Truth is
Operation Expression determined if something is not equal to numeric zero, the null
Addition $x + $y string, or undefined (I.e. 0, 0, , or undef). Note: The string
Subtraction $x - $y 0.0 is evaluated to be true.
Multiplication $x * $y
Division $x / $y Comparison operators numeric string
Modulus $x % $y Equal == eq
Exponentiation $x ** $y Not equal != ne
Less than < lt
Precedence Table Greater than > gt
() Evaluated first innermost nest first. Less than or equal to <= le

Mark Travnikoff - ii -
Perl Quick Reference v1.0
Greater than or equal to >= ge
foreach $element (@array) {
The if control structures execute code if the condition is true. print $element ;
}
if ($variable == 1) {
print The variable is 1.\n; The next statement immediately starts the next iteration of a loop.
}
while ($x < 5) {
if ($variable eq poly) { $x++;
print The variable is equal to \poly\\n; if ($x == 3) { next; }
} print "$x";
}
if ($variable == 1) {
print The variable is 1.\n; The last statement immediately exits from the current loop.
} else {
print The variable is not 1.\n; while ($x < 5) {
} $x++;
if ($x == 3) { last; }
if ($variable == 2) { print "$x";
print The variable is 2.\n; }
} elsif ($variable == 1) {
print The variable is 1.\n; The redo statement immediately restarts the current iteration
} elsif ($variable == 0) { without evaluating the loops condition.
print The variable is 0.\n;
} else { while ($x < 5) {
print The variable is not equal to 2, 1 or 0.\n; $x++;
} print "$x";
if ($x == 5) { redo; }
The unless control structure executes code if the condition is }
false.
A statement may be followed by a single modifier that gives the
unless ($a == 1) { statement a conditional or looping mechanism.
print $a is not equal to one.\n;
} $a = $ans if ($ans < 100);
$b = $ans unless ($ans <= 50);
The while control structure executes code while the condition is $c++ while ($c < 10);
true. print "$y\n" until (++$y == 5);

while ($number < 100) { Use the logical AND operator && to test if multiple conditions
print $number is less than 100.\n; must be true.
$number++;
} if ($var1 >= 10 && $var1 <= 100) {
print Var1 is between 10 and 100.\n;
The until control structure executes code while the condition is }
false.
Use the logical OR operator || to test if one of multiple
until ($index >= 100) { conditions must be true.
print The index ($index) isnt 100 yet.\n;
$index++; if ($var1 < 10 || $var1 > 100) {
} print Var1 is not between 10 and 100.\n;
}
The for structure uses counter controlled repetition & is ideal for
looping through an array of elements. Use the logical NOT operator ! to reverse the truth of a
condition.
for ($index = 0; $index <= 100; $index++) {
print Printing up to 100: $index.\n; if (!($var1 >= 10 && $var1 <= 100)) {
} print Var1 is not between 10 and 100.\n;
}
The foreach structure is used to iterate over a list of values, one
by one.

Mark Travnikoff - iii -


Perl Quick Reference v1.0
-x file is executable

Sorting if(-e $filename && -w $filename) {


Use the sort function along with the <=> and cmp operators to $size = -s $filename;
sort values in an array. print $filename exists and is writable. Size is: $size\n;
} else {
@sorted = sort { $a cmp $b } @array; # ascii sort print $filename does not exist or is not writable.\n;
@sorted = sort { $a <=> $b } @array; # numeric sort }
@sorted = sort { $b cmp $a } @array; # reverse ascii sort
@sorted = sort { $b <=> $a } @array; # reverse numeric sort

File Handles & Test Operators Command Line Arguments


The open function allows a file to be opened for read, write The @ARGV variable stores all command-line arguments. The
and/or append access. The open function requires two arguments. first command-line argument is stored in the $ARGV[0]
The first argument is the filehandle name, which is used to refer variable, the second in $ARGV[1], etc
to the opened file. The second argument is the filename. The
filename should include the path to the file, if it is not in the foreach $arg (@ARGV) {
directory in which the perl program was invoked. print $arg\n; # print command line args
}
open (FH, <file.txt) or die Could not open file: $!;
< # open for read access When no filehandle is used with the diamond operator, <>, Perl
> # create for write access will examine the @ARGV variable for input.
>> # open for append
while (<>) { # obtain one line of input from @ARGV
When done with the filehandle, close it using the close function: print; # print input
}
close(FH) or die Cannot close file: $!;
Special Variables
To read a line from a file, use the specified filehandle surrounded The $_ special variable is the default argument for many Perl
by the diamond operator, <>. functions and is also the default control variable for many Perl
control structures. $_ represents the current thing that Perl is
$line = <FH>; # read one line from FH working with.

while(<FH>) { # grab one line at a time from FH foreach ( Jan , Feb , March ) {
print $_; # print line print $_;
} } => Jan Feb March

If a file is opened in write or append mode, the print function can When you know a function takes arguments, but you dont see
be used to insert data into the file. any, it probably is using the $_ variable.

print FH Hello\n; # print to FH foreach ( Jan, Feb, March ) {


print FH @array; # print contents of @array to FH print; # $_ is the default argument used for print
}
# copy data from one file to another
open(IN,"$a") || die "Could not open file: $!"; The $! variable is a special built-in perl variable that holds the
open(OUT,">$b") || die "Could not open file: $!"; latest system-error message. This variable will basically
while (<IN>) { interpolate into a message that will tell the user what went wrong.
print OUT $_;
} open (FH, <file.txt) or die $!;
close(IN) || die "can't close $a: $!";
close(OUT) || die "can't close $b : $!"; E.g. Error Message: No such file or directory at cat.pl line 3.

Main File Test OperatorsCheck When reading from a file, Perl will read in data until it reaches a
-d file is a directory character that matches the value stored in the input record
-e file exists separator variable, $/. By default, this variable is set equal to
-l file is a symbolic link \n, so Perl will read in one line of data at a time. Setting this
-r file is readable variable to some other value, | for example, will cause Perl to
-s file size read from a file until it sees an occurrence of that value. Each
-w file is writable line is considered to be everything up to and including that

Mark Travnikoff - iv -
Perl Quick Reference v1.0
value. Setting $/ equal to undef will make Perl read the entire Quantifiers Desciption
file as one long string. . match any character except newline
? match zero or one times
$/ = #; # reset input record separator to # * match zero or more times
open(FD, test.dat) or die Couldnt open file: $!; + match one or more times
$line = <FD>; # read up till first # symbol {n} match n times
{n,} match at least n times
The %ENV variable is a special built-in hash that holds the {n,m} match between n & m times (inclusive)
current environmental variables.
Assertions Desciption
print $ENV{HOST}; # prints host name ^ match the beginning of the line
$ match the end of the line (before newline)
# print all environmental variables \b match a word boundary
foreach (keys %ENV) { \B match a non-word boundary
print $_ => $ENV{$_}\n;
} Regular Expression Examples
/\d/ # a single digit
Regular Expressions Pattern Matching /\d+/ # one or more digits
A regular expression (RE) is a pattern of characters used to look /\d*/ # zero or more digits
for the same pattern of characters in another string. Use the m// /\d$/ # a digit at the end of the line
operator for basic pattern matching. Note that the m prefix is /^\d/ # a digit at the beginning of the line
optional. /\w\w\w/ # three word characters in a row
/\S\s\S/ # nonwhite-space, space, nonwhite-space
=~ match operator /.{10,}/ # lines 10 characters or longer
!~ negate match operator /abc*/ # ab, abc, abcc, abccc, abcccc
/b{3}/ # three b's
The =~ operator return a nonzero value if the pattern is found & /\w{5}/ # 5 word characters in a row
0 if not found. The !~ operator is the exact opposite. /de{1,2}f/ # def or deef
/de{3,}/ # d followed by at least 3 es
while(<FH>) { /\w{2,8}/ # 2 to 8 word characters
if (/hello/) {
print Found: hello; Word Boundary Regular Expression Examples
} /fred\b/ # fred, but not frederick
} /\bmo/ # moe and mole, but not Elmo
/\bFred\b/ # Fred but not Frederick or alFred
The g modifier makes Perl perform a global search on the /\Bfred/ # matches abcfred, but not fred
string. By default, Perl will stop looking for a pattern in a string /def\B/ # matches fredghi
once it finds the first matching occurrence. /\Bfred\B/ # cfredd but not fred, fredrick, or afred

$text = Hellllo there.; Parentheses are used for grouping. Use a pipe (vertical bar) to
while ($text =~ m/l/g) { separate two or more alternatives.
print Found another l.\n; # printed out 4 times
} /(abc)*/ # "", abc, abcabc, abcabcabc
/^(x|y)/ # either x or y at beginning of a line
The i modifier makes Perl perform a case insensitive search. /(a|b)(c|d)/ # ac, ad, bc, or bd
/dog(gies|gy)/ # doggies or doggy
$text = xyZ;
if ($text =~ /z/gi) { # search for zs or Zs A pattern-matching character class is represented by a pair of
print Found a z character.; open and close square brackets and a list of characters between
} the brackets. Only one of the characters must be present at the
corresponding part of the string for the pattern to match. Specify
Special Escape Characters a range of characters with the - character.
\d match a digit character (0-9)
\D match a non-digit character $text = Here he is.;
\s match a nonwhite-space character if ($text =~ /[aeiou]/) { # match any single vowel
\S match a white-space character print There are vowels.\n;
\w match a word character (a-z & A-Z) and _ }
\W match a non-word character

Mark Travnikoff -v-


Perl Quick Reference v1.0
There's also a negated character class that matches any character
not in the list. Create a negated character class by placing a caret The translation operator scans a string, character by character,
^ immediately after the left bracket. and replaces all occurrences of the characters found in pattern1
with the corresponding character in pattern2.
$text = Hello;
if ($text =~ /[^aeiou]/i) { # match any non-vowel $text = Hello Tammy.;
print There are consonants; $text =~ tr/a/o/; # translate as into os
} print $text; => Hello Tommy.
$text =~ tr/Tm/Dn/; # translate Ts w/Ds and ms w/ns
You can designate a match for later reference by enclosing its print $text; => Hello Donny.
subpattern in parentheses. You can refer to that match in the same
regular expression by number prefaced with a backslash (i.e. \1, \ $text =~ tr/a-z/A-Z/; # upper case everything
2). You can refer to that match outside the regular expression print $text; => HELLO DONNYY.
by number prefaced with a dollar sign (i.e. $1, $2).
Use the d option to delete found but unreplaced characters.
$text1 = How are you? Good?;
if ($text1 =~ /you(.).Good\1/) { $text2 = zed sandy tabey;
print \$text1 matches.\n; $text2 =~ tr/a-z/A-N/d;
} print $text2; => ED AND ABE

$text = David is 20 years old.; Use the c option to compliment a pattern translation.
if ($text =~ /(\d+)/) {
print He is $1 years old.\n; $text2 = ED AND ABE;
} $text2 =~ tr/A-Za-z/_/c; # replace non-letters w/underscores
print $text2; => ED_AND_ABE
Regular Expressions Pattern Substitution $text2 =~ tr/A-Za-z//cd; # delete non-letters
Use the s/// operator for pattern substitution. All the pattern print $text2; => EDANDABE
matching operators, modifiers, assertions and so on, work with
pattern substitution as well. Use the s option to remove duplicate characters.

$text = Im pretty young.; $text3 = aaabbbcccabcd;


$text =~ s/young/old/; $text3 =~ tr/ab/ab/s;
print $text; => Im pretty old. print $text3; => abcccabcd

$text = Here he is.; External Commands


$text =~ s/\w+/There/; The exec command executes a shell command specified with an
print $text; => There he is. argument, then replaces the current process with the command
that was launched. Therefore, when the specified command
$text = Today is Tuesday.; terminates, the current process (i.e. the perl script) terminates.
$text =~ s/./*/g; Shell wildcard expansion can be used (e.g. ls *.exe).
print $text; => *****************
exec echo Hello!;
$text = How are youuuuuuu?; exec cd ~/scripts;
$text =~ s/u+/u/g;
print $text; => How are you? The system command is similar to exec in that it can be used to
execute shell commands; however, the system command returns
# backreferencing with pattern substitution back to the original process.
$text = snakes eat rabbits;
$text =~ s/^(\w+) *(\w+) *(\w+)/$3 $2 $1/; system(clear); # perform Unixs clear command
print $text; => rabbits eat snakes system(ls > dir.txt); # perform ls & direct output to a file
system(mkdir scripts); # make a scripts directory in cwd
Pattern Translation
The translation operator, tr///, performs straightforward Backticks can also be used to execute an external command.
translations. It does not use regular expressions. It is a bit easier The difference between backticks and the system command is
to use for simple substitutions. The tr/// operator will translate that backticks will return the output of the forked process.
every occurrence of a pattern with another pattern and returns the
number of translations made. @result = `dir altr`; # perform dir & store output
$wordcount = `wc $file`; # perform wc command & store result
Usage: $variable =~ tr/pattern1/pattern2/options @result = qx(dir altr); # qx construct use: same as `dir altr`

Mark Travnikoff - vi -
Perl Quick Reference v1.0
by the subscript of the particular element in square brackets, [].
Another way to invoke an external command is to use a For example, $_[0] is the first argument passed to the
filehandle. A filehandle can be opened as a pipe to an external subroutine, $_[1] is the second, $_[2] is the third,
command, using the | symbol. Writing to the filehandle will
write to the standard input of the command. Reading from the Note when used in a subroutine, the shift function defaults to
filehandle will read the commands output. working on the @_ array.

open(ENV_FH, env |) or die Could not open pipe: $!\n; The ||= operator is used to provide default values for arguments
print $_ while(<ENV_FH>); # print env variables that the user might have omitted.
close(ENV_FH) or die Could not close pipe: $!\n;
When the subroutine completes, data can be returned to the main
open(DATE_FH, date |) or die Could not open pipe: $!\n; program with the return keyword. Subroutines can return scalar
$date = <DATE_FH>; # grab date or list values. Perl will exit from a subroutine immediately after
close(DATE_FH) or die Could not close pipe: $!\n; the return statement exectues. Any code in the subroutine that
appears after the return statement does not get executed.

# e-mail example using sendmail command


$ENV{PATH} = #!/usr/intel/bin/perl
"/usr/bin:/usr/ucb:/usr/sbin:/usr/sbin/sendmail:/usr/intel/bin/";
open(MAIL, | /usr/lib/sendmail -odb david\@intel.com) $sum = add_two(3, 3); # call subroutine
or die Could not open pipe: $!; print sum is: $sum\n;
print MAIL Subject: This is the subject line\n\n;
print MAIL This is the body.\n; sub add_two {
close(MAIL) or die Could not close pipe: $!\n; $value1 = shift; # grab first passed value
$value2 = shift; # grab second passed value
The benefit of pipes over backticks is that you dont have to wait $value2 ||= 1; # default $value2 to 1 if
for an entire child process to complete before analyzing what is # second value not passed
returned. This is a huge benefit when the external process is huge $sum = $value1 + $value2;
or takes a long time. In addition, using a pipe is more efficient return($sum); # return $sum value
than using backticks because all the results from a backtick }
expression are loaded into memory at the same time where as
with a pipe, only a small bit of information is loaded into Variable Scope
memory at a time. A variables scope is the portion of a program in which a variable
can be accessed and manipulated. The reason why scope is so
Subroutines important is because it prevents important variables from being
A subroutine is a block of code separate from your main modified unexpectedly. If you have a variable in the main part of
program, designed to perform a particular task. the program, and accidentally use the same variable in a
subroutine, the program may not run the way you expect. You
The basic subroutine call syntax is: should restrict variables to the smallest possible scope.

subroutine_name(); There are three types of variable scope in Perl: global scope,
lexical scope, and dynamic scope. By default, variables have
The basic syntax of a subroutine is: global scope, which means you can access and manipulate them
anywhere in a program.
sub <subroutine_name> {
. statements . A lexically scoped variable is a variable that can be accessed only
} in the block in which it was created. A lexically scoped variable
can be created by using the my keyword. A programmer can
Arguments can be passed to a subroutine by including them in require that variables be defined as lexically scoped throughout a
the parentheses when you call it. program by using the use strict; statement.

subroutine_name($arg1, $arg2); #!/usr/intel/bin/perl


use strict; # require lexically scoped variables
The arguments passed to the subroutine get stored in the special
array @_, which is only visible inside the called subroutine. my $name = Rocky;
You can pass any number of scalars in as arguments. They will my $years_old = 3;
all end up in @_ in the same order you passed them. As with all my $dog_years = dog_years($years_old);
array variables, an individual element in the @_ array is print My dog $name is $dog_years dog years old.;
accessed by using the name of the array, preceded by $, followed

Mark Travnikoff - vii -


Perl Quick Reference v1.0
sub dog_years {
my $yrs_old = shift;
my $age = 7 * $yrs_old;
return $age;
}

The local keyword defines a dynamically scoped variable. A


dynamically scoped variable exists from the time it is created
until the end of the block in which it was created in. Dynamically
scoped variables are inherited by other functions called within the
block in which they were created in. Dynamically scoped
variables are good for making temporary copies of a global
variable (such as one of Perls special built-in variables). You can
work with that temporary copy until it goes out of scope. The
value of the global variable will then be restored.

#!/usr/intel/bin/perl

{
$file = test;
open (INFILE, $file) or die Couldnt open file: $!;
local $/ = undef; # undefine the input separator
$all = <INFILE>; # read entire file
close (INFILE) or die Couldnt close file: $!;
}
open (ALIAS, .alias) or die Could not open file : $!;
while(<ALIAS>) {
print;
}
close(ALIAS) or die Could not close file: $!;

The local $/ = undef statement assigns a temporary value


(undef) to the $/ variable, and then restores the old value (\n)
when the local variable goes out of scope.

Mark Travnikoff - viii -