Perl

You’re going to need three things to write Perl.

  • A computer.
  • The perl interpreter.
  • A text editor.

The computer thing is easy, as I presume you have one to read this. You will need some basic familiarity with your computer’s file system, and how to open its command line interface.

The perl interpreter works on just about any computer you can name, so whether you’re running MacOS, Windows, or some flavour of Unix, then all is well. You can download the source and binaries from CPAN, the Comprehensive Perl Archive Network, where you can also obtain sundry modules. If you’re running Linux or some other Unix (including MacOSX, which is BSD under the hood), you will almost certainly have a recent version of perl on your system anyway. If not, install it yourself by compiling the source code from CPAN with the C compiler of your system (e.g. gcc). If you’re on Windows you can download a precompiled binary distribution from Active State or Strawberry Perl.

A text editor is something like vi or vim, emacs, UltraEdit, Notepad++, or even (ugh) Notepad. It is not Word. You don’t need a bells-and-whistles text editor, or integrated design environment to write decent code.

Some conventions I’ll be using:

Code to be entered into a text editor, saved and run
Things to type into a command line (bash/cmd/<em>etc.</em>)
Output that perl spits onto the command line

You may find the following Perl sites useful:

This tutorial was originally published on my old website, but I stopped maintaining it and it became old and out-of-date, so I decided to pull it. I’ve recently ported it, updating it as I go along.

  1. Not a hello world script – using print, shebangs, and running scripts.
  2. Input and output -input with <STDIN>, output with print and say, creating $scalar variables, and chomp.
  3. Arrays and slices – creating and accessing simple @array data.
  4. Hashes – creating and accessing simple %hash data.
  5. Simple loops – scalar and list context, scalar, array interpolation in strings, increment++, decrement--, C-style for loops, basic mathematical operators and concatenation.
  6. Prettier loops and nicer codeforeach loops, the default $_ variable, quoting strings with qq, q, and qw, $/ and statement modifier foreach.
  7. Bondage, discipline and subroutines – creating user-defined functions with sub and return, the @_ array, lexical scope, the use strict pragma and my variables, more loops (while and until) and things to do to arrays (push, pop, shift, unshift, reverse and splice).
  8. Conditionals – the truth according to Perl, if, elsif, else, unless, numerical (==) and string (eq) comparisons.
  9. More hashes and sorting – manipulating hashes with each, keys and values, sorting things with sort, the ASCIIbet, sort subroutines, Orcish manoeuvre and Schwartzian transform.
  10. Symbol table – the symbol table and typeglobs, dynamic local and lexical my scoping; follows on from hashes, but probably skippable for the casual reader.
  11. Files and directories – opening, reading and writing files, opening and reading directories, die and warn, logical operators (or, || and friends), operator precedence, pipes, running external programs with system, and getting information with perldoc.
  12. Regexes – regular expressions, / /, =~ and !~, character classes, quantifiers, alternation, capturing matches, back-references and modifiers.
  13. Substitutions, splitting and joining – list manipulation with split, join, grep and map, the s/// and tr/// substitution and transliteration functions.
  14. References and data structures – taking and dereferencing references, arrayrefs, hashrefs, coderefs, closures, do, Data::Dumper.
  15. Bits and bobs – capturing exceptions, eval, $?, telling the localtime, sleep, sprintf and printf, loop control with LABELS:, last and next, heredocs.
  16. Installing modules – installing modules from CPAN, ppm, and some useful modules I couldn’t live without: Getopt::Long, DBI, File::, Net::, HTML::Template.
  17. Packages and writing modulesh2xs, Module::Starter, Exporter and documenting code with POD.
  18. Classes and objects – object oriented syntax, classes, objects, inheritance, hand-rolling blessed hashrefs.
  19. Moose objects and roles – using Moose to define classes declaratively, code reuse with roles.
  20. Command-line – one-liners, command-line switches.
  21. Debugging – finding bugs and squashing them, avoiding creating bugs in the first place.

2 comments

  1. I hope to get old-fashioned objects done before the end of the year (2012), but I have a feeling I won’t have this completely finished before February 2013. The other tutorial sections need a lot more work than the basic syntax ones; indeed, a couple of them need writing from scratch!

  2. Making promises I can’t keep, it would appear. Easter 2013 is the new target for *all finished*.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.