Perl One Liners

To use a module from command line:

perl -MLWP::Simple -e 'print head "http://www.example.com"'

The -M switch is the same as 'use module'. If the module has default import that you don't want imported, the you can use -m switch (equivalent to 'use module ()') which turn off any default import. Any arguments you would normally pass to the use statement can be listed following an = sign:

perl -MCGI=:standard -e 'print header'

This command imports the ":standard" export set from CGI.pm and therefore the header function becomes available to your program. Multiple arguments can be listed using quotes and commas as separators:

perl -MCGI='header,start_html' -e 'print header, start_html'

To see if URI.pm is installed:

perl -MURI -e 1

Print the version number of a module:

perl -MDBD::mysql -e 'print $DBD::mysql::VERSION'
perl -MDBI -e 'print $DBI::VERSION'

To find which version of Perl is install:

perl -V

Find where URI.pm is on the system:

perldoc -l URI

To display the content of a file:

perldoc -F filename

To display the documentation for open():

perldoc -f open

Search the perlfag:

perldoc -q shuffle

To view the source of a module:

perldoc -m Test::MockObject

Print lines of the selected files between the START and END markers, without printing the markers themselves:

perl -ne 'print if s/^START\n$// .. s/^END\n$//' files...

Print lines following (and including) a line that matches a pattern:

perl -ne '$i = 1 if /pattern/; print if $i--' files

Replace Windows line terminators with Unix line terminators:

perl -i -pe 's/\r\n/\n/g' /usr/share/vim/vim63/ftplugin/pdoc4vim.vim

Install CPAN modules:

cpan install URI
perl -MCPAN -e 'install URI'

Also look at:

perldoc perldoc
perldoc perl
perldoc perlstyle

Search filename for all instance of sysread and replace them with read:

perl -p -i -e 's/sysread/read/g' filename

Execute perl code:

perl -e 'print "Hello World\n"'

Perl switches:

-e: indicates that the entire program is provided right there on the command line
-n: add a loop around your -e code.
-i: cause any changes to be written back to the file when done

http://www.perl.com/pub/a/2004/08/09/commandline.html

page_revision: 6, last_edited: 1219392628|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License