SlideShare a Scribd company logo
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

More Related Content

What's hot (20)

PPTX
Unit 1-introduction to perl
sana mateen
 
PDF
2021.laravelconf.tw.slides2
LiviaLiaoFontech
 
PDF
Quick tour of PHP from inside
julien pauli
 
ODP
Getting started with Perl XS and Inline::C
daoswald
 
PPT
typemap in Perl/XS
charsbar
 
ODP
PHP Tips for certification - OdW13
julien pauli
 
PDF
Building Custom PHP Extensions
AzRy LLC, Caucasus School of Technology
 
PDF
Create your own PHP extension, step by step - phpDay 2012 Verona
Patrick Allaert
 
PDF
Perl Programming - 02 Regular Expression
Danairat Thanabodithammachari
 
PDF
Profiling php5 to php7
julien pauli
 
PPTX
Perl
Ahmed Hmed
 
PPTX
Php Extensions for Dummies
Elizabeth Smith
 
ODP
Perl Moderno
Tiago Peczenyj
 
ODP
Php in 2013 (Web-5 2013 conference)
julien pauli
 
PPTX
Pearl
Naga Dinesh
 
PDF
Perl 5.10
acme
 
PPT
Perl tutorial
Manav Prasad
 
PDF
Power of Puppet 4
Martin Alfke
 
ODP
Perl Introduction
Marcos Rebelo
 
PDF
Understanding PHP objects
julien pauli
 
Unit 1-introduction to perl
sana mateen
 
2021.laravelconf.tw.slides2
LiviaLiaoFontech
 
Quick tour of PHP from inside
julien pauli
 
Getting started with Perl XS and Inline::C
daoswald
 
typemap in Perl/XS
charsbar
 
PHP Tips for certification - OdW13
julien pauli
 
Building Custom PHP Extensions
AzRy LLC, Caucasus School of Technology
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Patrick Allaert
 
Perl Programming - 02 Regular Expression
Danairat Thanabodithammachari
 
Profiling php5 to php7
julien pauli
 
Php Extensions for Dummies
Elizabeth Smith
 
Perl Moderno
Tiago Peczenyj
 
Php in 2013 (Web-5 2013 conference)
julien pauli
 
Perl 5.10
acme
 
Perl tutorial
Manav Prasad
 
Power of Puppet 4
Martin Alfke
 
Perl Introduction
Marcos Rebelo
 
Understanding PHP objects
julien pauli
 

Viewers also liked (7)

ODP
Evolving Software with Moose
Dave Cross
 
PDF
Battersea Election Candidates on the Internet
Dave Cross
 
ODP
Watching the Press
Dave Cross
 
PDF
Matt's PSGI Archive
Dave Cross
 
ODP
The Perl Community
Dave Cross
 
PPTX
Moo the universe and everything
Henry Van Styn
 
ODP
Things I Learned From Having Users
Dave Cross
 
Evolving Software with Moose
Dave Cross
 
Battersea Election Candidates on the Internet
Dave Cross
 
Watching the Press
Dave Cross
 
Matt's PSGI Archive
Dave Cross
 
The Perl Community
Dave Cross
 
Moo the universe and everything
Henry Van Styn
 
Things I Learned From Having Users
Dave Cross
 
Ad

Similar to Yapc::NA::2009 - Command Line Perl (20)

ODP
Perl one-liners
daoswald
 
ODP
Perl - laziness, impatience, hubris, and one liners
Kirk Kimmel
 
PDF
newperl5
tutorialsruby
 
PDF
newperl5
tutorialsruby
 
PDF
IO Streams, Files and Directories
Krasimir Berov (Красимир Беров)
 
PDF
perltut
tutorialsruby
 
PDF
perltut
tutorialsruby
 
PDF
Introduction to PERL Programming - Complete Notes
Jason J Pulikkottil
 
PDF
System Programming and Administration
Krasimir Berov (Красимир Беров)
 
PDF
Perl.predefined.variables
King Hom
 
PPT
Perl one liners
Shaun Griffith
 
DOCX
Perl 20tips
Ravi Kumar
 
PDF
perl
tutorialsruby
 
PDF
perl
tutorialsruby
 
PPTX
Perl basics for Pentesters
Sanjeev Kumar Jaiswal
 
PPT
PERL - complete_Training_Modules_Ref.ppt
ssuserf4000e1
 
PPT
PERL - complete_guide_references (1).ppt
ssuserf4000e1
 
PPT
Perl Programming_Guide_Document_Refr.ppt
ssuserf4000e1
 
PDF
Tutorial perl programming basic eng ver
Qrembiezs Intruder
 
PPT
PERL.ppt
Farmood Alam
 
Perl one-liners
daoswald
 
Perl - laziness, impatience, hubris, and one liners
Kirk Kimmel
 
newperl5
tutorialsruby
 
newperl5
tutorialsruby
 
IO Streams, Files and Directories
Krasimir Berov (Красимир Беров)
 
perltut
tutorialsruby
 
perltut
tutorialsruby
 
Introduction to PERL Programming - Complete Notes
Jason J Pulikkottil
 
System Programming and Administration
Krasimir Berov (Красимир Беров)
 
Perl.predefined.variables
King Hom
 
Perl one liners
Shaun Griffith
 
Perl 20tips
Ravi Kumar
 
Perl basics for Pentesters
Sanjeev Kumar Jaiswal
 
PERL - complete_Training_Modules_Ref.ppt
ssuserf4000e1
 
PERL - complete_guide_references (1).ppt
ssuserf4000e1
 
Perl Programming_Guide_Document_Refr.ppt
ssuserf4000e1
 
Tutorial perl programming basic eng ver
Qrembiezs Intruder
 
PERL.ppt
Farmood Alam
 
Ad

Recently uploaded (20)

PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
July Patch Tuesday
Ivanti
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
July Patch Tuesday
Ivanti
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 

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 ([email protected]), 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 <[email protected]> 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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