Small codes of perl can be executed on command line with -e switch. The code is contained in single quotes. Following is an
example.
Command :
perl -e ' $\=" "; for (1 .. 9) { print; } '
Output :
1 2 3 4 5 6 7 8 9
Another frequently used switch is -n. This switch assumes 'while(<>){...}' around your code. Useful for processing all lines of the input coming through pipes. Example follows.
Command :
ls -l | perl -ne ' print if(/^d/); '
Here, output of ls -l (long listing) is piped to the perl command. -n makes the code loop over all the lines of the input that come from the previous command. The regular expression checks if the lines start with the letter d. Thus the code prints details of only the directories contained in the working directory. The command is equivalent to
ls -l | perl -e ' while(<>){ print if(/^d/); } '
(without -n switch)
-a is autosplit mode with -n or -p (splits $_ into @F ). See how it works
ls -l | perl -ane ' print $F[0]."\t".$F[7]."\n"; '
This prints the 0th and the 7th fields of the long directory listing (output of ls -l).
You can learn more about switches with this command
perl -h
To see version, built and other license related info,
perl -v
Thursday, April 27, 2006
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment