How to Use Perl
These notes are written to help you learn the basics of
Perl
Useful Perl Commands
- Opening Files for Input and Output
- open(OUTFILE, ">junk.txt") or die "Can't Open File $!\n"
The > is associated with an output file. The $! variable contains the most recent operating system error.
- open(OUTFILE, ">>junk2.txt") or die "Can't Open File $!\n"
The >> opens the file for appending to the end of the file.
- open(INFILE, "<junk3.txt") or die "Can't Open File $!\n"
The < is associated with an input file.
- String Matching -- Letters
- $mystring =~ /anytexthere/ -- TRUE if "anytexthere" is found in the string $mystring
- $mystring =~ /anytexthere/i -- TRUE if "anytexthere" or "ANYtextHERE" or "AnyTextHere" etc, are found in the string $mystring
- $mystring =~ /anytexthere/g -- Match all occurrences of "anytexthere" -- multipleTRUEs
- $mystring =~ /anytexthere/s -- If there are line breaks, ^J's, use "." operator to add strings together into one
long string (that is, take out any line-wraps). TRUE if "anytexthere" is found
- $mystring =~ /^anytexthere/ -- TRUE only if "anytexthere" is found at the beginning of the string $mystring
- $mystring =~ /anytexthere$/ -- TRUE only if "anytexthere" is found at the end of the string $mystring
- $mystring =~ /\banytexthere\b/ -- TRUE if " anytexthere " is found in the string $mystring -- that is,
whitespace on either side of "anytexthere"
- $mystring =~ /\Banytexthere/ -- TRUE if "anytexthere" is found inside a word in string $mystring -- e.g.,
"exitanytexthere"
- $mystring =~ /anytexthere+/ -- TRUE if one or more "anytexthere"s are found -- e.g., "anytexthereanytexthere", etc. -- in the string $mystring
- $mystring =~ /anytexthere./ -- TRUE if
the string "anytexthere" followed by any single character ("." means any character) --
e.g., "anytexthereA", "anytexthereo", etc. -- is found in the string $mystring
- $mystring =~ /anytexthere.*/ -- TRUE if
the string "anytexthere" followed zero or more characters is found in the string $mystring
- $mystring =~ /anytext*here/ -- TRUE if
the string "anytexthere" or "anytextthere" or "anytexttthere", etc., is found in the string $mystring
- $mystring =~ /anytext?here/ -- TRUE if
the string "anytexthere" or "anytexhere" is found in the string $mystring -- the "?" means the character
preceding it is optional
- String Matching -- Numbers, WhiteSpace, and Letters
- $mystring =~ /\d/ -- TRUE if any single digit, 0,1,...,9 is found in the string $mystring
- $mystring =~ /\d\d/ -- TRUE if any two digits, 00, 12, 88, 99, etc., is found in the string $mystring
- $mystring =~ /\d+/ -- TRUE if one or more digits are found in the string $mystring
- $mystring =~ /\D/ -- TRUE if any single character not a digit is found in the string $mystring
- $mystring =~ /\s/ -- TRUE if any single WhiteSpace, spacebar, tab (^I), newline (^J or ^M) is found in the string $mystring
- $mystring =~ /\S/ -- TRUE if any single character not a WhiteSpace is found in the string $mystring
- $mystring =~ /\s\d+/ -- TRUE if any single WhiteSpace followed by any number of digits -- e.g., " 655" -- is found in the string $mystring
- $mystring =~ /\w/ -- TRUE if any single word character (digits and letters) is found in the string $mystring
- $mystring =~ /\W/ -- TRUE if any single non-word character (not a digit or letter) is found in the string $mystring
- String Matching -- Character Classes
- $mystring =~ /[pgmsb]et/ -- TRUE if any of the words pet, get, met, set, or bet are found in the string $mystring
- $mystring =~ /[a-g]/ -- TRUE if any of the characters a, b, c, d, e, f, g, or h are found in the string $mystring
- $mystring =~ /[.!?] / -- TRUE
if any the punctuation marks ".", or "!", or "?" are found followed by two spaces
in the string $mystring
- $mystring =~ /[^pg]/ -- TRUE if anything besides
p or g is found in the string $mystring
- $mystring =~ /[^><]/ -- TRUE if anything besides
> or < is found in the string $mystring
- [0-9] is the same as \d
- [^0-9] is the same as \D
- [0-9a-zA-Z_] is the same as \w
- [^0-9a-zA-Z_] is the same as \W
- [ \t\n\r\f] (space, tab, newline, carriage return, form feed)
is the same as \s
- [^ \t\n\r\f] (any WhiteSpace character) is the same as \S
- Metacharacters -- ^ $ + * ? . | ( ) { } \ / [ ] -- When
these characters are part of an expression and you wish to match them in a search, then the search argument
must use \ before the metacharacter. For example:
- $mystring =~ /\.htm/ -- TRUE if the string
$mystring containts ".htm"
- $mystring =~ /http:\/\// -- TRUE if the string
$mystring containts "http://"
- String Manipulation
- Splitting a String
- @stuff = split(/,/,$mystring) -- Takes a string with commas in it,
splits the string at every comma, and stores the results in the vector @stuff. If $mystring="abc,def,ghi" then
@stuff[0] = abc, @stuff[1] = def, @stuff[2] = ghi.