lesson 2 ======== push ---- A function that "pushes", i.e. adds objects to an array. For example, say array @INC has the following elements $INC[0] = "~/directory0" $INC[1] = "~/directory1" $INC[2] = "~/directory2" The elements of @INC are simply strings that are directory pathnames. The result of push(@INC,"~/bin"); is $INC[0] = "~/directory0" $INC[1] = "~/directory1" $INC[2] = "~/directory2" $INC[3] = "~/bin" @INC ---- A special array used by perl containing directory pathnames used to search for subroutines used in the perl program. subroutines ----------- Perl programs with code that can be "called", i.e. used in another program. This makes programs smaller since the instructions in the subroutine do not have to be coded for each use. For example: ... foreach (@hex_array) { $bin_number0 = $bin_number0 . $hex_val{$_}; } $result0 = substr($bin_number0,-$num_bits); foreach (@hex_array) { $bin_number1 = $bin_number1 . $hex_val{$_}; } $result1 = substr($bin_number1,-$num_bits); foreach (@hex_array) { $bin_number2 = $bin_number2 . $hex_val{$_}; } $result2 = substr($bin_number2,-$num_bits); ... A subroutine, say "bin_numb", can be defined that performs the same set of instructions with different values, so each of the repeated "foreach..." instructions above can be replaced with subroutine calls ... bin_numb($bin_number0,$num_bits); bin_numb($bin_number1,$num_bits); bin_numb($bin_number2,$num_bits); .. require ------- Loads a subroutine into the perl program. The directories in the @INC array are searched for the specified file containing the subroutine, e.g. require 'bin2dec.pl'; regular expressions (regexp) ---------------------------- Regular expressions are one of the most powerful features of perl, a way of performing complex text searches in a flexible, easy manner. Regular expressions consist of a series of terms which represent the string pattern in an object. Some of the terms are "^" - the beginning of the string "." - any character "a" - the character "a" "b" - the character "b" ... etc. "$" - the end of the string "\n" - carriage control "\t" - tab "\w" - any alpha-numeric character or "_" {n} - indicates the preceding term repeated n times "\" - used to escape characters that are interpreted by perl syntax, e.g. "#" or ";" "\\" - the character "\" Examples pattern (/regexp/) command -------------------------- Take the form /regexp/ where "regexp" is a regular expression. if (/^\#/){ next; } else { print; } while loops =========== while (condition){ ... # commands } Executes the commands between the curly brackets while the condition is true. For example while () { ... # commands } Executes the commands between the curly brackets until the last line of the object pointed to by the file handle MEM_PRG is read. returns the next line of the object. After the last line is read, returns a "false" condition. next ---- Skips to the next iteration of the loop, e.g. while () { if (/^\#/){ next; } else { print; } } skips to the next iteration if the /^\#/ command returns a "true" value, otherwise, prints the current line read with . $_ --- An internal perl variable. "$_" is assigned the results of many perl expressions and can be used in other expressions. Many perl operations can be performed on "$_" without explicitely including "$_" in an expression. For example, in the code while () { if (/^\#/){ next; } else { print; } } reads the next line from the file pointed to by the MEM_PRG file handle into "$_" or sets "$_" to false after the last line is read. What is really happening is $_ = The "/^\#/" function is performed on "$_" and returns a true value if the string pattern in "$_" is matched. What is really happening is $_ =~ /^\#/ where "=~" performs the pattern match on "S_"; print; really means print "$_";