Introduction to Perl 

or, Learn Perl in Two Hours
Stolen from - MU Campus Computing Short Course
This Laboratory exercise has been adapted from the web course at:
Campus Computing, University of Missouri - Columbia
Document update: 27 September 1996
Location: http://www.cclabs.missouri.edu/things/instruction/perl/perlcourse.html
This version for the University of Surrey is of 20th September 1998.

Lab Preparation

 

Contents

1. What Is Perl? 
2. Course Requisites and Goals 
3. Perl References & Resources
4. State of Perl 
5. Taste of Perl 
6. Storing & Running Perl Programs
Make sure that you do and understand the 2 pieces of WORK (Work! and More Work!) before coming to the lab session.

0. Introduction

As preparation for your time in the laboratory you should read through these pages and ensure you can log in to the departmental UNIX facilities and can use simple UNIX commands to allow you to edit files and compile simple programmes. Instructions to help you with this are available here.  It should not be necessary to print out either the preparation or the experiment web pages. Doing so will be frowned upon. Remember, you have a yearly print quota!

1. What Is Perl?

Perl is a "Practical Extraction and Report Language" freely available for Unix, MVS, VMS, MS/DOS, Macintosh, OS/2, Amiga, and other operating systems. Perl has powerful text-manipulation functions. It eclectically combines features and purposes of many command languages. Perl has enjoyed recent popularity for programming World Wide Web electronic forms and generally as glue and gateway between systems, databases, and users. 

2. Course Requisites and Goals

This course presumes participants have elementary programming experience in a procedural programming language such as C, Pascal, or Basic.

By completing this course and the associated lab work, you should be able to:

  1. Locate reference materials and other resources related to Perl;
  2. Express fundamental programming constructs such as variables, arrays, loops, subroutines and input/output in Perl;
  3. Understand several concepts relatively idiosyncratic to Perl, such as associative arrays, Perl regular expressions, and system interfaces;
  4. Program in Perl for data manipulation, file maintenance, packaging or interfacing system facilities, and for "Common Gateway Interface" Web applications.
To keep this a short course, we won't explain object-oriented concepts and some other facilities appropriate for large projects. Perl, perhaps more than any other computer language, is full of alternative ways to do the same thing; we tend to show only one or two. We will try to stimulate by examples of useful bits of code, results, and questions. Turn to the reference materials for further explanation. 

3. Perl References and Resources

MU Perl and Perl CGI Materials:
www.cclabs.missouri.edu/things/instruction/perl

One of many sites for Perl Software:
www.cis.ufl.edu/perl/

Usenet Newsgroups:
news:comp.lang.perl.announce, news:comp.lang.perl.misc.

Reference Manual:
Larry Wall, Tom Christiansen & Randall L. Schwartz, Programming Perl, 2nd Edition September 1996, 670 pages, O'Reilly and Associates, Inc., ISBN 1-56592-149-6, $39.95. The "Camel Book". A thorough Perl 5 reference with plenty of examples. See perl.oreilly.com.

Tutorial Books:
Hoffman, Perl 5 For Dummies, 408 pages, IDG Books, ISBN 0-7645-0460-6, $29.99.  See www.idgbooks.com.
Randal L. Schwartz, Learning Perl, 1993, 274 pages, O'Reilly and Associates, Inc., ISBN 1-56592-042-2, $24.95 The "Llama Book".

On-line Tutorials:
Web Developer's Virtual Library has tutorials and reference for HTML, VRML, XML, CSS, CGI, Perl, Jav, JavaScript and much more:  wdvl.internet.com.
Tom's Object-Oriented Perl Tutorial:  http://language.perl.com/all_about/perltoot.html
Randy's Column on OO Perl:  http://www.stonehenge.com/merlyn/UnixReview/col13.html

Work!

Learn how to search the Perl 5 online manual using the above URL with Netscape, Mosaic, or another World Wide Web browser! 

4. State of Perl

Two types of programmers use Perl. System administrators like it for the way it glues together system commands to manipulate data and processes, and for its pattern-matching functions aids in system searches and reporting. People developing electronic forms for Unix Web servers find Perl easier to learn and use than C, and for their purposes Perl offers more built-in or publicly available functions such as easy data validation and simple databases.

Larry Wall, Perl's creator, chief architect, implementer, and maintainer, released Perl 5 in October 1994. Perl 5 brings object-oriented capabilities, more data structures, new standard interfaces to system and database functions, and handy new functions. Extensions to Perl for windowing, graphics, and database work are being developed in Perl 5. Full Perl 5 documentation with lots of examples is freely available on-line! See below.

The Perl code in this document works under both Perl 4.036 (the last version of Perl 4) and Perl 5.003 (the latest version of Perl 5 at this writing). The Reference Guide identifies with a double-dagger (as in ++) new features of Perl 5.

For major general-purpose Perl applications, particularly CGI scripts and client or server applications, first check the 'Net for Perl modules that you can exploit! See the FAQ "Perl 5 Module List" regularly posted to the Usenet group comp.lang.perl.announce.
 

5. Taste of Perl

Quite useful Perl programs can be short. Suppose we want to change the same text in many files. Instead of editing each possible file or constructing some cryptic find, awk, or sed commands, you could issue a single command:

Example: Amazing Perl One-Liner That Substitutes Text In Multiple Files

      perl  -e 's/gopher/World Wide Web/gi'  -p  -i.bak  *.html
This command, issued at the Unix prompt, executes the short Perl program specified in single quotes. This program consists of one perl operation; it substitutes for original word "gopher" the phrase "World Wide Web", (globally, ignoring case). The remainder of the Unix command indicates that the perl program should run for each file ending in ".html" in the current directory. If any file "blah.html" needs changing, a backup of the original is made as file "blah.html.bak". Programming Perl lists additional handy one-liners.
Go on try it for yourself!

For those accustomed to "classic" procedural programming, the "amazing one-liner" above can be expanded in Perl in a style more like C or Pascal:

Example: Global Substitution, The Scenic Route

#!/usr/local/bin/perl -w
# File:  go2www    
# This Perl program in classic programming style changes
# the string "gopher" to "World Wide Web" in all files
# specified on the command line.
# 19950926 gkj
$original='gopher';
$replacement="World Wide Web";
$nchanges = 0;
# The input record separator is defined by Perl global 
# variable $/.  It can be anything, including multiple 
# characters.  Normally it is "\n", newline.  Here, we
# say there is no record separator, so the whole file
# is read as one long record, newlines included.
undefine $/;

# Suppose this program was invoked with the command
#     go2www ax.html  big.basket.html  candle.html
# Then builtin list @ARGV would contain three elments 
# ('ax.html', 'big.basket.html', 'candle.html')
# These could be accessed as $ARGV[0] $ARGV[1] $ARGV[2] 

foreach $file (@ARGV) {
     $file = $ARGV[$i];
     if (! open(INPUT,"<$file") ) {
         print STDERR "Can't open input file $bakfile\n";
         next;
     }

     # Read input file as one long record.
     $data=<INPUT>;
     close INPUT;

     if ($data =~ s/$original/$replacement/gi) {
         $bakfile = "$file.bak";
         # Abort if can't backup original or output.
         if (! rename($file,$bakfile)) {
             die "Can't rename $file $!";
         }
         if (! open(OUTPUT,">$file") ) {
             die "Can't open output file $file\n";
         }
         print OUTPUT $data;
         close OUTPUT;
         print STDERR "$file changed\n";
         $nchanges++;
     }

     else {  print STDERR "$file not changed\n"; }
}
print STDERR "$nchanges files changed.\n";
exit(0);
Questions:
  1. What do you guess that the "!" means, as in:
  2.             if (! open(OUTPUT,">$file") ) {
                    die "Can't open output file $file\n";
                }
  3. What does the ">" probably mean here? Compare with "open(INPUT ...)".
  4. What does "die" do?
  5. Some languages use "IF ... THEN DO ... END; ELSE IF ... THEN DO ... END". How is this notated in Perl?
  6. What does $nchanges++ do?
The Perl Creed is, "There is more than one way!" This noble freedom of expression however results in the first of the four Perl Paradoxes: Perl programs are easy to write but not always easy to read. For example, the following lines are equivalent!
    if ($x == 0) {$y = 10;}  else {$y = 20;}
    $y = $x==0 ? 10 : 20;
    $y = 20;  $y = 10 if $x==0;
    unless ($x == 0) {$y=20} else {$y=10}
    if ($x)  {$y=20} else {$y=10}
    $y = (10,20)[$x != 0];

6. Storing and Running Perl Programs

More WORK!

Hello

To test your ability to store and run a Perl program, enter and execute something like this classic code:
#!/usr/local/bin/perl -w
if ($#ARGV >= 0) { $who = join(' ', @ARGV); }
else { $who = 'World'; }
print "Hello, $who!\n";

Here's How:

Let us assume that the above lines are stored in a Unix file ~/labs/hello. (That's in your home directory, subdirectory labs, file hello.) You can then run the program by entering a command like:
    ~/labs/hello
    ~/labs/hello Citizens of Earth
    hello          (If you're in the ~/labs directory.)
If you expect to use this program a lot and want to execute it as a command, then you need to do two things.

1. The first line of the program should, after a "#!", specify the location of the perl command, typically as #!/usr/local/bin/perl, as illustrated in the preceding example program. This line can also give command options, like -w (warn of possible inconsistencies). It is advisable to perform a syntax check first by using the command option -cw, i.e.:

    perl -cw ~/labs/hello

 And then run the program with the following line: ^M

    perl ~/labs/hello

2. Set the execute permissions of the program file. To make the file executable (and readable and writable) by only yourself, use a Unix command like:

    chmod 700 ~/labs/hello
To make it executable and readable by all enter a Unix command like the following:
    chmod a+rx ~/labs/hello
You may also need to use chmod a+x on the directories ~ and ~/labs. See "man chmod" for details and the security implications.


With thanks to MU Campus Computing for allowing us to adapt their course material so freely.
This HTML document and the Postscript derived from it may be freely copied and adapted, so long as appropriate credit is given to MU Campus Computing. Anything less or anything more would not be the Perl Way. I solicit your suggestions and comments!
Special thanks to Hugo van der Sanden for corrections.
- Greg Johnson - ccgreg@missouri.edu
 Published by: MU Campus Computing - Webmaster <webmaster@www.missouri.edu>