--- name: one-liners guide --- # The Definitive Perl One-Liner Guide A Perl one-liner is a complete program delivered as an argument to `pperl -e` (or `-E`), run directly from the shell. It occupies the same niche as `awk`, `sed`, and ad-hoc shell pipelines, with a richer regex engine, a full standard library, and the ability to grow into a real script the moment one line stops being enough. This guide is for a Linux shell user who already composes `grep`/`sed`/`awk`/`xargs` pipelines and has written a small Perl script or two. It teaches the command-line surface end to end: the switches, a progressive recipe catalogue, regex-driven transformations, numeric work, how to wrap recipes into reusable shell aliases, and the traps that cost time. The binary is `pperl`. Every example in this guide is copy-pasteable into a Linux shell. pperl is a Rust reimplementation of Perl 5; its command-line interface matches Perl 5.42. ## Which chapter do I want? | You want to … | Start here | |----------------------------------------------------------------|--------------------------------------| | Know what each switch (`-n`, `-p`, `-a`, …) does | [switches](switches) | | Browse recipes from trivial substitutions up to data reductions | [progression](progression) | | Match, extract, or rewrite text with regexps | [regex-recipes](regex-recipes) | | Sum, average, generate primes, convert IPs, do date arithmetic | [numeric](numeric) | | Turn a one-liner into a reusable shell function or alias | [aliases](aliases) | | Debug quoting, encoding, in-place edits, and record-separator surprises | [gotchas](gotchas) | ## The three layers of this guide - **Orientation** — this page. - **Working programmer** — the six chapters above. Each stands on its own; skim the ones relevant to your current problem. - **Deep reference** — the compact switch/variable table at the end of [switches](reference-table). ## Two examples before you start A sed/awk user reading this guide wants to see what a one-liner buys them. Two demonstrations. ### Sum the numbers in the last column of a whitespace-separated file ```bash pperl -lane '$s += $F[-1]; END { print $s }' data.txt ``` `-l` chomps input newlines and appends one on output. `-a` splits each line into `@F`. `-n` wraps the program in an implicit read loop. `$F[-1]` is the last column. `END { … }` runs once after input is exhausted. No temporary file, no `awk 'BEGIN{…}END{…}'`, no boilerplate. ### Extract every unique IPv4 address from a log, newest first ```bash pperl -lne 'print for /\b(?:\d{1,3}\.){3}\d{1,3}\b/g' access.log \ | awk '!seen[$0]++' ``` The one-liner handles the hard part (finding all matches per line, not just the first), the awk keeps the first sighting of each. Written as two short tools, each doing one job — that is the working mode of this guide. ## Read in this order on a first pass 1. [switches](switches) — the vocabulary. 2. [progression](progression) — the recipes, ordered by difficulty. 3. [regex-recipes](regex-recipes), [numeric](numeric) — applied pieces. 4. [aliases](aliases) — lifting what you use daily into shell functions. 5. [gotchas](gotchas) — the traps, once you are running into them. On later passes, go directly to whichever chapter names the problem you are holding. ## Bibliography This guide is not derived from any single book. It was researched the way a technical writer would research the topic — perldoc, CPAN module documentation, conference talks, blog posts, mailing-list archives, and the books listed below — and then distilled in several passes: removing references to tools and versions nobody runs anymore, collapsing the same advice repeated by different authors into one statement, and merging multiple partial treatments of one topic into a single complete treatment. The result covers more ground than any single source we consulted, because no single source covers the whole field. It is also more densely cross-linked than any of them: when the regex chapter touches something the regex guide already explains in depth, that is one click away — not a chapter you have to find by yourself in another volume. The two books below are the closest published companions in print form. Buy them if the one-liner idiom is a daily tool for you and you want a printed reference on the shelf. - Peteris Krumins, *Perl One-Liners: 130 Programs That Get Things Done*, No Starch Press, 2014. - Sundeep Agarwal, *Perl One-Liners Guide* (learnbyexample), 2023. ```{toctree} :maxdepth: 1 :hidden: switches progression regex-recipes numeric aliases gotchas ```