One-liners¶
One line: A Perl one-liner is a program with a loop you did not write — -n and -p supply the loop, -l, -a, -F, -i and -0 each add a line to it, and perl -MO=Deparse prints the whole program back so you can read what you actually ran.
This is where Perl earns its place beside grep, sed and awk: the same one-line shape, with a whole language in the middle, and the same behaviour on macOS and Linux. Every lesson here runs its one-liners over a small file in its own demo/ folder, so any command on the page can be pasted into a terminal in that folder and give the same output.
| Lesson | Level | What it settles |
|---|---|---|
-n and -p are a loop |
101 | The while loop the two switches write for you, why next inside -p skips nothing, and why $. keeps counting into the next file |
-l, -a and -F take a line apart |
101 | @F, the special ' ' pattern, the newline left on the last field, and a tab that is not a space |
-i edits in place |
201 | What gets written back with -p, with -n, and with a print to anywhere else — and where the backup goes |
| Records are not always lines | 201 | $/: paragraph mode, the whole file as one record, fixed-size blocks, and NUL-separated names |
Where this connects¶
- The encodings library's tools chapter ↗ measures the programs a one-liner replaces on text that is not ASCII:
grep↗,sed↗,awk↗,cut↗ andtrandsort↗. - These lessons read the input as bytes, which is what a one-liner does unless told otherwise. What changes when the input is decoded —
-CSD,-Mutf8— is chapter 02.