1. Command-line Perl
Command-line Perl
(Part 1)
or: Perl one-liners saved my career
or: `perldoc perlrun` brought to life
Perl code on the command line
Look Ma, no script!
_
$ perl print
# Can't open perl script quot;printquot;: No such file or directory
-e (The `e` is for hysterical raisins)
_
$ perl -e print
# (No output)
Quoting on the command line
...and you can quote me on that
_
$ perl -e ' print quot;Hellonquot;; ' # Unix
$ perl -e quot; print 'Hello' . $/; quot; # Win32
$ perl -e quot; print qq'Hellon'; quot; # Win32; sick
1 of 4
2. Command-line Perl
$ perl -e quot; print qq{Hellon}; quot; # Win32; better
Automatic end-of-line handling
# Calculator
$ perl -e ' print 42 * 42 '
1764$ perl -e ' print 42 * 42 * 42 '
74088$
The -l flag adds quot;nquot; to `print`
$ perl -le ' print 42 * 42 '
1764
$ perl -le ' print 42 * 42 * 42 '
74088
Defaults
@ARGV is the array of command-line arguments
shift takes @ARGV as its default
_
$ perl -le ' print @ARGV; $z=shift; print $z; ' foo bar baz
# foobarbaz
# foo
print takes $_ as default, and for sets $_ as default.
_
$ perl -le ' print for @ARGV ' foo bar baz
# foo
# bar
# baz
2 of 4
3. Command-line Perl
Warnings
Double your money!
$ perl -l -e ' $zz = shift; print $z . $z ' 15
# (No output)
Always pay for the cheap insurance
$ perl -wle ' $zz = shift; print $z . $z ' 15
# Name quot;main::zzquot; used only once: possible typo at -e line 1.
# Use of uninitialized value in concatenation (.) or string at -e line 1.
# Use of uninitialized value in concatenation (.) or string at -e line 1.
Corrected
$ perl -wle ' $z = shift; print $z . $z ' 15
# 1515
Command-line Modules
-M means `use`
_
$ perl -MData::Dumper -wle 1
# (No output)
$ perl -MData::Dumper::Dear::John -wle 1
# Can't locate Data/Dumper/Dear/John.pm in @INC (@INC contains ...
$FOO::VERSION is a CPAN standard
_
$ perl -MData::Dumper -wle 'print $Data::Dumper::VERSION'
# 2.121_02
3 of 4