CSE271 Lab 10: Perl

Announcements

Introduction

Perl

Perl Basics

Perl scripts can be run in the same way as shell scripts: 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.

Perl Functions

Perl functions (built in or user defined) are identified by their unique names. Parameters are comma separated, but parentheses are often optional. Define as:

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.

In order to implement ls -l, you needed to pull together a number of helpful library functions:

A sample solution (mostly complete) for reference is here.
Last revised: 26 March 2013, Brian D. Davison.