Introduction to RAG (Retrieval Augmented Generation) and its application
Yapc::NA::2009 - Command Line Perl
1. Command-line Perl 1
Command-line Perl
or: Perl one-liners saved my career
or: `perldoc perlrun` brought to life
or: Data Munging on PowerThirst
(YAPC::NA::2009)
Bruce Gray (Util)
Any sufficiently advanced technology...
... will fail when used for even more advanced tasks.
If you are viewing a printed version of this presentation, or seeing it on SlideShare, it probably looks awful.
Instead, please get a packaged copy of the HTML version by emailing me (bruce.gray@acm.org), or downloading from:
http://feather.perl6.nl/~util/
It should look great in Firefox, or even better in Opera's "Presentation Mode" (fullscreen).
Larry == Comedic One-liners
You want it in one line? Does it have to fit in 80 columns? :-)
--Larry Wall in <7349@jpl-devvax.JPL.NASA.GOV>
Perl code on the command line
Look Ma, no script!
$ perl print
Can't open perl script "print": No such file or directory
-e (The `e` is for hysterical raisins)
$ perl -e print
(No output)
Quoting on the command line
file://localhost/YAPC_Talk/index.html June 22, 2009 3:25:46 PM
2. Command-line Perl 2
Quoting on the command line
...and you can quote me on that
$ perl -e ' print "Hellon" ' # Unix
$ perl -e " print 'Hello' . $/ " # Win32
$ perl -e " print qq'Hellon' " # Win32; sick
$ perl -e " print qq{Hellon} " # Win32; better
Automatic end-of-line handling
Computing the meaning of 2 lives. `bc` not found
$ perl -e ' print 42 * 42 '
1764$ perl -e ' print 42 * 42 * 42 '
74088$
Automatic end-of-line handling
The -l flag adds "n" to `print`
$ perl -le ' print 42 * 42 '
1764
$ perl -le ' print 42 * 42 * 42 '
74088
file://localhost/YAPC_Talk/index.html June 22, 2009 3:25:46 PM
3. Command-line Perl 3
Unconventional conventionists
The code formerly known as:
$ perl -e ' print "Hellon"; '
with output:
Hello
will now appear as:
$ perl -e '...' any arguments here
print "Hellon";
Hello
Defaults
@ARGV is the array of command-line arguments.
shift takes @ARGV as its default.
$ perl -le '...' foo bar baz
print @ARGV; $z=shift; print $z;
foobarbaz
foo
Defaults
print takes $_ as default, and for sets $_ as default.
$ perl -le '...' foo bar baz
print for @ARGV
foo
bar
baz
file://localhost/YAPC_Talk/index.html June 22, 2009 3:25:46 PM
4. Command-line Perl 4
Warnings
Double your money!
$ perl -le '...' 15
$zz = shift; print $z . $z
(No output)
Warnings
Always pay for the cheap insurance
$ perl -wle '...' 15
$zz = shift; print $z . $z
Name "main::zz" used only once: possible typo...
Use of uninitialized value in concatenation...
Use of uninitialized value in concatenation...
Corrected
$ perl -wle '...' 15
$z = shift; print $z . $z
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
file://localhost/YAPC_Talk/index.html June 22, 2009 3:25:46 PM
5. Command-line Perl 5
Command-line Modules
$FOO::VERSION is a CPAN standard
$ perl -MData::Dumper -wle '...'
print $Data::Dumper::VERSION;
2.121_02
Cross Words
Capital of Portugal, 6 letters, ? i ? b o ?
$ perl -wlne '...' /usr/share/dict/words
print if /^[A-Z]i.bo.$/
Lisbon
Hi, Cog!
PHP
Scrambled word: ulsacepoer
$ perl -wlne '...' /usr/share/dict/words | wc
print if /^[ulsacpeeor]{10}$/
104
PHP
Scrambled word: ulsacepoer
$ perl -wlne '...' /usr/share/dict/words
print if join("", sort split "") eq "aceeloprsu"
perlaceous
Per*la"ceous, a. [See Pearl.] Pearly; resembling pearl.
file://localhost/YAPC_Talk/index.html June 22, 2009 3:25:46 PM