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:
- 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
print
, shebangs, and running scripts. - Input and output -input with
<STDIN>
, output withprint
andsay
, creating$scalar
variables, andchomp
. - Arrays and slices – creating and accessing simple
@array
data. - Hashes – creating and accessing simple
%hash
data. - Simple loops – scalar and list context,
scalar
, array interpolation in strings,increment++
, decrement--
, C-stylefor
loops, basic mathematical operators and concatenation. - Prettier loops and nicer code –
foreach
loops, the default$_
variable, quoting strings withqq
,q
, andqw
,$/
and statement modifierforeach
. - Bondage, discipline and subroutines – creating user-defined functions with
sub
andreturn
, the@_
array, lexical scope, theuse strict
pragma andmy
variables, more loops (while
anduntil
) and things to do to arrays (push
,pop
,shift
,unshift
,reverse
andsplice
). - Conditionals – the truth according to Perl,
if
,elsif
,else
,unless
, numerical (==
) and string (eq
) comparisons. - More hashes and sorting – manipulating hashes with
each
,keys
andvalues
, sorting things withsort
, the ASCIIbet,sort
subroutines, Orcish manoeuvre and Schwartzian transform. - Symbol table – the symbol table and typeglobs, dynamic
local
and lexicalmy
scoping; follows on from hashes, but probably skippable for the casual reader. - Files and directories – opening, reading and writing files, opening and reading directories,
die
andwarn
, logical operators (or
,||
and friends), operator precedence, pipes, running external programs withsystem
, and getting information withperldoc
. - Regexes – regular expressions,
/ /
,=~
and!~
, character classes, quantifiers, alternation, capturing matches, back-references and modifiers. - Substitutions, splitting and joining – list manipulation with
split
,join
,grep
andmap
, thes///
andtr///
substitution and transliteration functions. - References and data structures – taking and dereferencing references, arrayrefs, hashrefs, coderefs, closures,
do
,Data::Dumper
. - Bits and bobs – capturing exceptions,
eval
,$?
, telling thelocaltime
,sleep
,sprintf
andprintf
, loop control withLABELS:
,last
andnext
, heredocs. - Installing modules – installing modules from CPAN,
ppm
, and some useful modules I couldn’t live without:Getopt::Long
,DBI
,File::
,Net::
,HTML::Template
. - Packages and writing modules –
h2xs
,Module::Starter
,Exporter
and documenting code with POD. - Classes and objects – object oriented syntax, classes, objects, inheritance, hand-rolling
bless
ed hashrefs. - Moose objects and roles – using
Moose
to 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.
2 comments
Author
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!
Author
Making promises I can’t keep, it would appear. Easter 2013 is the new target for *all finished*.