CSE271 Lab 10: Perl
Announcements
- Quiz #6: Low 3, High 10, Mean 8.5.
- Friday's exam will be on coding style, debugging, bash, and UNIX programming.
- Homework #6 was due yesterday.
- Project #7 is due tonight.
- 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 is 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.
Perl Strings
There are several kinds of quotes, which generate different results.
- The single quote 'like this' is used to surround literal text -- no
variable expansion
- Double quotes "like this" are used when variable expansion is
permitted
- The backtick `like this` surrounds system calls, just as in shell
programming
$this_is_a_variable = "some string";
$var2 = 44;
$varstring = 'no expansion of $var2';
$rightnow = `date`;
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); # creates an array containing wind instruments
@woodwindPrincipals = values(%principal); # contains an array of people names
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,
and is due April 5.
Solutions to last week's lab
Last week we implemented ls, including the features associated with the -l option.
We started with
a
working version of ls.
- Were you able to implement the important details of ls -l?
- Size, last modified
- Owner and group
- Type and permissions
In order to implement ls -l, you needed
to pull together a number of helpful library functions:
- The lstat() function provides the file details we need.
- The getpwuid() function provides access to user information.
- Use ctime() to convert modification time.
- Use bitwise masks from </sys/stat.h> to extract types and permissions from mode.
A sample solution (mostly complete) for reference is
here.
Last revised: 26 March 2013, Brian D. Davison.