Exploring PERL, a programming language

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

Operators

Sending Text to the User's Browser with PERL

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

Relational Operators

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.

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

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

Page Modified: (Hand noted: 2007-08-08 18:38:37Z) (Auto noted: 2007-11-17 06:49:09Z)