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
Comments on this entry are closed.
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.
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.
@Clyde, Felix,
Thanks for pointing out the mistake. It’s corrected now.
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.
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}\””
I need perl command explaination.
what is the purpose of using it.