Robert F's Quick-n-Dirty Perl Tutorial                 

Revised 2/18/2000
Robert G. Ferrell

So, you want to learn Perl? Great. There are lots and lots of books and other Web sites that can help you in this noble quest; rather than list them here I'd suggest you just go to your favorite search engine and type in "Perl Tutorial" or something to that effect. If you're interested in my "condensed" overview, read on.

This tutorial is, as the name implies, an abbreviated overview that hits some of the high points of the incredibly useful Perl language. My comments will be heavily biased in favor of Perl running on Unix, since that's where I do the vast majority of my work. Again, many references exist to educate you on running Perl on other platforms.

I'm not going to cover programming theory, installing Perl, the history of Perl, or anything that isn't directly related to the subject at hand. If you want this sort of information (and I certainly encourage you to seek it out at some point), there are lots of authors out there far more qualified than myself who will be happy to fill you in. This is really just a bare-bones tutorial for people who already know how to drive but need a road map to get to a particular destination.
Don't forget to buckle up!

                 Running Perl                 

There are two ways to get Perl to do work for you. One is by creating an executable program that can be invoked from the command line or as a CGI script. The other is to include one or more valid Perl statements in a string following a perl -e command.

# perl -e 'print "Perl is Great!\n";'

You can even string together commands separated by semicolons:

# perl -e '$dir=qx(pwd);print "The current directory is $dir";'

The -n switch lets you read a file line by line. This is useful if you want to print the file or do regular expression matching in it.

# perl -ne 'print' myfile.txt

# perl -ne 's/\s/-/g; print' myfile.txt

This last example converts all whitespace characters to dashes in myfile.txt and prints out the results.

The other two command line switches that I use routinely are -c and -w.

The -c switch checks the syntax of a Perl script without actually executing the code. If it finds a syntactic mistake, it tries to alert you as to the nature and approximate location of the error.

# perl -c myscript.pl
myscript.pl syntax OK

The -w switch executes the script and shows (non-fatal) warnings, most notably user-created objects which appear only once; e.g., variables which are defined but never referenced.

One of the most useful ways to employ these two switches is to use them in tandem to test a script during development.

# perl -wc myscript.pl
Name "main::foo" used only once: possible typo at myscript.pl line 21.
Name "main::bar" used only once: possible typo at myscript.pl line 37.
myscript.pl syntax OK

Some people always invoke scripts using the -w switch, just to be sure. I don't recommend this practice in a CGI script, because it can generate messages that might confuse and/or annoy the users.


Next | SAPM Home


Created by Robert G. Ferrell