>_cmd.script

perl

General-purpose scripting language, historically a sysadmin staple for text processing

Development

By CMD Script Team · 4 min read · Last updated

SYNTAX
perl [OPTIONS] [SCRIPT.pl | -e 'CODE'] [ARGS...]

Options

Command options and flags
FlagDescription
-e CODEExecute the given code directly instead of reading a script file
-pWrap the program in a loop that reads each input line, runs the code, and prints the line (auto-print)
-nWrap the program in a loop that reads each input line and runs the code, without auto-printing
-iEdit files in place; combine with a suffix (e.g. -i.bak) to keep a backup
-wEnable warnings about questionable constructs
-lAutomatically 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 script
  • perl script.pl — run a Perl script file
  • perl -pe 's/foo/bar/' — substitute the first match of foo with bar on each line of stdin
  • perl -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 awk does, 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 -l to 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 -i performs an irreversible in-place edit unless you also give it a backup suffix (-i.bak) — always test the transformation without -i first.
  • 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 .pl script file is more maintainable than a giant -e string.
  • Assuming Perl regex syntax is identical to sed's or grep's POSIX regex — Perl's regex engine (PCRE-style) supports more features and slightly different syntax.

Tips

  • Always dry-run a perl -pe substitution without -i first to confirm the output looks right before overwriting files.
  • -i.bak is 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 @F array) with -F to set a custom delimiter for quick field-based processing, similar to awk.

Best practices

  • For simple, well-defined substitutions, prefer sed for its ubiquity and simplicity; reach for perl -pe when the pattern needs Perl-flavored regex features like lookaheads or non-greedy matching that sed can'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 .pl file with use strict; and use warnings; rather than an unwieldy multi-clause -e string.

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:// to https:// in place.
  • Parsing and summarizing log files with regex conditions too complex for grep alone.
  • Legacy sysadmin and bioinformatics pipelines that still rely on Perl one-liners baked into existing automation.

Common interview questions

  • What does perl -pe do? 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.bak saves the original as file.txt.bak before overwriting file.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 or awk'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.