Tutorial by Examples

Simple one-liners may be specified as command line arguments to perl using the -e switch (think "execute"): perl -e'print "Hello, World!\n"' Due to Windows quoting rules you can't use single-quoted strings but have to use one of these variants: perl -e"print qq(Hello, W...
Windows uses only double quotes to wrap command line parameters. In order to use double quotes in perl one-liner (i.e. to print a string with an interpolated variable), you have to escape them with backslashes: perl -e "my $greeting = 'Hello'; print \"$greeting, world!\n\"" T...
perl -ne'print if /foo/' file.txt Case-insensitive: perl -ne'print if /foo/i' file.txt
perl -pe"s/foo/bar/g" file.txt Or in-place: perl -i -pe's/foo/bar/g' file.txt On Windows: perl -i.bak -pe"s/foo/bar/g" file.txt
perl -lane'print "$F[0] $F[-1]"' data.txt # prints the first and the last fields of a space delimited record CSV example: perl -F, -lane'print "$F[0] $F[-1]"' data.csv
perl -ne'print if 5..10' file.txt
Without a backup copy (not supported on Windows) perl -i -pe's/foo/bar/g' file.txt With a backup copy file.txt.bak perl -i.bak -pe's/foo/bar/g' file.txt With a backup copy old_file.txt.orig in the backup subdirectory (provided the latter exists): perl -i'backup/old_*.orig' -pe's/foo/bar/g' ...
perl -0777 -ne'print "The whole file as a string: --->$_<---\n"' Note: The -0777 is just a convention. Any -0400 and above would de the same.
perl -Mojo -E 'p("http://localhost:3000" => form => {Input_Type => "XML", Input_File => {file => "d:/xml/test.xml"}})' File d:/xml/test.xml will be uploaded to server which listen connections on localhost:3000 (Source) In this example: -Mmodule execu...

Page 1 of 1