SlideShare ist ein Scribd-Unternehmen logo
1 von 11
Downloaden Sie, um offline zu lesen
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

Más contenido relacionado

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Andere mochten auch

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
 

Andere mochten auch (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Último

IT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced ComputingIT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced ComputingMAGNIntelligence
 
Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Muhammad Tiham Siddiqui
 
Patch notes explaining DISARM Version 1.4 update
Patch notes explaining DISARM Version 1.4 updatePatch notes explaining DISARM Version 1.4 update
Patch notes explaining DISARM Version 1.4 updateadam112203
 
Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...DianaGray10
 
.NET 8 ChatBot with Azure OpenAI Services.pptx
.NET 8 ChatBot with Azure OpenAI Services.pptx.NET 8 ChatBot with Azure OpenAI Services.pptx
.NET 8 ChatBot with Azure OpenAI Services.pptxHansamali Gamage
 
Keep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES LiveKeep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES LiveIES VE
 
How to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxHow to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxKaustubhBhavsar6
 
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024Alkin Tezuysal
 
Automation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsAutomation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsDianaGray10
 
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through TokenizationStobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through TokenizationStobox
 
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 
20140402 - Smart house demo kit
20140402 - Smart house demo kit20140402 - Smart house demo kit
20140402 - Smart house demo kitJamie (Taka) Wang
 
Top 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTop 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTopCSSGallery
 
UiPath Studio Web workshop series - Day 4
UiPath Studio Web workshop series - Day 4UiPath Studio Web workshop series - Day 4
UiPath Studio Web workshop series - Day 4DianaGray10
 
AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024Brian Pichman
 
UiPath Studio Web workshop Series - Day 3
UiPath Studio Web workshop Series - Day 3UiPath Studio Web workshop Series - Day 3
UiPath Studio Web workshop Series - Day 3DianaGray10
 
Introduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its applicationIntroduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its applicationKnoldus Inc.
 
Oracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptxOracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptxSatishbabu Gunukula
 

Último (20)

IT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced ComputingIT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced Computing
 
Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)
 
Patch notes explaining DISARM Version 1.4 update
Patch notes explaining DISARM Version 1.4 updatePatch notes explaining DISARM Version 1.4 update
Patch notes explaining DISARM Version 1.4 update
 
Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...
 
SheDev 2024
SheDev 2024SheDev 2024
SheDev 2024
 
.NET 8 ChatBot with Azure OpenAI Services.pptx
.NET 8 ChatBot with Azure OpenAI Services.pptx.NET 8 ChatBot with Azure OpenAI Services.pptx
.NET 8 ChatBot with Azure OpenAI Services.pptx
 
Keep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES LiveKeep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES Live
 
How to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxHow to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptx
 
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
 
Automation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsAutomation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projects
 
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through TokenizationStobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
 
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
20140402 - Smart house demo kit
20140402 - Smart house demo kit20140402 - Smart house demo kit
20140402 - Smart house demo kit
 
Top 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTop 10 Squarespace Development Companies
Top 10 Squarespace Development Companies
 
UiPath Studio Web workshop series - Day 4
UiPath Studio Web workshop series - Day 4UiPath Studio Web workshop series - Day 4
UiPath Studio Web workshop series - Day 4
 
AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024
 
UiPath Studio Web workshop Series - Day 3
UiPath Studio Web workshop Series - Day 3UiPath Studio Web workshop Series - Day 3
UiPath Studio Web workshop Series - Day 3
 
Introduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its applicationIntroduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its application
 
Oracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptxOracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptx
 

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