Perl on the command line

Besides writing Perl scripts in a file, we also can run Perl on the command line using one line. This will definitely help us use Perl in our daily Linux work.

 

Samples:

(luhuang) public_html- perl -v

This is perl, v5.6.1 built for i686-linux

Copyright 1987-2001, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl’ or `perldoc perl’. If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.

(luhuang) public_html-

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

(luhuang) public_html- set NAME=sysu
(luhuang) public_html- perl -e “print ‘I love $NAME ‘”
I love sysu

perl -n option: Implicit Looping. Option -n wraps the code inside the loop as shown below:

while(<>){

#perl code

}

(luhuang) public_html- cat sample.data
1 abc
2 def
3 ghi
4 gho
5 ghu
4 gho
6 tet
7 ;ls
8 jkl
(luhuang) public_html- perl -ne ‘print if 2..5’ sample.data
2 def
3 ghi
4 gho
5 ghu

perl -i Option: Edit file contents. It opens files one by one and replaces the content with STDOUT.

perl -p option: uses the same loops as -n but with a continue block so taht $_will be printed every time through the loop. If both -n and -p are specified on the command line, the -p option will take precedence.

while(<>){

#perl code

} continue {

print ;

}

perl -p -i -e ‘s/gho/change/g’ sample.data

(Note, if you use -n here, the sample.data will be empty at last. You can use .bak to backup it firstly)

perl -c option: Check Syntax Errors. You can use it compile the program without running it to ensure no syntax.

(luhuang) public_html- perl -c debug.pl
Name “main::DATA” used only once: possible typo at debug.pl line 3.
debug.pl syntax OK
(luhuang) public_html-

Appendix:

Usage: perl [switches] [–] [program file] [arguments]

-0[octal] specify record separator (, if no argument)
-a autosplit mode with -n or -p (splits $_ into @F)
-C enable native wide character system interfaces
-c check syntax only (runs BEGIN and CHECK blocks)
-d[:debugger] run program under debugger
-D[number/list] set debugging flags (argument is a bit mask or alphabets)
-e ‘command’ one line of program (several -e’s allowed, omit programfile)
-F/pattern/ split() pattern for -a switch (//’s are optional)
-i[extension] edit <> files in place (makes backup if extension supplied)

-Idirectory specify @INC/#include directory (several -I’s allowed)
-l[octal] enable line ending processing, specifies line terminator
-[mM][-]module execute `use/no module…’ before executing program
-n assume ‘while (<>) { … }’ loop around program
-p assume loop like -n but print line also, like sed
-P run program through C preprocessor before compilation
-s enable rudimentary parsing for switches after programfile
-S look for programfile using PATH environment variable
-T enable tainting checks
-t enable tainting warnings
-u dump core after parsing program
-U allow unsafe operations
-v print version, subversion (includes VERY IMPORTANT perl info)
-V[:variable] print configuration summary (or a single Config.pm variable)
-w enable many useful warnings (RECOMMENDED)

-W enable all warnings
-X disable all warnings
-x[directory] strip off text before #!perl line and perhaps cd to directory