typestar

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.

Start this tour

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-word elsif, and unless for the if-not case.
  • foreach loopsThree foreach shapes: walking a list, counting a range, and editing in place.
  • Statement modifiersThe postfix forms that put if, unless, and for after the statement they guard.

Regular expressions

  • Regex matchingBinding strings to patterns with =~, plus !~ and the /i flag.
  • Substitution with s///Search and replace with s///: global edits, and /r when the original must survive.
  • Capture groupsCapture groups three ways: numbered lists, named %+ entries, and /g harvests.

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, cmp for strings, and anything you compute.

Strings

Subroutines

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 featureprint with the newline built in, switched on by a feature pragma.
  • Postfix dereferenceTwo spellings of the same deref: sigil-first @$ref and 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.

The other Perl tours