Perl for Linguists
Michael Hammond
University of Arizona
Presentation materials:
Various links:
- Basic structure
- Statements. A program is composed of a list of statements. Each
statement is terminated with a semicolon.
- Groups. Statements can be grouped together with curly braces into
"statement groups".
- Data types
- Strings are denoted with double or single quotes.
- Numbers have no special punctuation.
- Variables
- Simple scalar variables are marked with a leading dollar sign,
e.g.
$myVar, $x,
$a_long_variable_name, etc.
- Arrays are lists of related variables. The entire array is
indicated with a leading
@, e.g. @anArray,
@myArray. Individual members of the array are referred
to by index (starting at zero). The index occurs in square brackets
and in that case the array is prefixed with a leading dollar sign,
e.g. $anArray[0], $myArray[35], etc.
- Hashes are like arrays except that each element in the list has a
name, rather than an index. The entire hash is indicated with a
leading
%, e.g. %myhash,
%boringhashname, etc. Individual slices are referred to
with a leading dollar sign and with the hash name in curly braces,
e.g. $myhash{bob}
$boringhashname{oranges}.
- Some functions
print(): prints something.
localtime(): gets the local time.
rand(): returns a random number.
push(): adds something to the end of an array.
pop(): returns the last element from an array (and
removes it from the array).
- Input-Output
print(): prints something.
open() and close(): creates a file
handle from a file name and then closes the handle.
< >: reads a line from a file handle.
- Regular expressions
- Concatenation, e.g.
/ab/ the string composed of
a followed by b.
- Union, "or", e.g.
/a(b|cd)e/ the string
abe or the string acde.
- Kleene star, "any number of", e.g.
/ab*c/ the
strings ac, abc, abbc,
abbbc, etc.
variable =~ /regular-expression/: true if the
variable matches the regular expression.
variable =~ s/regular-expression/string/: replaces
the first instance of the regular expression by the string.
- Control structures
if (condition) {statement group}: conditional
application. If the condition is true, the statement group gets a
chance to apply.
while (condition) {statement group}: conditional
iteration. As long as the condition remains true, the statement group
keeps applying.
for (initialize; end-condition; iteration) {statement
group}: quantified iteration. The statement group
iterates a specified number of times.
foreach variable (list) {statement group}: list
iteration. The statement group iterates once for every element of the
list.
- Object-oriented programming
- Object-oriented programming involves creating objects. Each
object has functions/methods and data that it uses to interact with
other objects.
new(): The typical name for the method that creates
some object. For example, $mybug = Bug->new() would
create a new object of the Bug class and assign it to
the variable $mybug.
->: Object methods and data are usually referred
to with the dereferencing extractor, e.g.
$mybug->{thestring} would refer to a string within
the object held in $mybug. Something like
$mybug->doit() would call the doit()
method of the Bug class.
@ISA: Objects can inherit data and methods from
other objects. Parent objects are indicated as members of this
array.
Mike Hammond