Skip to content

Perl Command Line Guide — One-Liners and CLI Scripting Techniques

DodaTech Updated 2026-06-28 2 min read

In this tutorial, you will learn about Perl Command Line Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Perl command-line flags enable powerful one-liners for text processing -- -e for inline code, -p for automatic print loop, -n for non-printing loop, -a for auto-split into @F, -F to set field delimiter, and -i for in-place file editing.

Basic One-Liners

# Execute code
perl -e 'print "Hello, World!\n"'

# With strict and warnings
perl -Mstrict -Mwarnings -e 'my $x = 42; print $x'

# Print file lines
perl -pe '$_' file.txt

# Print lines containing pattern
perl -ne 'print if /error/' log.txt

In-Place Editing

# Backup and replace
perl -i.bak -pe 's/old/new/g' file.txt

# In-place without backup
perl -i -pe 's/\s+$//' *.txt  # trim trailing whitespace

# Multiple files
perl -i.bak -pe 's/copyright 2025/copyright 2026/g' *.html

Auto-Split Mode

# Print first column (default: split on whitespace)
perl -ane 'print "$F[0]\n"' data.txt

# Custom delimiter
perl -F: -ane 'print "$F[0]\n"' /etc/passwd

# CSV processing
perl -F, -ane 'print "$F[1]\n"' data.csv

Common Patterns

# Line count
perl -ne 'END { print "$.\n" }' file.txt

# Remove duplicate lines
perl -ne 'print unless $seen{$_}++' file.txt

# Sum a column
perl -ane '$sum += $F[1]; END { print "$sum\n" }' data.txt

# Extract emails
perl -ne 'print "$_\n" while /([\w.]+@\w+\.\w+)/g' file.txt

Common Mistakes

1. Single vs double quotes on Windows

On Windows, use double quotes: perl -e "print 'hello'". Single quotes don't protect $variables.

2. Forgetting -i creates backup

-i.bak creates .bak backups. Use -i alone for no backup (dangerous). Always test without -i first.

3. -a without -n or -p

-a (auto-split) requires -n or -p to create the implicit loop. Alone, it does nothing.

Practice Questions

1. What does -pe do? -p creates a print loop (prints $_ after each iteration). -e provides inline code.

2. How do you edit a file in-place with backup? perl -i.bak -pe 's/old/new/g' file.txt. Original is saved as file.txt.bak.

3. What does @F contain with -a? @F contains the fields of the current input line, split on whitespace (or the -F delimiter).

FAQ

{{< faq question="Can I run multi-line scripts from command line?" >}} Yes, separate with newlines or semicolons: perl -e 'my $x = 5; print $x'. {{< /faq >}}

{{< faq question="What does -MO=Deparse do?" >}} Shows how Perl compiles your code: perl -MO=Deparse -e 'print "hello"'. Useful for understanding optimization. {{< /faq >}}

{{< faq question="How do I use modules in one-liners?" >}} -M loads a module: perl -MData::Dumper -e 'print Dumper(\%ENV)'. {{< /faq >}}

What's Next

Now learn about debugging Perl code.

Topic Description Link
Debugging Perl debugging techniques {{< ref "23-debugging" >}}
Testing Perl testing with Test::More {{< ref "25-tests" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro