Perl
Practical Extraction and Report Language
Larry Wall developed Perl because awk was inadequate for a report-generating
job he was doing.
- Perl is an interpreted language
- 1st line indicates interpreter to use:
#!/usr/local/bin/perl
- Interpreter parses and compiles the program before executing
- Sources for Perl:
http://www.perl.com/CPAN
Perl Basics
- Source file is executable (after chmod)
- # starts a comment (continues to end of line)
- Free-format language: whitespace (space, tab, newline) is usually optional
- Perl statements terminated by ;
- Program automatically gets 3 filehandles:
STDIN, STDOUT, STDERR
- Variables are defined as they are used
Scalar Data
Scalars are numbers and strings:
- All numbers are represented internally as double- precision floats
- Floating point literals the same as for C
- Integer literals include decimal, octal (number begins with 0), and
hexadecimal (number begins with 0x or 0X)
- Strings are sequences of characters
- Single-quoted strings may contain any character including newline
- Double-quoted strings interpret variables and escape characters
Scalar Operators
Perl converts values to proper types for operator
- Numeric operators
- Arithmetic operators
+ - * / ** %
- Relational operators
< <= == >= > !=
- Boolean operators
|| &&
- Auto increment and decrement
++ --
- String operators
- Concatenation with ‘.’
$greeting = ‘Welcome’ . $number;
- String comparisons
eq ne lt gt le ge
- String repetition with ‘x’
“la” x 3 # produces “lalala”
- Chop & chomp remove last character of a string
chop($a); # remove last character
chomp($a); # only remove newline
Scalar Variables
Scalar variables are always preceded by $
- $ must be followed by a letter
- may thereafter be followed by letters, digits, underscores (up to 255
characters)
- case sensitive
Scalar variables are assigned values as in C
- Regular assignment
$a = 17 + $b;
$a = $b = $c = 5;
- Binary assignment
+= -= *= /= .=
- Variables that have not been assigned a value have a value of undef
Lists
A list is ordered scalar data
- Represented literally in ( )
(1, 2, 3) # list of three values
($a + $b, $c . " " . $d) # list of two values
( ) # empty list
- Ranges may be represented
(1 .. 5) # same as (1, 2, 3, 4,
5)
(1.3 .. 5.1) # same as (1.3, 2.3, 3.3, 4.3)
(5 .. 1) # same as ( )
- Quote word function creates a list of strings cleanly
qw(Elmo Zoe Telly)
Arrays
An array is a variable that holds a list (i.e., 0 or more scalar values)
- Array variable names always begin with @; follow other rules for scalar
variables
- Assignment copies the list over
@fred = (1, 2, 3);
@barney = @fred;
@wilma = (0, @fred, 4);
- Arrays that have not been assigned a value have a value of ( )
- Individual elements of the array are addressed as scalar values, starting
at index 0
$fred[0] = 0; # now @fred is (0,2,3)
- $#fred returns the index value of the last element of @fred
- Assignment to an element beyond the ends of the current array extends
the array, filling in unassigned elements with undef
Input and Output
<STDIN> may be used as a scalar value
- Reads a string from standard input up to (and including) the newline
character
$a = <STDIN>;
print prints to standard output
- Prints out argument; parentheses optional
print ("Hi There!");
print "Hi There!";
- Concatenates list items
print ("You are #", $nextnum, " on the list");
- Converts numeric values to strings