CSE271 Lab 10: Perl
Announcements
- Exam 2 is being graded.
- Homework #6 is due tomorrow.
- Homework #7 is defined at the end of this lab.
Introduction
Perl
- is an interpreted programming language
- is known for its power and flexibility
- combines the familiar syntax of C, C++, sed, awk, grep, sh, and csh
into one powerful tool
- was invented by Larry Wall
- is an acronym for "Practical Extraction and Report Language"
- is optimized for string manipulation, I/O, and system tasks
- has built-in functions for almost everything that's in section 2 of
the UNIX manual pages
Perl Basics
Perl scripts can be run in the same way as shell scripts
- by perl scriptname
- by making the script executable and including an appropriate !# line
Perl ignores extra whitespace -- indent or not, it's up to you.
Comments start with a # and go to the end of the line.
All Perl statements end in a ;
Perl Variables
Perl variables don't need to be declared before use, and their type
is inferred from their use.
Variable identifiers are composed of letters, numbers, and the
underscore and are case sensitive (as is all of Perl).
A scalar variable is a single value, either numeric or a character
string.
Scalars are prefixed with a $, e.g., $username.
Arrays (lists) are prefixed with @, e.g., @array, but individual
members subscripted with the correct type, as in $array[4], and allocation is
automatic.
$this_is_a_variable = "some string";
$var2 = 44;
@list = ("one", "two");
foreach $i (@list) {
print "Element: $i\n";
}
Perl Strings
There are several kinds of quotes, which generate different results.
Perl Functions
Perl functions (built in or user defined) are identified by their
unique names.
Parameters are comma separated, but parentheses are often optional.
print("length: " ,length("hello world"));
# prints the same thing as
print "length: ",1, length "a";
Define as:
sub functionname {
statements
}
functionname(list of parameters);
Associative Arrays (Hashes)
Perl provides the built-in ability to handle associative arrays.
These are arrays that are ordered not by an integer index, but by an
arbitrary string.
Usually think of them (the string index values) as keys with
associated values.
$principal{"clarinet"} = "Susan Bartlett";
$principal{"basson"} = "Andrew Vandesteeg";
$principal{"flute"} = "Heidi Lawson";
$principal{"oboe"} = "Jeanine Hassel";
@woodwinds = keys(%principal);
@woodwindPrincipals = values(%principal);
Digging Deeper into Perl
Perl is huge; there is always more than one way to do something.
It has loops, pointers, object-oriented support, full regular
expressions, many supporting libraries, etc.
While we have started to scratch the surface here, I recommend that
you get more comfortable by skimming through a Perl tutorial.
If you are uncertain of your programming skills, I suggest: Picking
Up Perl. If you are more confident, try Perl
in 20 pages.
There is lots (perhaps too much!) of Perl
documentation online and there are books on Perl (such as
Learning Perl and Programming Perl) in the Lehigh
Safari e-book library.
A first exercise
Now write a very simple Perl script that prints out the parameters given
to it, like the echo(1) command. It should work like this:
% ./echo.pl hello world
hello world
Note that we haven't reviewed how to examine and interpret script (or
function) parameters. You'll have to find that out from a tutorial as
mentioned above.
A more significant exercise
When you have worked through a Perl tutorial, you can begin
homework 7,
which re-implements the apache log file parser in Perl,
is due next Tuesday (8 days from how).
Last revised: 2 April 2007, Brian D. Davison.