You’re going to need three things to write Perl.
- A computer.
- A text editor.
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:
- Perl dot org, for all things perl.
- Perl dot com, for news and views.
- CPAN, for modules.
- Perlmonks, for help.
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.
- Not a hello world script – using
- Input and output -input with
<STDIN>, output with
- Arrays and slices – creating and accessing simple
- Hashes – creating and accessing simple
- Simple loops – scalar and list context,
scalar, array interpolation in strings,
forloops, basic mathematical operators and concatenation.
- Prettier loops and nicer code –
foreachloops, the default
$_variable, quoting strings with
$/and statement modifier
- Bondage, discipline and subroutines – creating user-defined functions with
@_array, lexical scope, the
use strictpragma and
myvariables, more loops (
until) and things to do to arrays (
- Conditionals – the truth according to Perl,
unless, numerical (
==) and string (
- More hashes and sorting – manipulating hashes with
values, sorting things with
sort, the ASCIIbet,
sortsubroutines, Orcish manoeuvre and Schwartzian transform.
- Symbol table – the symbol table and typeglobs, dynamic
myscoping; follows on from hashes, but probably skippable for the casual reader.
- Files and directories – opening, reading and writing files, opening and reading directories,
warn, logical operators (
||and friends), operator precedence, pipes, running external programs with
system, and getting information with
- Regexes – regular expressions,
!~, character classes, quantifiers, alternation, capturing matches, back-references and modifiers.
- Substitutions, splitting and joining – list manipulation with
tr///substitution and transliteration functions.
- References and data structures – taking and dereferencing references, arrayrefs, hashrefs, coderefs, closures,
- Bits and bobs – capturing exceptions,
$?, telling the
printf, loop control with
- Installing modules – installing modules from CPAN,
ppm, and some useful modules I couldn’t live without:
- Packages and writing modules –
Exporterand documenting code with POD.
- Classes and objects – object oriented syntax, classes, objects, inheritance, hand-rolling
- Moose objects and roles – using
Mooseto define classes declaratively, code reuse with roles.
- Command-line – one-liners, command-line switches.
- Debugging – finding bugs and squashing them, avoiding creating bugs in the first place.