• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Yapc::NA::2009 - Command Line Perl
 

Yapc::NA::2009 - Command Line Perl

on

  • 4,321 views

Candidate talk for YAPC::NA::2009 on using Perl from the command-line.

Candidate talk for YAPC::NA::2009 on using Perl from the command-line.

Statistics

Views

Total Views
4,321
Views on SlideShare
4,321
Embed Views
0

Actions

Likes
0
Downloads
15
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Yapc::NA::2009 - Command Line Perl Yapc::NA::2009 - Command Line Perl Document Transcript

    • 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
    • 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
    • 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
    • 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
    • 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
    • Command-line Perl 6 Dizzy Camels Scrambling word: perlaceous $ perl -MList::Util=shuffle -wle '...' perlaceous @z = split "", shift; print join "", shuffle @z; ulsacepoer Prized Doors $ perl -MList::Util=shuffle -wle '...' 27 @z = 1 .. shift; print for (shuffle @z)[0..9]; perl -MList::Util=shuffle -wle '@z = 1 .. shift ; print for (shuffle @z)[0..9];' Coke? Don't run this just for this talk! Download svn.log.gz ! $ svn -q log https://svn.parrot.org/parrot/ > svn.log ------------------------------------------------------------------- r39696 | petdance | 2009-06-21 23:12:08 -0500 (Sun, 21 Jun 2009) ------------------------------------------------------------------- r39695 | japhb | 2009-06-21 23:10:39 -0500 (Sun, 21 Jun 2009) ------------------------------------------------------------------- r39694 | cotto | 2009-06-21 21:49:35 -0500 (Sun, 21 Jun 2009) ------------------------------------------------------------------- r39693 | japhb | 2009-06-21 21:23:28 -0500 (Sun, 21 Jun 2009) ------------------------------------------------------------------- r39692 | moritz | 2009-06-21 17:08:28 -0500 (Sun, 21 Jun 2009) ------------------------------------------------------------------- r39691 | japhb | 2009-06-21 13:03:24 -0500 (Sun, 21 Jun 2009) ------------------------------------------------------------------- r39690 | coke | 2009-06-21 12:50:53 -0500 (Sun, 21 Jun 2009) ------------------------------------------------------------------- r39689 | japhb | 2009-06-21 12:36:39 -0500 (Sun, 21 Jun 2009) ------------------------------------------------------------------- r39688 | cotto | 2009-06-21 10:42:11 -0500 (Sun, 21 Jun 2009) ------------------------------------------------------------------- r39687 | bacek | 2009-06-21 06:24:16 -0500 (Sun, 21 Jun 2009) ------------------------------------------------------------------- r39686 | whiteknight | 2009-06-21 05:44:46 -0500 (Sun, 21 Jun 2009) file://localhost/YAPC_Talk/index.html June 22, 2009 3:25:46 PM
    • Command-line Perl 7 10 GOTO 10 -p reads a line, runs your code, prints a line. $ perl -wlpe '...' svn.log $_ = uc; ---------------------------------------------------------------- R39696 | PETDANCE | 2009-06-21 23:12:08 -0500 (SUN, 21 JUN 2009) ---------------------------------------------------------------- R39695 | JAPHB | 2009-06-21 23:10:39 -0500 (SUN, 21 JUN 2009) ---------------------------------------------------------------- R39694 | COTTO | 2009-06-21 21:49:35 -0500 (SUN, 21 JUN 2009) ---------------------------------------------------------------- R39693 | JAPHB | 2009-06-21 21:23:28 -0500 (SUN, 21 JUN 2009) ---------------------------------------------------------------- R39692 | MORITZ | 2009-06-21 17:08:28 -0500 (SUN, 21 JUN 2009) 10 GOTO 10 -n reads a line, runs your code. $ perl -wlne '...' svn.log next if /---/; $_ = uc; print; R39696 | PETDANCE | 2009-06-21 23:12:08 -0500 (SUN, 21 JUN 2009) R39695 | JAPHB | 2009-06-21 23:10:39 -0500 (SUN, 21 JUN 2009) R39694 | COTTO | 2009-06-21 21:49:35 -0500 (SUN, 21 JUN 2009) R39693 | JAPHB | 2009-06-21 21:23:28 -0500 (SUN, 21 JUN 2009) R39692 | MORITZ | 2009-06-21 17:08:28 -0500 (SUN, 21 JUN 2009) R39691 | JAPHB | 2009-06-21 13:03:24 -0500 (SUN, 21 JUN 2009) R39690 | COKE | 2009-06-21 12:50:53 -0500 (SUN, 21 JUN 2009) R39689 | JAPHB | 2009-06-21 12:36:39 -0500 (SUN, 21 JUN 2009) R39688 | COTTO | 2009-06-21 10:42:11 -0500 (SUN, 21 JUN 2009) R39687 | BACEK | 2009-06-21 06:24:16 -0500 (SUN, 21 JUN 2009) file://localhost/YAPC_Talk/index.html June 22, 2009 3:25:46 PM
    • Command-line Perl 8 HITBULLS $ perl -MO=Deparse -ne ' next if /---/; $_ = uc; print; ' LINE: while (defined($_ = <ARGV>)) { next if /---/; $_ = uc $_; print $_; } $ perl -MO=Deparse -pe ' $_ = uc; ' LINE: while (defined($_ = <ARGV>)) { $_ = uc $_; } continue { die "-p destination: $!n" unless print $_; } Home on the Range $ perl -ne '...' svn.log print if /^r39688/ .. /^r39686/; r39688 | cotto | 2009-06-21 10:42:11 -0500 (Sun, 21 Jun 2009) ------------------------------------------------------------------------ r39687 | bacek | 2009-06-21 06:24:16 -0500 (Sun, 21 Jun 2009) ------------------------------------------------------------------------ r39686 | whiteknight | 2009-06-21 05:44:46 -0500 (Sun, 21 Jun 2009) print if ($. == 2) .. ($. == 4); print if 2 .. 4; r39696 | petdance | 2009-06-21 23:12:08 -0500 (Sun, 21 Jun 2009) ------------------------------------------------------------------------ r39695 | japhb | 2009-06-21 23:10:39 -0500 (Sun, 21 Jun 2009) file://localhost/YAPC_Talk/index.html June 22, 2009 3:25:46 PM
    • Command-line Perl 9 Laughing Buddha, Frowning PerlMonk XKCD w/o EDVO $ ls XKCD_mirror/xkcd*.html | tail -2 XKCD_mirror/xkcd0539.html XKCD_mirror/xkcd0540.html $ perl -wlne '...' XKCD_mirror/xkcd*.html push @L, $_ if not $h{$_}++; END{ printf "%7dt%sn", $h{$_}, $_ for @L } Laughing Buddha, Frowning PerlMonk ... 539 <div id="contentContainer"> 539 <div id="middleContent" class="dialog"> 1 <h1>Barrel - Part 1</h1><br/> 1617 <br /> 1078 <div class="menuCont"> 1078 <ul> 1078 <li><a href="/1/">|&lt;</a></li> 2 <li><a href="#" accesskey="p">&lt; Prev</a></li> 539 <li><a href="http://dynamic.xkcd.com/comic/random/" id="rnd_btn_t">Random</a></li> 2 <li><a href="/2/" accesskey="n">Next &gt;</a></li> 1078 <li><a href="/">&gt;|</a></li> 1078 </ul> 2156 </div> 1617 <br/> 1 <img src="http://imgs.xkcd.com/comics/barrel_cropped_(1).jpg" title="Don't we all." alt="Barrel - Part 1" /><br/ > file://localhost/YAPC_Talk/index.html June 22, 2009 3:25:46 PM
    • Command-line Perl 10 Awk $ perl -wlane '...' svn.log print $F[0]; ------------------------------------- r39696 ------------------------------------- r39695 ------------------------------------- r39694 Old McDonald had a farm... $ perl -wlnae '...' svn.log {FAIL} Respect My Authoritay $ cut -s -d ' ' -f 3 svn.log | tr "[:upper:]" "[:lower:]" | sort | uniq -c | sort -nr $ perl -wlane '...' svn.log $h{ lc $F[2] }++ if @F>1; END{ printf "%7dt%sn", $h{$_}, $_ for sort { $h{$b} <=> $h{$a} } keys %h } 4215 leo 3008 coke 2850 bernhard 2669 jkeenan 2408 pmichaud 1975 chromatic 1814 allison You have to believe we are magic... $ perl -MY -wlane '...' svn.log $h{ lc $F[2] }++ if @F>1; END{ dhn } file://localhost/YAPC_Talk/index.html June 22, 2009 3:25:46 PM
    • Command-line Perl 11 And the hits keep coming -i -i.bak -0 -00 -0777 -E -l[octnum -m -C PERLIO ] $ perldoc perlrun $ perldoc perlvar http://perldoc.perl.org/perlrun.html http://perldoc.perl.org/perlvar.html Golf file://localhost/YAPC_Talk/index.html June 22, 2009 3:25:46 PM