Tuesday, July 28, 2009

Perl Switches( CLO's)

>>perl -h for getting the help on command line options.
>>perl -v for getting the version of installed perl.

Usage
c:\>perl [switches] [Perl programfile] [Input arguments]
Switches to Perl Interpreter:-
Some important ones:
-w : Turn on many useful warnings( Use always, good for beginners)(RECOMMENDED)
-W : enable all warnings
-c : check syntax without actually executing the perl script.
-d : run script using perl debugger, to debug your script.

Some extra Switches:
-a : autosplit mode with -n or -p (splits $_ into @F)
-e : one line of program (several -e's allowed, omit programfile)
-E : like -e, but enables all optional features
-F/pattern/ : split() pattern for -a switch (//'s are optional)
-n : assume "while (<>) { ... }" loop around program
-p : assume loop like -n but print line also.
-P : run program through C preprocessor before compilation
-X : disable all warnings

Perl One Liners
>> perl -e [any perl instruction(statement) ]

*********************************************************************************
Examples:-
C:\> perl -e "print 'Hello World';" -e "print 'Howzit goin?';"
C:\> Hello WorldHowzit goin?

C:\> perl -n -e 's/^\s+//g; print $_;' file1
Above code is equivalent to

while(<>) {
s/^\s+//g;
print $_;
}


*************************************************
C:\>perl -p -e 's/\r//g' file1 > file1
above usage is a wrong way to write into file which is opened in reading mode, correct way will be as following:

C:\>perl -p -e 's/\r//g' file1 > file2
C:\>rename file2 file1
*************************************************
Note: Multiple -e statements can be used

No comments:

Post a Comment