next up previous
Next: 7. Where to get Up: 6. What Perl looks Previous: 4. Modules

5. Command line invocation

You can also invoke Perl from the command line in various ways:

perl -e 'print (2+2);'			# -e means execute a
					# statement; prints 4

perl -pe 's/foo/bar/g;'			# -p means print the result; 
					# this loops through a file 
					# and prints the result

perl -pi.bak -e 's/foo/bar/g;'		# -i specifies the file extension 
					# to use when making a backup

This is how obfuscated Perl one-liners are born, but more importantly, it allows you to easily use Perl for quick modifications to files or to easily perform any Perl function from the command line. For instance, this prints out all text files in the current directory:

perl -e 'foreach (<*>) { print "$_\n" if -T $_; }'

Kirrily Robert