########################################################### # Documentation perldoc man perl ########################################################### # Variables $var -- Scaler variable. $_ -- Special scratch variable. @arr -- Variable array. @ARGV -- Command line argument array @_ -- Subroutine argument array. @arr = ($var1, ...) -- Set variable array. @arr = qw (a, ...) -- Set array with string values "a", etc. $arr[n] -- N^th (starting from 0) var in an array. @arr[n1,n2,n3,...] -- Array slice. Negative n counts backward from the end. $var = @arr; -- $var set to size of @arr. $#arr -- Index value of the last element of @arr ($a, $b) = ($c, $d) -- $a = $c and $b = $d. ($a, @arr) = @arr2 -- $a = @arr2[0] and @arr = @arr2[2,3,4...] $x = index($str, $sub); -- Index of $sub in $str. $x = -1 if not found. chomp ($str) -- Get rid of an ending newline character chomp; -- Works on $_ $str1 = chop ($str2); -- returned with last character gone. set to character chopped. $x = substr ($str, n) -- Substring beginning at position n. $x = substr ($str, n, l) -- Substring of length l beginning at position n. push(@arr, $val); -- Push $val on @arr stack end. $val = pop(@arr); -- Pop end value of @arr. unshift(@arr, $val); -- Push $val on @arr stack beginning. $val = shift(@arr); -- Pop beginning value of @arr. @b = reverse(@a); -- @b = reverse of @a. @b = sort(@a); -- @b = ASCII string sort of @a. $a = 'abc' . 'def' -- Concat strings. 'str' -- Single quoted string. "str" -- Double quoted string is variable interpolated (single pass only). %hash -- Hash table %hash = ($key1, $val1, ...); -- Set hash table: $key1 => $val1, etc. %hash = qw (a b ...); -- Set hash table: key: "a" => value: "b". $hash{$key} -- Value associated with $key in hash table. @k = keys %hash; -- Get array of hash keys. @v = values %hash; -- Get array of hash values. if (exists $hash{"abc"}) -- Check existance of an entry with key "abc". delete $hash{"abc"}; -- Remove an entry with key "abc". %inv = reverse %hash; -- Create a hash with key/values switched. while (($k, $v) = each %hash) -- Loop over all hash key/value pairs. foreach $k (sort keys %hash) { $v = $hash{$k}; -- Loop over all hash key/value pairs in order. ... } $ref = \@array; -- The slash (\) in front gives a reference to @array. @{$ref}[3] -- #3 element in array pointed to by $ref. $ref->[3] -- Same as above with different syntax. $ref = [1, "foo"]; -- $ref now holds a reference to an array. $arr[3] = [@ref]; -- $arr[3] holds reference to a copy @ref. $arr[3][1]; -- Gives "foo". $ref = {Jan => 4}; -- $ref now holds a refenence to a hash. "[]" -- Array constructor. "{}" -- Hash constructor. $array[7][12] # array of arrays $array[7]{string} # array of hashes $hash{string}[7] # hash of arrays $hash{string}{'another string'} # hash of hashes ########################################################### # Regular expressions / / -- Regular expression delimitors. Match to $_. $str =~ /abc/ -- Match "abc" to $str. $` -- Holds part before the match. $& -- Holds the match. $' -- Holds part after the match. ' m@ @ -- Using "@" as the regular expression delimitors. / /i -- Match ignoring case. /$str/ -- Match to $str. Pattern matching characters in $str are active. /\Q$str\E/ -- Match to $str. Pattern matching characters in $str are not significant. /./ -- Match to any single character (except newline \n). /[abc]/ -- Match to a single character: a, b, or c. /[0-9]/ -- Match to a single character: 0 through 9. /[\-]/ -- Match to "-". /[^abc]/ -- Match to a single character: Not a, b or c. /song|bird/ -- Match to "song" or "bird". /a*/ -- Match to zero or more "a". /a+/ -- Match to 1 or more "a". /a?/ -- Match to 0 or 1 "a". /a{5,10}/ -- Match to 5 through 10 "a". /a{5,}/ -- Match to 5 or more "a". /a{5}/ -- Match to 5 "a". /.{5}/ -- Match to any 5 characters. Equivalent to /...../ /a*?/ -- Force "*" to be lazy, not greedy. ( ) -- Remember pattern, store in $n starting from $1. (?: ) -- Do not remember pattern. \n -- Internal: N^th remembered pattern using ( ). $n -- External: N^th remembered pattern using ( ). /a(.)b\1/ -- Match to say "azbz" but not to "azby". ($a, $b) = /( ) ( ) / -- Match RHS to $_ and set ($a, $b) = ($1, $2) \d --> [0-9] -- A digit character. \w --> [a-zA-Z0-9_] -- A word character. \s --> [ \r\t\n\f] -- A space character. \D --> [^0-9] -- A non-digit character. \W --> [^a-zA-Z0-9_] -- A non-word character. \S --> [^ \r\t\n\f] -- A non-space character. \b -- Ancher to a word boundary defined by \w. \B -- Ancher to a non-word boundary. ^ -- Ancher to the beginning of a string (if it makes sense). $ -- Ancher to the end of a string (if it makes sense). /\b\+\b/ -- Matches "x+y" but not "++" or " + ". Precedence: Name Representation ----------------- --------------------------- Parentheses ( ) (?: ) Qualifiers ? + * {m,n} ?? +? *? {m,n}? Sequence and anchoring abc ^ $ \A \Z (?= ) (?! ) Alternation | $str =~ s/a/b/; -- Substitute first "b" for "a" in $str. s/a/b/; -- Substitute first "b" for "a" in $_. s/a/b/g; -- Substitute all "b" for "a" in $_. s/a/b/i; -- Substitute first "b" for "a" in $_ ignoring case. s/a/b/gi; -- Substitute all "b" for "a" in $_ ignoring case. s/(\w+)/<$1>/g; -- Put <> around all words in $_. tr/ab/ba/; -- Transpose: "a" -> "b" and "b" -> "a" in $_. tr/a-z/x/; -- All lowercase letters -> "x" in $_. tr/a-z/x/d; -- "a" -> "x", all other letters get deleted in $_. tr/a-z//; -- $_ is unchanged. $ct = tr/a-z//; -- $_ is unchanged. $ct = number of characters matched. tr/a-z/_/c; -- Everything but a-z -> "_" $c = tr/a-z//c; -- $_ is unchanged. $c = number of non a-z chars. $_ = "ddgh"; tr/gh/dd/s; -- Squeeze: $_ -> "ddd" ("gh" -> "dd"-> "d"). @arr = split(/:/, $str); -- Split line using ":" as a delim, put in @arr. @arr = split(/:+/, $str); -- Same as above except @arr does not contain any empty strings in case $str contains "::". @arr = split(/:/); -- Same as above using $_ instead of $str $str = join($glue, @arr); -- Join array with $glue. \U -- Uppercase everything. \u -- Uppercase first letter. \L -- Lowercase everything. \l -- Lowercase first letter. /(abc)/\U$1/gi; -- Convert all instances of "abc" to upper case. ########################################################### # Files -- File handle for the Standard Input. <> -- File handle for the Command line @ARGV files. open (FH, "file"); -- Opens file for reading with file handle . open (FH, ">file"); -- Opens file for writing with file handle . open (FH, ">>file"); -- Opens file for appending with file handle . open (FH, "file") || die ("str"); -- Open or die. open (FH, "file") || warn ("str"); -- Open or warn. close (FH); -- Closes file with file handle $var = -- Read line from STDIN. @arr = -- Read all lines from STDIN. print ($str); -- Print to . print (FH, $str); -- Print to . printf "Hello %s", $you; -- Print with format. %6d -- 6 decimal digits. %10s -- right justified 10 character string. %-5s -- left justified 5 character string. %g -- Mixed deciaml or floating %12.3f -- right justified floating while () { } -- While loop reading $_ = . my @list = glob "*.f90"; -- Make a list of all *.f90 files. ########################################################### # Control Structures if () { } elsif () { } else { } if ($str =~ /rex/) -- Regular exp test. True if "rex" is in $str. if (/rex/) -- Test against $_ if (exists $hash{"abc"}) -- See if hash key "abc" exists in hash. if ($hash{"abc"}) -- See if value for hash key "abc" is true. while ($count < 10) { } -- While loop. label: while () { } -- While loop with a label. while (<>) { } -- Read from @ARGV files until end of files. while (($key, $val) = each %hash) { } -- Loop over a hash table. something while expr -- Alternative syntax: do "something" while "expr" is true unless () { } -- Not while. until () { } -- Test at bottom of loop. last; -- Break out of a loop. last labed; -- Break out of the loop labeled "label". next; -- Next iteration of a loop. redo; -- Go back to top of loop. Same iteration. foreach $var (@arr) { } -- loop: $var = $arr[i], i = 1:n foreach $var (sort keys %hash) { } -- Loop over sorted hash keys. for (init_exp; test_exp; inc_exp) { } -- "for" control structure for ($i = 1; $i <= 10; $i++) { } -- Count from 1 to 10 ########################################################### # Operators Numeric String ------- ------ == eq != ne < lt > gt <= le >= ge Boolian ------- 0 -- False. 1 -- True (or any other non-zero value). ! -- NOT operator. && -- AND operator. || -- OR operator. str . str -- Concat A%B -- Modulo A**B -- Exponential ########################################################### # Misc printf "string", var1, var2; %g -- Choose real or integer as needed %f -- Float %12.3f -- Float %d -- Decimal %6d -- Decimal, 6 char field width. %x -- Hexedecimal %o -- Octal %s -- String, left justified %10s -- String, right justified, 10 char field width %-9s -- String, left justified, 9 char field width Subroutines: &my_routine (arg0, arg1, ...) sub my_routine { my ($a0) = $_[0]; my ($a0, $a1) = @_; }