perl
General-purpose scripting language, historically a sysadmin staple for text processing
By CMD Script Team · 4 min read · Last updated
perl [OPTIONS] [SCRIPT.pl | -e 'CODE'] [ARGS...]Options
| Flag | Description |
|---|---|
-e CODE | Execute the given code directly instead of reading a script file |
-p | Wrap the program in a loop that reads each input line, runs the code, and prints the line (auto-print) |
-n | Wrap the program in a loop that reads each input line and runs the code, without auto-printing |
-i | Edit files in place; combine with a suffix (e.g. -i.bak) to keep a backup |
-w | Enable warnings about questionable constructs |
-l | Automatically handle line endings: strip on input, add on output |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
perl is a general-purpose scripting language with deep, built-in support for regular
expressions and text manipulation, which made it the dominant sysadmin and CGI scripting
tool through the 1990s and 2000s. Beyond running full .pl scripts, perl shines as a
command-line one-liner tool: flags like -p, -n, and -e turn it into a powerful
stream editor that can outclass sed and awk for anything involving non-trivial
regular expressions.
Beginner examples
perl -e 'print "hello world\n"'— run a quick inline scriptperl script.pl— run a Perl script fileperl -pe 's/foo/bar/'— substitute the first match offoowithbaron each line of stdinperl -ne 'print if /pattern/' file.txt— print only lines matching a pattern
perl -pe 's/\s+$//' file.txt
Advanced examples
- Replace text across a file and save the result back in place, keeping a backup:
perl -i.bak -pe 's/http:/https:/g' config.ini - Process comma-separated fields like
awkdoes, using auto-split mode:perl -F, -ane 'print "$F[0]\n"' data.csv - Count matching lines without loading extra tools:
perl -ne '$c++ if /ERROR/; END {print "$c\n"}' app.log - Strip trailing whitespace from every line of a file in place:
perl -i -pe 's/\s+$//' file.txt - Use
-lto handle line endings automatically while building output line by line:perl -lne 'print uc' file.txt
perl -i -pe 's/DEBUG/INFO/g' app.log
Common mistakes
- Forgetting
-iperforms an irreversible in-place edit unless you also give it a backup suffix (-i.bak) — always test the transformation without-ifirst. - Mixing up
-p(auto-print every line) and-n(no auto-print, print explicitly) and getting either duplicated or missing output. - Writing complex, deeply nested one-liners that become unreadable — past a certain
complexity, a proper
.plscript file is more maintainable than a giant-estring. - Assuming Perl regex syntax is identical to
sed's orgrep's POSIX regex — Perl's regex engine (PCRE-style) supports more features and slightly different syntax.
Tips
- Always dry-run a
perl -pesubstitution without-ifirst to confirm the output looks right before overwriting files. -i.bakis cheap insurance — it costs one extra flag and saves you from a bad regex clobbering a file with no way back.- Combine
-a(auto-split each line into the@Farray) with-Fto set a custom delimiter for quick field-based processing, similar toawk.
Best practices
- For simple, well-defined substitutions, prefer
sedfor its ubiquity and simplicity; reach forperl -pewhen the pattern needs Perl-flavored regex features like lookaheads or non-greedy matching thatsedcan't express. - Always test in-place edits (
-i) on a copy or with a backup suffix first, especially in scripts that run unattended against production files. - For anything beyond a short one-liner, write a proper
.plfile withuse strict;anduse warnings;rather than an unwieldy multi-clause-estring.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Bulk find-and-replace across configuration files during a migration, e.g. rewriting
http://tohttps://in place. - Parsing and summarizing log files with regex conditions too complex for
grepalone. - Legacy sysadmin and bioinformatics pipelines that still rely on Perl one-liners baked into existing automation.
Common interview questions
- What does
perl -pedo? It wraps the given code in an implicit per-line loop that reads each input line, executes the code, and automatically prints the resulting line — a common pattern for one-liner text substitution. - How do you edit a file in place with perl while keeping a backup?
perl -i.bak -pe 's/old/new/g' file.txt—-i.baksaves the original asfile.txt.bakbefore overwritingfile.txt. - When would you choose perl over sed or awk for text processing? When the
transformation needs more expressive regular expressions (lookaheads, non-greedy
matches) or general-purpose scripting logic that goes beyond what
sed's substitution model orawk's field-based model can cleanly express.
Frequently Asked Questions
What does perl -pe do?
-p wraps the script in an implicit loop that reads each input line, runs your code against it, and automatically prints the (possibly modified) line — making perl -pe a fast way to write a one-liner stream editor similar in spirit to sed.
What's the difference between perl -p and perl -n?
-p auto-prints each line after running the code against it; -n does not auto-print, so you must explicitly print anything you want in the output — useful when you only want to print matching or transformed lines, not every line.
How do I edit a file in place with perl?
Use perl -i -pe 's/old/new/g' file.txt to substitute text and save the result back to the same file. Add a suffix like -i.bak to keep a backup of the original before overwriting.
Is perl still relevant compared to Python for scripting?
Perl's usage has declined relative to Python for general scripting, but it remains common in legacy sysadmin scripts, bioinformatics pipelines, and anywhere its terse regex-and-text-processing one-liners are already deeply embedded in existing tooling.
Cheat sheet
Download a quick-reference cheat sheet for perl.