≡ Menu

8 Awesome Perl Command Line Arguments You Should Know

Command line options in Perl could be useful to do smaller tasks effectively.

In this article, let us review how to use Perl command line options to do the following tasks:

  • Edit file content
  • Handle line separator
  • Check syntax errors
  • Load modules
  • Perform looping
  • Execute perl code
  • Set input line separator
  • Split the input line
  • etc.,

1. Perl -i Option: Edit file contents

It opens files one by one and replaces the content with STDOUT.

Let us use the following sample.txt file to convert it’s content to upper case as shown below.

$ cat sample.txt
Practical Extraction Report Language

$ perl -pi  -e “tr/[a-z]/[A-Z]/” sample.txt

$ cat sample.txt
PRACTICAL EXTRACTION REPORT LANGUAGE

You can also backup the original file as shown below:

$ perl -pi.bak  -e “tr/[a-z]/[A-Z]/” sample.txt

$ cat sample.txt
PRACTICAL EXTRACTION REPORT LANGUAGE

$ cat sample.txt.bak
Practical Extraction Report Language

To debug a Perl program, use Perl debugger as we explained earlier.

2. Perl -l Option: Line separator handling

The line separator takes the octal value as the argument. Following are few examples of some common separators and it’s octal value.

Character    Octal Value
=========    =========
new line(\n) 012
tab(\t)      011
space        040
&            046

Note : use “man ascii” command to know the octal value for all characters.

Example usage of line separator:

$ perl -p -l046 -e “tr/[a-z]/[A-Z]/”;
bala

Output :

BALA&

Also, refer to 6 Perl eval functions for additional reading.

3. Perl -c option: Check Syntax Errors

Option -c compiles your program without running it to ensure no syntax errors are present in your program.

$ perl -c sample.pl

4. Perl -M option : For Loading Modules

With the Perl -M option, we can load the desired modules as shown below.

$ perl -MFile::Copy -e ‘move(“sample.txt”,”/tmp”);’

-m option also does the modules loading but it does not import anything. Following is the difference:

  • -MFile::Copy equals to “use File::Copy; “
  • -mFile::Copy equals to “use File::Copy()“

We also discussed about Option -M in our earlier 20 Killer Perl Programming Tips.

5. Perl -n and -p Option: Implicit Looping

Option -n wraps your code inside the loop as shown below.

while(<>) {
# perl code
}

Following snippet describes this better.

while(<>) {
$_ =~ tr/[a-z]/[A-Z]/;
}

By using -n, we can rewrite the above code snippet as shown below.

$ perl -n -e ‘tr/[a-z]/[A-Z]/;print’

Option -p wraps your code inside the loop as shown below.

while(<>) {
# perl code
print;
}

Following snippet describes this better.

while(<>) {
$_ =~ tr/[a-z]/[A-Z]/;
print;
}

By using -p, we can rewrite the above code snippet as shown below.

$ perl -p -e “tr/[a-z]/[A-Z]/”;

If you develop lot of Perl code, you should read Perl best practices book.

6. Perl -e option: Execute perl code in command line itself

Simple or short Perl program can be written in the command line itself with this option as shown below.

$ perl -e “print \”Username : $ENV{USER}”

7. Perl -0 Option: Input Record Separator

Using option -0, we can change the “input record separator” from newline to something else as shown below.

$ perl -p -0046 -e “tr/[a-z]/[A-Z]/”;
bala
raja&

Output :

BALA
RAJA&

In the above example, 046 is the octal value for ‘&’ character. So, after it receives & character, Perl consider it as the end of a record and then does the translation operation.

8. Perl -a Option: Split the input line

It splits the $_ into @F with the delimiter as space.

Following example demonstrates option -a.

$ cat emp_salary.txt
bala     10000
rajesh  12300
kumar 14000

$ perl -n -l012  -a -e ‘print “$F[1]“‘  emp_salary.txt
10000
12300
14000

By default, it takes space as an delimiter and does the split operation. The delimiter can be changed by using -F option as shown below.

$ cat emp_salary.txt
bala:10000
rajesh:12300
kumar:14000

$ perl -n -l012  -F: -a -e ‘print “$F[1]“‘  emp_salary
10000
12300
14000
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Clyde Ingram June 10, 2010, 7:16 am

    I am very impressed by “3. Perl -c option: Correct Syntax Errors”. I now use perl -c to automatically correct all my Perl code. No matter how many errors I have stuffed into my programs, it is reassuring to know that simple invoking “perl -c” magically corrects the lot. Effortless. Zero time spent debugging.

  • Felix Frank June 14, 2010, 5:34 am

    Clyde,

    so you identified and sarcastically pointed out a faulty section title, erroneous probably due to the author’s relative unfamiliarity with the English language. I acknowledge both your wisdom and wit and applaud you for clearly being both the superior developer/unix user and greater man.

  • Ramesh Natarajan June 14, 2010, 9:46 am

    @Clyde, Felix,

    Thanks for pointing out the mistake. It’s corrected now.

  • tsabi November 1, 2011, 1:50 am

    Hi Ramesh

    #6 doesn’t work for me like that.

    Did you want to write
    perl -e ‘ print “Username : $ENV{USER}\n” ‘
    instead?
    I mean single quotes before and after the command, and double quotes round the print parameters.

  • Bryan Moore April 18, 2014, 3:08 pm

    Tsabi, I got it to work with double quote, but copy-paste didn’t quite work. I had to add an escaped closing double quote and escaped quote:

    perl -e “print \”Username : $ENV{USERNAME}\””

  • revathy February 13, 2017, 4:50 am

    I need perl command explaination.
    what is the purpose of using it.