Perl Tutorial

Lessons

The tutorial is split into twenty-one sections, although you'll probably find it easier if you start from the beginning, especially if you're new to Perl. Lessons zero to ten deal with the basics, and the rest deal with more advanced topics, like servers, perl's guts, and parsing. Lesson 12 seems particularly popular: it deals with perl under Windows. The tutorial should be in line with modern Perl practices, so hopefully you won't see any more bareword filehandles, two-argument open or -w switches.

Lesson 0. Zeroeth things first.

What you'll need to run perl and program Perl.

Lesson 1. First things second.

Shebangs and running perl scripts. Basic input and output with <STDIN> and print. Creating scalar variables.

Lesson 2. No-one likes being type cast.

Perl data types: scalars, arrays, and hashes. Scalar and list context. Numbers and strings. Slicing arrays and hashes, and quoting strings. for and foreach loops, and the infamous $_ variable. Mathematical and string functions.

Lesson 3. Bondage, discipline and subroutines.

my variables, strict mode and subroutines. Things to do to arrays: push, pop, shift, unshift and splice. The infamous @_ variable. The truth according to perl, while and until loops. Conditional branching with if, elsif and else. Comparing strings and numbers with >, gt and their friends.

Lesson 4. Anyone got some hash? Sorted.

Manipulating hashes: each, keys and values. Sorting things: sort, the ASCIIbet, sort subroutines. The symbol table and typeglobs. Dynamic and lexical scoping.

Lesson 5. Save it for later.

Opening, reading and writing files. Opening and reading directories. die and warn, and logical operators (or, || and their friends). Operator precedence, pipes and running external programs with system. Getting information with perldoc.

Lesson 6. Line noise.

Regular expressions (regex) and matching patterns. The split and join operators. grep and map.

Lesson 7. Arrays of hashes of arrays of hashrefs.

References for multidimensional data structures. Running perl from inside perl : eval. Loop control, printf, sleeping and telling the time.

Lesson 8. Classy.

Objects and classes, writing perl modules and documenting them with POD.

Lesson 9. Black boxes.

Common perl modules and how to use them: CGI, Getopt, DBI, File, Net, Tk. Using heredocs and the command line Perl switches.

Lesson 10. Guts and gore.

Perl 6 and perl's guts.

Lesson 11. De Bugger.

Fixing wayward scripts: common goofs and debugging. How not to make life difficult for yourself in the first place.

Lesson 12. Casting Perls before swine.

How to get the best out of Perl on a Windows box.

Lesson 13. Sock it to them.

Using sockets, and coding servers and clients. Creating a very simple web client and mp3 server.

Lesson 14. We've come a long way.

Extending Perl with C and manipulating perl's internal data types.

Lesson 15. Tied up in knots.

Creating persistent data by tying hashes to external files and databases.

Lesson 16. Over the top.

Overloading operators in classes, so that 1 and 1 is 3. Using prototypes and wantarray to mimic perl built-ins and create constants. Advanced sorting using the Orcish manoeuvre and the Schwartzian transform for efficiency.

Lesson 17. Universal glue.

Using XML::Parser to parse an XML file and generate a set of HTML documents from it.

Lesson 18. Reinventing the wheel.

The limits of the regex: using Parse::RecDescent to define and use grammars.

Lesson 19. Design and technology.

Using HTML::Template, taint checking and CGI to create secure, maintainable CGI scripts.

Lesson 20. Pretty pictures.

The PerlMagick interface to the ImageMagick library.

Lesson 21. Heavy widgetry.

The Tk toolkit for creating graphical user interfaces.

Zeroeth things first

Well, it's fairly obvious you're going to need three things.

The computer thing is easy, as I presume you have one to read this. Perl works on just about any computer you can name, so whether you're running MacOS, Windows, and particularly any flavour of Unix, then all will be fine. You can even get perl for weird things like Amiga and DOS.

The perl thing is equally simple (although it's your problem to make sure you have sufficient privileges to install it!). You can download the perl source and binaries from CPAN, the comprehensive perl archive network, where you can also obtain any and sundry modules.

A text editor is something like vi or vim, emacs, UltraEdit, or even *gulp* MS Notepad (not recommended however, it has issues with newlines). It is not Word. You don't need a bells-and-whistles text editor, or integrated design environment to write decent code, but it may help.

When you come to installing modules, you may find it useful to grab some standard Unix tools. Most Unices will have these (obviously), but Windows is lacking a number of tools that will make your life easier. In particular, nmake or some other make tool is indispensable. MacOS-X may need this installing from the developer's CD (it comes with the OS, but isn't installed by default). ftp, gzip/unzip, tar, ncftpget, wget, less and lynx are also handy for module installs. These can be obtained from various porters: I recommend Cygwin, which even comes with its own port of perl. If you ever want to install modules with C extensions (XS modules), you will also need the C compiler used to build your version of perl: gcc will be available on most Unices, and in the MacOS-X developers' tools, cl.exe from Visual Studio 6/.NET will not be available on Windows, and is an expensive nightmare to obtain. If you're playing with Perl with an eye to CGI scripting, you will probably also need an FTP client, FileZilla is a lovely SourceForge project with a good GUI.

A few things on conventions I'll be using:

perl code to be entered into a text editor, saved and run looks like this,
things to type into a command line (bash/cmd/etc.) look like this,
and things that perl spits out on the command line look like this.

Hope that's all clear.

In case it isn't, here are the most useful Perl sites I've come across:

Next…