[ Perl tips index ]
[ Subscribe to Perl tips ]
Our last tip discussed
many of the common command line switches in Perl: -e, -E, -p,
-n, and -M. In this tip we'll discuss some of the other switches you
can use.
Perl can be used to edit a file ``in place''. This means you don't need to handle making your changes to a temporary file and then renaming it.
perl -i -pe 's/freind/friend/' file
perl -i.old -pe 's/freind/friend/' file
Using -i on its own edits the file in place, overwriting
the original version. This can be dangerous, as a bug in your program
can result in data-loss, and if your program terminates unexpectedly
your file can be left in an inconsistent state.
A better solution is to provide an argument to the switch:
-i.old. This creates a backup copy of the original file file.old
and then overwrites the original upon successful completion of your program.
This is equivalent to:
mv file file.old
perl -pe 's/freind/friend/' file.old > file
If the backup file contains an asterisk, then it is replaced with the current filename. This allows you to add a prefix instead of a suffix if needed. For example:
perl -i'badly_spelled_*' -e's/freind/friend/' file
would create a backup called badly_spelled_file. You can get fancy and place the asterisk in the middle of the backup name, or even have multiple asterisks if you prefer.
If your operating system or file-system does not allow an opened file
to be removed, then you must specify a backup extension when using
-i. In particular, Windows systems always require an extension.
-a is Perl's autosplit switch. When using autosplit (with -n or -p),
Perl automatically does a split on whitespace and assigns the result to the
@F variable.
Let's say that we want to parse the output of ls -l from a Unix
system. It consists of a series of lines in the following format:
-rw-r--r-- 1 pjf pjf 10201 Jul 17 13:52 command.pod
-rw-r--r-- 1 pjf pjf 17739 Jul 17 15:51 command.sgml
-rw-r--r-- 1 pjf pjf 1320760 Jul 18 14:57 sysadmin.ps
-rw-r--r-- 1 pjf pjf 2010 Jul 14 17:31 sysadmin.sgml
If we want to print all lines which have a file-size greater than 1MB we could use:
ls -l | perl -ane 'print if $F[4] > 1_000_000;'
Note that Perl always counts fields starting from zero. The above code run over our sample input would display the single line:
-rw-r--r-- 1 pjf pjf 1320760 Jul 18 14:57 sysadmin.ps
The above Perl code is equivalent to:
while (<>) {
our @F = split(" ", $_, 0);
print if $F[4] > 1_000_000;
}
Passing 0 as a final argument to split means that empty fields are
simply discarded; the effect of this is that any sequence of space
characters is considered a separator. The -F switch can also be used to
specify an alternative pattern on which to split.
Parsing the results of ls -l to get file information is not
a recommended way to gain information about files. It's both
slow and prone to error. A better way is to use glob and Perl's
in-built stat function, or file test operators.
You might use an example similar to the above if you were parsing the
output of ls that has been stored in a file.
Perl has many other switches. Below are some others you are probably familiar with.
perl -c program.pl
-c causes Perl to check the program for syntactic errors and to exit
without executing the main body of code. Code in BEGIN and CHECK
blocks, as well as use lines will still be executed.
perl -w program.pl
The -w switch runs your program with warnings turned on. Running
with warnings helps catch common mistakes, and is highly recommended.
perl -T program.pl
Turns on taint mode. Any input from outside the program must be cleaned before being used to cause effects outside the program. For example data received from a user must be cleaned before being passed as an argument to a system call.
We cover taint in another tip and also in our Perl Security course notes.
perl -d program.pl
Runs the program under the Perl debugger. You can learn more about the
Perl debugger by using perldoc perldebug
perl -I/home/pjf/perl/lib/ program.pl
Specifies which additional directories should be searched when looking for
modules. This modifies Perl's special @INC variable. For full programs
you should use the lib pragma instead.
For more information on these switches read perldoc perlrun.
[ Perl tips index ]
[ Subscribe to Perl tips ]
This Perl tip and associated text is copyright Perl Training Australia. You may freely distribute this text so long as it is distributed in full with this Copyright noticed attached.
If you have any questions please don't hesitate to contact us:
| Email: | contact@perltraining.com.au |
| Phone: | 03 9354 6001 (Australia) |
| International: | +61 3 9354 6001 |
Copyright 2001-2012 Perl Training Australia. Contact us at contact@perltraining.com.au