Perl Command Line Tutorial - Run, Debug, and Manipulate Perl Code

Learn how to effectively use the Perl command line. This tutorial covers running, debugging, manipulating, and optimizing Perl code with various command-line options. Master essential Perl commands for efficient development.

Perl Command Line

This tutorial demonstrates various Perl command-line options for running, debugging, and manipulating Perl code. Refer to perldoc.perl.org/perlrun for comprehensive documentation.

Basic Usage

Viewing Perl Version

perl -v
perl -V

Running a Perl Program

perl <program> [args]

Syntax Checking

perl -cw <program>

Advanced Usage

Enabling Warnings

perl -W <program>

Modifying Module Search Path

perl -I <path1> <program> [args]

Debugging with the Perl Debugger

perl -d <program>

One-liners with -e

perl -e 'print "Hello World!\n"'

Unicode Support

perl -C -e <program_text>

Using -E for New Features

perl -E 'say "Hello World!"'

Using Modules with -M

perl -M<module>[=import,list] -E <program_text>

Compiling and Decompiling with B::Deparse

perl -MO=Deparse -E <program_text>

Line-by-Line Processing

perl -ne <program_text> [files]
perl -pe <program_text> [files]

Reading Entire Files

perl -0777 -ne <program_text> [files]
perl -0777 -pe <program_text> [files]
perl -g -pe <program_text> [files]

Working with Arrays (-a)

perl -ane  <program_text>
perl -ae  <program_text>
perl -aF<separator> -e <program_text>

In-place Editing (-p, -i)

perl -pe <program_text> [files]
perl -pie <program_text> [files]
perl -pi.bak -e <program_text> [files]

String Manipulation Examples

echo -e "foo\nbar\nbaz" | perl -pe 's/\n/\\n/g;'
cat test.txt | perl -0pe "s/test1\ntest2/test1 test2/m"
perl -pe '$/=""; s/(\n)+/$1/' my-file