SlideShare a Scribd company logo
1 of 11
Download to read offline
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

Unit 1-introduction to perl
Unit 1-introduction to perlUnit 1-introduction to perl
Unit 1-introduction to perlsana mateen
 
2021.laravelconf.tw.slides2
2021.laravelconf.tw.slides22021.laravelconf.tw.slides2
2021.laravelconf.tw.slides2LiviaLiaoFontech
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from insidejulien pauli
 
Getting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::CGetting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::Cdaoswald
 
typemap in Perl/XS
typemap in Perl/XS  typemap in Perl/XS
typemap in Perl/XS charsbar
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13julien pauli
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaPatrick Allaert
 
Profiling php5 to php7
Profiling php5 to php7Profiling php5 to php7
Profiling php5 to php7julien pauli
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for DummiesElizabeth Smith
 
Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)julien pauli
 
Perl 5.10
Perl 5.10Perl 5.10
Perl 5.10acme
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objectsjulien pauli
 

What's hot (20)

Unit 1-introduction to perl
Unit 1-introduction to perlUnit 1-introduction to perl
Unit 1-introduction to perl
 
2021.laravelconf.tw.slides2
2021.laravelconf.tw.slides22021.laravelconf.tw.slides2
2021.laravelconf.tw.slides2
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from inside
 
Getting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::CGetting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::C
 
typemap in Perl/XS
typemap in Perl/XS  typemap in Perl/XS
typemap in Perl/XS
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
 
Building Custom PHP Extensions
Building Custom PHP ExtensionsBuilding Custom PHP Extensions
Building Custom PHP Extensions
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
 
Perl Programming - 02 Regular Expression
Perl Programming - 02 Regular ExpressionPerl Programming - 02 Regular Expression
Perl Programming - 02 Regular Expression
 
Profiling php5 to php7
Profiling php5 to php7Profiling php5 to php7
Profiling php5 to php7
 
Perl
PerlPerl
Perl
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for Dummies
 
Perl Moderno
Perl ModernoPerl Moderno
Perl Moderno
 
Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)
 
Pearl
PearlPearl
Pearl
 
Perl 5.10
Perl 5.10Perl 5.10
Perl 5.10
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
 
Power of Puppet 4
Power of Puppet 4Power of Puppet 4
Power of Puppet 4
 
Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objects
 

Viewers also liked

Evolving Software with Moose
Evolving Software with MooseEvolving Software with Moose
Evolving Software with MooseDave Cross
 
Battersea Election Candidates on the Internet
Battersea Election Candidates on the InternetBattersea Election Candidates on the Internet
Battersea Election Candidates on the InternetDave Cross
 
Watching the Press
Watching the PressWatching the Press
Watching the PressDave Cross
 
Matt's PSGI Archive
Matt's PSGI ArchiveMatt's PSGI Archive
Matt's PSGI ArchiveDave Cross
 
The Perl Community
The Perl CommunityThe Perl Community
The Perl CommunityDave Cross
 
Moo the universe and everything
Moo the universe and everythingMoo the universe and everything
Moo the universe and everythingHenry Van Styn
 
Things I Learned From Having Users
Things I Learned From Having UsersThings I Learned From Having Users
Things I Learned From Having UsersDave Cross
 

Viewers also liked (7)

Evolving Software with Moose
Evolving Software with MooseEvolving Software with Moose
Evolving Software with Moose
 
Battersea Election Candidates on the Internet
Battersea Election Candidates on the InternetBattersea Election Candidates on the Internet
Battersea Election Candidates on the Internet
 
Watching the Press
Watching the PressWatching the Press
Watching the Press
 
Matt's PSGI Archive
Matt's PSGI ArchiveMatt's PSGI Archive
Matt's PSGI Archive
 
The Perl Community
The Perl CommunityThe Perl Community
The Perl Community
 
Moo the universe and everything
Moo the universe and everythingMoo the universe and everything
Moo the universe and everything
 
Things I Learned From Having Users
Things I Learned From Having UsersThings I Learned From Having Users
Things I Learned From Having Users
 

Similar to Yapc::NA::2009 - Command Line Perl

Im trying to run make qemu-nox In a putty terminal but it.pdf
Im trying to run  make qemu-nox  In a putty terminal but it.pdfIm trying to run  make qemu-nox  In a putty terminal but it.pdf
Im trying to run make qemu-nox In a putty terminal but it.pdfmaheshkumar12354
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In PerlKang-min Liu
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Kang-min Liu
 
Whatsnew in-perl
Whatsnew in-perlWhatsnew in-perl
Whatsnew in-perldaoswald
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersKirk Kimmel
 
2010 Smith Scripting101
2010 Smith Scripting1012010 Smith Scripting101
2010 Smith Scripting101bokonen
 
PL/Perl - New Features in PostgreSQL 9.0
PL/Perl - New Features in PostgreSQL 9.0PL/Perl - New Features in PostgreSQL 9.0
PL/Perl - New Features in PostgreSQL 9.0Tim Bunce
 
Overloading Perl OPs using XS
Overloading Perl OPs using XSOverloading Perl OPs using XS
Overloading Perl OPs using XSℕicolas ℝ.
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Michael Schwern
 
Hiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret SauceHiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret SauceJesse Vincent
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.VimLin Yo-An
 
Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)David de Boer
 
Perl Sucks - and what to do about it
Perl Sucks - and what to do about itPerl Sucks - and what to do about it
Perl Sucks - and what to do about it2shortplanks
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationAttila Balazs
 

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

Im trying to run make qemu-nox In a putty terminal but it.pdf
Im trying to run  make qemu-nox  In a putty terminal but it.pdfIm trying to run  make qemu-nox  In a putty terminal but it.pdf
Im trying to run make qemu-nox In a putty terminal but it.pdf
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Whatsnew in-perl
Whatsnew in-perlWhatsnew in-perl
Whatsnew in-perl
 
Unix tips and tricks
Unix tips and tricksUnix tips and tricks
Unix tips and tricks
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one liners
 
2010 Smith Scripting101
2010 Smith Scripting1012010 Smith Scripting101
2010 Smith Scripting101
 
perlall
perlallperlall
perlall
 
PL/Perl - New Features in PostgreSQL 9.0
PL/Perl - New Features in PostgreSQL 9.0PL/Perl - New Features in PostgreSQL 9.0
PL/Perl - New Features in PostgreSQL 9.0
 
Perl Intro 4 Debugger
Perl Intro 4 DebuggerPerl Intro 4 Debugger
Perl Intro 4 Debugger
 
EC2
EC2EC2
EC2
 
Paexec -- distributed tasks over network or cpus
Paexec -- distributed tasks over network or cpusPaexec -- distributed tasks over network or cpus
Paexec -- distributed tasks over network or cpus
 
Overloading Perl OPs using XS
Overloading Perl OPs using XSOverloading Perl OPs using XS
Overloading Perl OPs using XS
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)
 
Perl dancer
Perl dancerPerl dancer
Perl dancer
 
Hiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret SauceHiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret Sauce
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)
 
Perl Sucks - and what to do about it
Perl Sucks - and what to do about itPerl Sucks - and what to do about it
Perl Sucks - and what to do about it
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl Presentation
 

Recently uploaded

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 

Recently uploaded (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 

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
  • 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