Exploring PERL, a programming language
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
# = 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. 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. print "Content-type: text/html\n\n";$ 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
octalprint "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 variableprint <<TextBoundaryMarker]; BlockOfTextToBePrinted]; TextBoundaryMarker; "here documents" for printing large block as is (will still need to esc spec. characters):print "<TABLE>\n"; where \n indicates a new line$ScalarVariableName = value; This auto-increments the value by +1
$ScalarVariableName = 'StringWithOutVariables';
$ScalarVariableName = "StringWithOutVariables";
$ScalarVariableName ++;$ScalarVariableName --; This auto-decrements the value by -1$variable3 = $variable1 * $variable2; Arithmetic can be performed on variables$full_name = "First" . " Last"; Appending with . operator Appending with .= operator
$name = " First"; $name .= " Last";chop ($variable); chops of the last character in the variablelength ($variable]; Function which results in length of variablesubstr(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.
@ArrayName = ("element1", "element2",..."elementn"); Function . Remember count from zero!
$array[array elemnent number];$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 elementsplice (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) Function which extracts and returns the RHS element
pop (@array)unshift (@array, NewElementsToAppendOnTheLHSOfArray) Function which extracts and returns the LHS element
shift (@array)
Also reverse (@array), chop (@array), split (/delimiter/, string), join (delimiter, @array)
%array = ('age', '29', 'key2', 'value2',...'keyn', 'valuen'); Function which returns value of the given key. If the key is a variable, do not enclose it with single quotations
$array{'key'}; Function which returns list of keys
keys (%array); Function which returns list of values
values (%array);$array {'NewKey'} = "NewValue"; Adds a new key/value pair to an arraydelete ($array{'key'}); Removes a key/value pair from an array
Not really a data type but points to file to read and/or write to. Usually in CAPS.STDERR Points to standard input, usu. keyboardSTDOUT Points to standard output, use=u. console or browserSTDERR 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
%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
|| (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==, !=, <, >, <=, >= Equality operators for numeric comparisonseq, ne, lt, gt, le, ge Equality operators for string comparisonsA single statement is either executed or not depending on whether a logical expression is true or false.statement if LogicalExpression execute if truestatement unless LogicalExpression execute unless falsestatement while LogicalExpression execute repeatedly while true or until falsestatement until LogicalExpression execute repeatedly until true or while false
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)) This is the usual format.
{
statement1; Like an inverse if
...
statementn;
}
if (comparison)
elseif (comparison)
unless (comparison)else No condition needed This loop iterates through some list and executes statements for each.
foreach $name ($names)
open (FileHandleName, 'filename"); This loop iterates and is used to read each line in a file.
while (<FileHandleName>)
{
print "$_";
}
close (FileHandleName);
for (InitialCondition]; test; incrementation)
{
ActionToPerform
}
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.
$variable =~ s/PatternToFind/PatternToReplaceWith/gi;@array = split (/PatternToSplitOn/, StringToSplit);$string = join ("PatternToJoinOn", ListToJoin);open (FileHandleName, "filename"); Syntax for PERL to open any fileclose (FileHandleName); Syntax for PERL to close any fileopen (FileHandleName, "filename") || &CgiDie("Cannot open [filename]"); Error sub-routine in cgi-lib.plopen (FileHandleName, ">filename"); To write to a file & erase previous. Often used with the print fn To append to a file. Both write & append create the file if it does not already exist.
open (FileHandleName, ">>filename");unlink ("filename"); Deletes a file Renames a file
rename ("OldFilename", "NewFilename"); Changes modification permission to anyone
chmod (0666, "filename");
if (filetest filename && OtherFiletest filename)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.
{
DoSomething
}
open (FileHandleName, "filename") || &CgiDie("Cannot open filename"); 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.
($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat (FileHandleName);
close (FileHandleName);
($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"); Function returns list of the filenames in an open directory
close (DirectoryLocation);
readdir(FileHandleName)@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 | 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 |
2007-08-08 18:38:37Z