Perl In The Command Line - Presentation Transcript
Perl in the Command Line
-c: Test Compiling
-c : causes Perl to check the syntax of the program and then exit without executing it.
It will execute BEGIN , UNITCHECK , CHECK , and use blocks, because these are considered as occurring outside the execution of your program.
It will not execute INIT and END blocks.
-w, -W and -X: Warnings
-w : This turns on warnings that Perl will then give you if it finds any of a number of problems in your code. The -w option has been replaced by the use warnings pragma.
-W: Enables all warnings regardless of no warnings or $^W .
-X: Disables all warnings regardless of use warnings or $^W .
$> perldoc perllexwarn
-d: Debugging
-d : This option puts you into the Perl debugger.
-d:DProf : executes the program using the Devel::DProf profiler, combined with dprofpp may help you to find where is your program slow.
$> perldoc perldebtut $> perldoc perldebug
-e: eval
-e: allows you to define code to be executed by the compiler.
$> perl -e 'print "Hello World"' "Hello World" program in the shell.
-E : like -e, but enables all optional features
-M: module Import
-M : This option imports a module. -Mmodule is the same has use module .
$> perl –Mdiagnostics script.pl Get better error message.
perl -MLWP::Simple -e 'print get($ARGV[0]), "
"' http://perl.com Better as an alias.
-n: Looping
-n : Perl assumes the following loop around your program: LINE: while (<>) { # Your code }
-n: Looping
The command $> perl -ne 'print "$. - $_"' in.txt Would become LINE: while (<>) { print "$. - $_" } This code prints each line of the file together with the current line number.
-p: Looping with print
-p : Perl assumes the following loop around your program: LINE: while (<>) { # your code goes here } continue { print or die "-p destination: $!
"; } This uses a continue block on a while loop to ensure that the print statement is always called.
-p: Looping with print
The -n example $> perl -ne 's/PHP/Perl/g; print' in.txt
or the simpler $> perl -pe ' s/PHP/Perl/g ' in.txt
It's very common to see command line programs that use Unix I/O redirection like this: $> perl -pe 's/PHP/Perl/g' in.txt > out.txt
-i: edit in place
$> perl -i -pe 's/PHP/Perl/g' in.txt Substitute every PHP occurence by Perl
-i[extension] : edit in place. Case extension exists the original file is renamed, otherwise the file is overwitten.
If the extension doesn't contain a * , then it is appended to the end of the current filename. If the extension does contain one or more * characters, then each * is replaced with the current filename.
-l: Line-ending processing
-l[octnum] : Automatic line-ending processing.
It assigns $ to have the value of octnum . Without octnum, $ will be assigned to $/ .
After
chomp() everything on input
Adds $ (the output record separator) to each print
-a and -F: Autosplitting
-a: Splits the $_ whith a space while (<>) { @F = split(' '); # your code }
-F: choses a diferent separator
perl -F, -ane 'print join " ", @F' data.csv converts a csv into a tsv. Not really true but enough for example propose.
0 comments
Post a comment