Perl — a learning library¶
Perl for text processing, measured. One idea per page. Every claim on every page is backed by a program that runs, whose output a tool pastes into the page and CI checks again, on Ubuntu and on macOS — so nothing here is quoted from perldoc and hoped for.
Perl 1.0 came out in December 1987 as a better way to pull reports out of text files, and it is still the tool with the shortest distance between "this file is wrong" and "this file is fixed": a one-liner. Its Unicode support arrived later, in layers — usable in 5.8 (2002), consistent only from 5.12 (2010) when asked for, with fc in 5.16 (2012) — and every layer kept the old behaviour as the default. Most of what surprises people about Perl and text follows from that: the correct answer is almost always available, and almost never the one you get by typing the least.
Start here¶
00 — Start here — what this library assumes, how a page works, and the five pages to read first.
The chapters¶
| Chapter | What it covers | |
|---|---|---|
| 01 | One-liners | -n, -p, -l, -a, -F, -i and -0 as the lines of code they are — and the loop they build |
| 02 | Unicode text | use utf8, "Wide character", decoding at the edges, what length counts, and fc |
| 03 | Regex | the Unicode bug, /g in two contexts, the counts s/// and tr/// return, and split |
| 04 | Records and fields | the \r that chomp leaves, CSV, sorting, and numbers that arrive as text |
| 05 | Resources | the perldoc pages behind each chapter, and the sibling libraries |
Running the examples¶
You need perl 5.36 or later and nothing else — no CPAN modules, no build tool. On a Mac that means Homebrew's perl: /usr/bin/perl is 5.34, one release too old for use v5.36. Every example is a single file, run from its own folder:
To run all of them and check every recorded output:
Written against perl 5.42, which CI pins on both machines. Where a fact depends on the version, the page says which version.
Sibling libraries¶
Same house style, same answer-key contract. Each page here links the page in these that teaches the same idea in another language:
- Encodings ↗ — bits, bytes, code points and UTF-8 from the bottom up. Read it first if "code point" is a new word; this library assumes it.
- Python ↗ —
strandbytes: the same boundary, drawn as two types that will not mix. - Java text ↗ — a language that copied Perl's
splitand chose UTF-16. - Ruby text ↗ — the language that kept Perl's
-n,-p,-a,-F,-i,splitandpack, and labels every string with an encoding where Perl keeps one flag. - C ↗ and Rust ↗ — text with no encoding in the type at all, and text with the encoding enforced by the compiler.
The one rule¶
No page hand-types what a program prints. A lesson marks the spot and the runner fills it from a real run — so an example that changes behaviour on a new perl breaks the build instead of quietly making a page wrong. See CONTRIBUTING.md.