Intro

PERL ("Practical Extraction and Report Language") was created by Larry Wall in 1986. PERL is like a shell script which is a sequence of shell commands stuffed into a text file and then made executable. PERL is an interpreted language that excels in string manipulation and I/O. It is particularly good for system task since it has built in features for the "section 2 of Unix manuals" and incorporates syntax elements from the Bourne shell, csh, awk, sed, grep, and C. PERL is very popular for Web development, mail filtering, and graphical programming. PERL is good for portable RAD solutions

Characters

  • Whitespace is optional.
  • # = a comment.
  • ; = the end of a simple statement.
  • $name = a scalar variable.
    • ${name)asaurus = delimits scalar when right next to string.
  • () = the conditions of logical construct, eg if ($jasper eq $guess) .... .
  • {} = the actions to be followed depending on the logical construct, eg if (....) {eat me}.
  • @name = an array or list where [] = a subscript of a list, eg $words[0] or $words[$i].
  • %name = an associative array where {} = a subscript of an associated array, eg $words{"betty"} or $words{$person}.
  • & = a subroutine, eg &good_word, which is elsewhere defined as sub good_word { ...}.
  • @_ = a special default temporary local array.

Operators

  • chop (...) = takes a scalar variable and remove the last character, eg chop ($name).
  • =~ = match operator, eg $name =~ /^randal\b/i (Where \b = boundary of regular expression and i = ignore case.).
  • s = substitute operator, eg $name =~ s/\W.*// (Where \W = nonword character and .* = any characters from there to the end of the line.).
  • tr (...) = translate operator, eg $name =~ tr/A-Z/a-z/.
  • local (...) = defines variables as applying only for that subroutine.
  • || = logical or, eg ($words{$somename} || "groucho").
  • ! = logical not, i.e. negates value following it, eg While (! &goodword($guess)).
  • open (FILEHANDLEX, "filex") = creates a filehandle for file x.
  • close () = discards a filehandle.
  • -M = modified, i.e. returns the age in days since modified.
  • die "..." = prints the string following to STDERR.
  • print MAIL "string" = sends e-mail.
  • getpwuid($<) = takes a UID number ($<)and returns list info from the password file.
    • format STDOUT = = 1st line in format subroutine.
    • @<<<<<< @<<<<<<<<< @<<<< = 2nd line, field definitions, i.e. #, length, & type.
    • $expression2 $exprs2 $exprs3 = 3rd line, field values, i.e. expressions evaluated above.
    • +++++++++++ = 4th line, contains no expressions so is copied as is.
    • . = last line.
  • write = invokes format subroutine.
    • dbmopen($last_good,"lasdb",0666) = 1st line in mapping an associative array into a pair of disk files, eg lastdb.dir and lastdb.pag, aka DBM. The 0666 set the permissions.
    • $last_good{$name} = time = 2nd line, uses the map just like a regular array.
    • dbmclose(%las_good) = last line, closes the DBM.
  • keys($associativearray) = returns a list of all the keys of the array.
  • sort @list = sorts list alphabetically.
  • foreach $scalar (@list) {...} = takes a list & assigns each to a scalar & executes the loop's block.
  • $a += 5 is the same as $a = $a + 5. This is an eg of a binary assignment operator.

Sending Text to the User's Browser with PERL

  • To comply with HTTP protocol, send HTTP header:
    print "Content-type: text/html\n\n";
  • Watch out for special PERL characters such as:
    • $ which names scalar variable
    • @ which names a list array
    • % which names associative arrays
    • \ used to escape, which tells PERL to accept the immediately following character as part of the string
    • % in strings followed by a formatting argument. eg %.2f formats to 2 decimal places (floating point), c formats character, s string, d decimal, x hexadecimal, o octal
  • Print command
    • print "StringToPrint";
    • print qq!StringToPrint!; Has the advantage of avoiding problems with quotations.
    • printf ("SomeText %FormattingArgument MoreText", ArgumentsToBeFormatted);
    • sprintf ("SomeText %FormattingArgument MoreText", ArgumentsToBeFormatted); This can be stored in a variable
    • print <<TextBoundaryMarker]; BlockOfTextToBePrinted]; TextBoundaryMarker; "here documents" for printing large block as is (will still need to esc spec. characters):
  • Use PERL formatting constructs or "here documents" to format output so it's easier to read when viewing the source. eg: print "<TABLE>\n"; where \n indicates a new line
  • Also \r return, \t tab, \b backspace, \v vertical tab, \e escape, \\backlash, \" double quote, \l lowercase the next character, \L lowercase every character until \E, \u uppercase the next character, \Uupper case every character until \E, \E terminate \L or \U

PERL Data Types

Scalar Variables

$ScalarVariableName = value;
$ScalarVariableName = 'StringWithOutVariables';
$ScalarVariableName = "StringWithOutVariables";
$ScalarVariableName ++;
This auto-increments the value by +1
$ScalarVariableName --; This auto-decrements the value by -1
$variable3 = $variable1 * $variable2; Arithmetic can be performed on variables
$full_name = "First" . " Last"; Appending with . operator
$name = " First"; $name .= " Last";
Appending with .= operator
chop ($variable); chops of the last character in the variable
length ($variable]; Function which results in length of variable
substr(MainString, BeginningPointOfExtraction, LengthOfExtraction); Function which extracts the selected text. Remember count from zero! If the extraction is all the way to the end, then do not include the 3rd operand.

$_ is the default PERL variable.

List Arrays

@ArrayName = ("element1", "element2",..."elementn");
$array[array elemnent number];
Function . Remember count from zero!
$NumberOfElementsInArray = @ArrayName Remember count from zero!
$array[array elemnent number] = "value" Can be used to append value to array or assign a value to an element
splice (ArrayToModify, BeginningPointOfModification, LengthOf, ListOfNewElements); Function which results in modified array. If no new elements are listed, then the elements specified will be eliminated.
push (@array, new element(s) to append on the RHS of array)
pop (@array)
Function which extracts and returns the RHS element
unshift (@array, NewElementsToAppendOnTheLHSOfArray)
shift (@array)
Function which extracts and returns the LHS element
Also reverse (@array), chop (@array), split (/delimiter/, string), join (delimiter, @array)

Associative Arrays

%array = ('age', '29', 'key2', 'value2',...'keyn', 'valuen');
$array{'key'};
Function which returns value of the given key. If the key is a variable, do not enclose it with single quotations
keys (%array);
Function which returns list of keys
values (%array);
Function which returns list of values
$array {'NewKey'} = "NewValue"; Adds a new key/value pair to an array
delete ($array{'key'}); Removes a key/value pair from an array

File Handles

Not really a data type but points to file to read and/or write to. Usually in CAPS.
STDERR Points to standard input, usu. keyboard
STDOUT Points to standard output, use=u. console or browser
STDERR Points to standard error recording device, usu. console or server-error log file
<FileHandleName> Putting angle brackets around a file handle returns the next line or lines of input from the file or device

Special Variables

%ENV A special PERL associative array which stores all environment variables
$_ A special variable used to store the current line of input
$! A special variable used to store the current system error number

Flow Control

Logical Operators

  • || (or) When the LHS of an expression is true, then the RHS is not evaluated
  • && (and) When the LHS of an expression is false, then the RHS is not evaluated

Relational Operators

  • ==, !=, <, >, <=, >= Equality operators for numeric comparisons
  • eq, ne, lt, gt, le, ge Equality operators for string comparisons

Conditional Expressions Modifiers

A single statement is either executed or not depending on whether a logical expression is true or false.
statement if LogicalExpression execute if true
statement unless LogicalExpression execute unless false
statement while LogicalExpression execute repeatedly while true or until false
statement until LogicalExpression execute repeatedly until true or while false

Compound Statements

For the execution of multiple statements, the modifiers or control statements come first, followed by the logical expression in parentheses, followed by the conditional statements in braces.
control statement ((condition) LogicalOperator (condition))
{
statement1;
This is the usual format.
...
statementn;
}

if (comparison)
elseif (comparison)
unless (comparison)
Like an inverse if
else No condition needed

foreach $name ($names)
This loop iterates through some list and executes statements for each.

open (FileHandleName, 'filename");
while (<FileHandleName>)
{
print "$_";
This loop iterates and is used to read each line in a file.
}
close (FileHandleName);

for (InitialCondition]; test; incrementation)
{
ActionToPerform
}

Manipulating Strings with Modifiers

See also Regular Expressions.

A match regular expression has this syntax. @ is a delimiter symbol placed on both sides of the pattern. If / is used for @, then the m is preceding the first delimiter is optional.

m@pattern@[modifier]

A substitution regular expression has this syntax.

s@PatternToFind@PatternToReplaceWith@[modifier]

The modifier can be any combination of the following.

  • g. The matching should occur globally, i.e. for the whole string instead of just the 1st occurrence.
  • i. The matching should ignore case.
  • m. The matching should search multiple lines.
  • s. The matching should search a single line.
  • x. The matching should exclude whitespace, i.e. ignore it.

PERL has special characters for patterns that are not part used by JavaScript or VBScript. EG: \Q makes the metacharacters (\ | () [] {} ^ $ * + ? .) assume literal meanings until \E or the end of the pattern.

EGs

  • $variable =~ s/PatternToFind/PatternToReplaceWith/gi;
  • @array = split (/PatternToSplitOn/, StringToSplit);
  • $string = join ("PatternToJoinOn", ListToJoin);

File Management

open (FileHandleName, "filename"); Syntax for PERL to open any file
close (FileHandleName); Syntax for PERL to close any file
open (FileHandleName, "filename") || &CgiDie("Cannot open [filename]"); Error sub-routine in cgi-lib.pl
open (FileHandleName, ">filename"); To write to a file & erase previous. Often used with the print fn
open (FileHandleName, ">>filename");
To append to a file. Both write & append create the file if it does not already exist.
unlink ("filename"); Deletes a file
rename ("OldFilename", "NewFilename");
Renames a file
chmod (0666, "filename");
Changes modification permission to anyone

if (filetest filename && OtherFiletest filename)
{
DoSomething
}
File test determine info about files on the file system. Here are common ones: -r is readable, -w is writable, -x is executable, -o is owned by user, -e exists, -z exists and has zero size, -s exist and has non-zero size, -f is a plain file, -d is a directory, -T is text, -B is binary, - M modification age in days, -A access age in days.

open (FileHandleName, "filename") || &CgiDie("Cannot open filename");
($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat (FileHandleName);
close (FileHandleName);
The stat fn returns a 13 element array of file info. $dev device that the file resides on, $ino inode for this file, $mode permissions for this file, $nlink # of hard links to this file, $uid numerical user id for the file owner, $gid numerical group id for the file owner, $rdev device type if the file is a device, $size size of the file in bytes, $atime when the file was last accessed, $mtime when the file was last modified, $ctime when the file status was last changed, $blksize the optimal block size for i/o operations on the file system containing the file, $blocks # of blocks allocated to this file. Bold indicates those used in CGI. Time values returned in seconds since 1970/01/01.

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime (time); Converts the seconds to regular format.
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime ($mtime);

opendir (FileHandleName, "DirectoryLocation") || &CgiDie("Cannot open DirectoryLocation");
close (DirectoryLocation);
readdir(FileHandleName)
Function returns list of the filenames in an open directory
@filenames = grep(!/^\.\.?$/, readdir (FileHandleName); To get a list of all the filenames in an open dir w/o including the "." (current directory) and ".." (root directory) files.

PERL 4 v PERL 5

PERL 4  PERL 5 
CGI-LIB.PL Library

Not OOP

3D Arrays

Regular Expressions

Associative Arrays via ,

Modules Loaded at start up

CGI.pm Library

OOP

2D Arrays

Regular Expressions Enhanced

Associative Arrays via =>

Modules loaded as needed



GeorgeHernandez.comSome rights reserved