Language basics
27 steps in 9 sets of Perl.
Perl held the internet together before the word backend existed: CGI scripts, log pipelines, bioinformatics, and the sysadmin one-liners that still run at 3am. Its motto — there is more than one way to do it — built a language of sigils, contexts, and regexes so central they shaped every engine that came after (PCRE is 'Perl-compatible' for a reason). Typing Perl means three sigils under your fingers and pattern matches everywhere.
This tour covers Perl as it is best written today: strict and warnings on every file, the three variable shapes and the contexts that flip between them, the regex toolkit in depth, map and grep over explicit loops, references for real data structures, and modern touches like say and postfix deref. The encore ships complete programs. Every seed passes perl -c and runs start to finish under Perl 5.38.
Scalars & sigils
- Scalars & sigilsOne
$sigil for every single value, whether it holds a string or a number. - Arrays & listsThe
@sigil for the whole array,$for one element, negative indexes for free. - HashesPerl's key-value shape:
%for the whole hash,$plus curly braces for one value.
Control flow
- if, elsif, and unlessBranching with
if, Perl's one-wordelsif, andunlessfor the if-not case. - foreach loopsThree
foreachshapes: walking a list, counting a range, and editing in place. - Statement modifiersThe postfix forms that put
if,unless, andforafter the statement they guard.
Regular expressions
- Regex matchingBinding strings to patterns with
=~, plus!~and the/iflag. - Substitution with s///Search and replace with
s///: global edits, and/rwhen the original must survive. - Capture groupsCapture groups three ways: numbered lists, named
%+entries, and/gharvests.
Lists & context
- Scalar and list contextThe idea that makes Perl Perl: the same expression means two things depending on what receives it.
- map and grepTransform and filter lists in one line each — then chain the two into a pipeline.
- Custom sort orderssort takes a comparator block:
<=>for numbers,cmpfor strings, and anything you compute.
Strings
- String functionsThe everyday string toolkit: case, length, search, slice, split and join.
- Formatting with sprintfprintf-style templates for columns, padding and number bases.
- Indented heredocsMulti-line strings with
<<~, which strips the indentation so the code stays pretty.
Subroutines
- Subroutines and @_The classic calling convention: every argument arrives in one flat array named
@_. - Subroutine signaturesNamed parameters in the declaration itself — stable since Perl 5.36, no
@_unpacking dance. - Anonymous subs and closuresCode in a variable:
sub { ... }with no name returns a reference you call with an arrow.
References & structures
- ReferencesHow Perl points at arrays and hashes instead of copying them.
- Hash of arraysOne key, many values: the workhorse structure for grouped data.
- Nested data structuresAnonymous
[]and{}nest to any depth, like JSON before JSON.
Files & modern Perl
- File I/OThe modern open idiom: three arguments, a lexical handle, and
or die. - The say feature
printwith the newline built in, switched on by a feature pragma. - Postfix dereferenceTwo spellings of the same deref: sigil-first
@$refand postfix$ref->@*.
Encore
- word_freq.plA word-frequency report over an inline passage, driven by one hash.
- grade_report.plAverages per-student scores from a hash of arrays into a graded table.
- log_parser.plBuckets log lines by severity with one named-capture regex.