SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Perl - laziness, impatience, hubris, and one liners
                   by Kirk Kimmel
WARNING:
●   The slides for this presentation are available
    online < kimmel.github.com > licensed under a
    Creative Commons Attribution-ShareAlike 3.0
    Unported License.
●   All images and comics are copyright their
    respective owners. xkcd rocks
Further Reading (all free)
●   Andy Lester's “A Field Guide To The Perl
    Command Line”
    http://speakerdeck.com/u/petdance/p/a-field-
    guide-to-the-perl-command-line
●   The “Modern Perl” book
    https://github.com/chromatic/modern_perl_book
●   Higher Order Perl
    http://hop.perl.plover.com/#free
Basic Perl tools
●   Syntax Check - perl -c program.pl
●   perldoc - Think man pages
●   cpan shell
●   https://metacpan.org/
●   Perl::Tidy - for code beautification
●   Perl::Critic - Static code analysis. Fully
    configurable for new rulesets.
●   Devel::NYTProf - Powerful feature-rich perl
    source code profiler
perldoc in the browser
●   Yes you can use perldoc in the browser for a
    more familiar browsing environment
●   cpan Pod::Webserver
●   Start the server with: podwebserver
●   http://localhost:8020/
A preface to One-liners
●   perldoc perlrun
●   perl -e 'one line program' for Perl prior to 5.10
●   perl -E 'one line program' to use all the new
    features
●   perl -E 'say “Hello World”'
●   An alternative with less quoting
    perl -E 'say q(Hello, World)'
command flags
●   perl -n
●   Tells Perl to add the following loop around your
    program
         while (<>) {
              ...
         }
●   perl -p
         while (<>) {
              ...
              print $_;
         }
●   perl -l
    Enables automatic line-ending processing.
    If used with -n it will chomp the input record
    separator $/
●   Example:
    find . -name '*.whatever' -exec rm{} ;
    find . -name '*.whatever' | perl -lne unlink
Calculator
●   perl -ple '$_=eval'
●   10+26
    36
●   7e2
    100
●   sqrt 9
    3
●   exit
What is ack?
●   Ack is designed as a replacement for 99% of
    the uses of grep.
●   By default, ack prints the matching lines and
    can do colorized output.
●   Ack can list files that would be searched,
    without actually searching them, to let you take
    advantage of ack's file-type filtering capabilities.
●   ack has very smart defaults which make
    common search tasks faster.
Helping with the transition
●   Shell::Command is a library that emulates common shell commands and is cross
    platform.
●   cat
●   eqtime
●   rm_rf
●   rm_f
●   touch
●   mv
●   cp
●   chmod
●   mkpath
●   test_f
●   test_d
●   dos2unix
a quick example
#!/usr/bin/perl


use warnings;
use strict;
use Shell::Command;


my $name = 'newfile.pl';


rm_rf '*.tar.gz';
touch $name;
mkpath 'cars';
# https://gist.github.com/1306010
ExtUtils::Command
●   Shell::Command is a wrapper around ExtUtils::Command
●   The module is used to replace common UNIX commands. In
    all cases the functions work from @ARGV rather than taking
    arguments
●   perl -MExtUtils::Command -e touch files...
●   perl -MExtUtils::Command -e rm_f files...
●   perl -MExtUtils::Command -e rm_rf directories...
●   perl -MExtUtils::Command -e mkpath directories...
●   perl -MExtUtils::Command -e eqtime source destination
●   perl -MExtUtils::Command -e test_f file
●   perl -MExtUtils::Command -e test_d directory
what most people think:
Making Perl look pretty
●   cpan Perl::Tidy
●   perltidy -pbp script.pl > new.pl
●   .perltidyrc if you want to make a certain style
    the default for all Perl scripts.
●   perltidy -b filename.pl
●   https://gist.github.com/1305940
before: Regex::Common
              t/test_balanced.t
# VOODOO LINE-NOISE
my($C,$M,$P,$N,
$S);END{print"1..$Cn$M";print"nfailed: $Nn"if$N}
sub ok{$C++; $M.= ($_[0]||!@_)?"ok $Cn":($N+
+,"not ok $C (".
((caller 1)[1]||(caller 0)[1]).":".((caller 1)[2]||(caller 0)
[2]).")n")}
sub try{$P=qr/^$_[0]$/}sub fail{ok($S=$_[0]!~$P)}sub
pass{ok($S=$_[0]=~$P)}
after:
# VOODOO LINE-NOISE
my ( $C, $M, $P, $N, $S );
END { print "1..$Cn$M"; print "nfailed: $Nn" if $N }


sub ok {
    $C++;
    $M .= ( $_[0] || !@_ )
     ? "ok $Cn"
     :(
      $N++,
      "not ok $C ("
          . ( ( caller 1 )[1] || ( caller 0 )[1] ) . ":"
          . ( ( caller 1 )[2] || ( caller 0 )[2] ) . ")n"
     );
}
sub try { $P = qr/^$_[0]$/ }
sub fail { ok( $S = $_[0] !~ $P ) }
sub pass { ok( $S = $_[0] =~ $P ) }
Fair warning. It is going to be like this:
Perl - laziness, impatience, hubris, and one liners

Weitere ähnliche Inhalte

Was ist angesagt?

Ravada VDI Eslibre
Ravada VDI EslibreRavada VDI Eslibre
Ravada VDI Eslibrefrankiejol
 
Job Queue in Golang
Job Queue in GolangJob Queue in Golang
Job Queue in GolangBo-Yi Wu
 
agri inventory - nouka data collector / yaoya data convertor
agri inventory - nouka data collector / yaoya data convertoragri inventory - nouka data collector / yaoya data convertor
agri inventory - nouka data collector / yaoya data convertorToshiaki Baba
 
Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlHideaki Ohno
 
Go Concurrency
Go ConcurrencyGo Concurrency
Go Concurrencyjgrahamc
 
Linux fundamental - Chap 00 shell
Linux fundamental - Chap 00 shellLinux fundamental - Chap 00 shell
Linux fundamental - Chap 00 shellKenny (netman)
 
PHP Internals and Virtual Machine
PHP Internals and Virtual MachinePHP Internals and Virtual Machine
PHP Internals and Virtual Machinejulien pauli
 
All you need to know about the JavaScript event loop
All you need to know about the JavaScript event loopAll you need to know about the JavaScript event loop
All you need to know about the JavaScript event loopSaša Tatar
 
(Practical) linux 104
(Practical) linux 104(Practical) linux 104
(Practical) linux 104Arie Bregman
 
Application Logging in the 21st century - 2014.key
Application Logging in the 21st century - 2014.keyApplication Logging in the 21st century - 2014.key
Application Logging in the 21st century - 2014.keyTim Bunce
 
Ondřej Šika: Docker, Traefik a CI - Mějte nasazené všeny větve na kterých pra...
Ondřej Šika: Docker, Traefik a CI - Mějte nasazené všeny větve na kterých pra...Ondřej Šika: Docker, Traefik a CI - Mějte nasazené všeny větve na kterých pra...
Ondřej Šika: Docker, Traefik a CI - Mějte nasazené všeny větve na kterých pra...Develcz
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scriptingTony Fabeen
 
nouka inventry manager
nouka inventry managernouka inventry manager
nouka inventry managerToshiaki Baba
 
Go debugging and troubleshooting tips - from real life lessons at SignalFx
Go debugging and troubleshooting tips - from real life lessons at SignalFxGo debugging and troubleshooting tips - from real life lessons at SignalFx
Go debugging and troubleshooting tips - from real life lessons at SignalFxSignalFx
 

Was ist angesagt? (20)

Ravada VDI Eslibre
Ravada VDI EslibreRavada VDI Eslibre
Ravada VDI Eslibre
 
Job Queue in Golang
Job Queue in GolangJob Queue in Golang
Job Queue in Golang
 
agri inventory - nouka data collector / yaoya data convertor
agri inventory - nouka data collector / yaoya data convertoragri inventory - nouka data collector / yaoya data convertor
agri inventory - nouka data collector / yaoya data convertor
 
Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed Perl
 
Laravel Day / Deploy
Laravel Day / DeployLaravel Day / Deploy
Laravel Day / Deploy
 
Go Concurrency
Go ConcurrencyGo Concurrency
Go Concurrency
 
Php engine
Php enginePhp engine
Php engine
 
Linux fundamental - Chap 00 shell
Linux fundamental - Chap 00 shellLinux fundamental - Chap 00 shell
Linux fundamental - Chap 00 shell
 
Go memory
Go memoryGo memory
Go memory
 
PHP Internals and Virtual Machine
PHP Internals and Virtual MachinePHP Internals and Virtual Machine
PHP Internals and Virtual Machine
 
All you need to know about the JavaScript event loop
All you need to know about the JavaScript event loopAll you need to know about the JavaScript event loop
All you need to know about the JavaScript event loop
 
nginx mod PSGI
nginx mod PSGInginx mod PSGI
nginx mod PSGI
 
(Practical) linux 104
(Practical) linux 104(Practical) linux 104
(Practical) linux 104
 
Application Logging in the 21st century - 2014.key
Application Logging in the 21st century - 2014.keyApplication Logging in the 21st century - 2014.key
Application Logging in the 21st century - 2014.key
 
Go for Rubyists
Go for RubyistsGo for Rubyists
Go for Rubyists
 
Ondřej Šika: Docker, Traefik a CI - Mějte nasazené všeny větve na kterých pra...
Ondřej Šika: Docker, Traefik a CI - Mějte nasazené všeny větve na kterých pra...Ondřej Šika: Docker, Traefik a CI - Mějte nasazené všeny větve na kterých pra...
Ondřej Šika: Docker, Traefik a CI - Mějte nasazené všeny větve na kterých pra...
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scripting
 
nouka inventry manager
nouka inventry managernouka inventry manager
nouka inventry manager
 
tdc2012
tdc2012tdc2012
tdc2012
 
Go debugging and troubleshooting tips - from real life lessons at SignalFx
Go debugging and troubleshooting tips - from real life lessons at SignalFxGo debugging and troubleshooting tips - from real life lessons at SignalFx
Go debugging and troubleshooting tips - from real life lessons at SignalFx
 

Ähnlich wie Perl - laziness, impatience, hubris, and one liners

One-Liners to Rule Them All
One-Liners to Rule Them AllOne-Liners to Rule Them All
One-Liners to Rule Them Allegypt
 
Qore for the Perl Programmer
Qore for the Perl ProgrammerQore for the Perl Programmer
Qore for the Perl ProgrammerBrett Estrade
 
Perl In The Command Line
Perl In The Command LinePerl In The Command Line
Perl In The Command LineMarcos Rebelo
 
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
 
Fundamental of Shell Programming
Fundamental of Shell ProgrammingFundamental of Shell Programming
Fundamental of Shell ProgrammingRahul Hada
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from insidejulien pauli
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionProf. Wim Van Criekinge
 
Tips and Tricks for Increased Development Efficiency
Tips and Tricks for Increased Development EfficiencyTips and Tricks for Increased Development Efficiency
Tips and Tricks for Increased Development EfficiencyOlivier Bourgeois
 
Toolbox of a Ruby Team
Toolbox of a Ruby TeamToolbox of a Ruby Team
Toolbox of a Ruby TeamArto Artnik
 
Flame Graphs for MySQL DBAs - FOSDEM 2022 MySQL Devroom
Flame Graphs for MySQL DBAs - FOSDEM 2022 MySQL DevroomFlame Graphs for MySQL DBAs - FOSDEM 2022 MySQL Devroom
Flame Graphs for MySQL DBAs - FOSDEM 2022 MySQL DevroomValeriy Kravchuk
 

Ähnlich wie Perl - laziness, impatience, hubris, and one liners (20)

Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
One-Liners to Rule Them All
One-Liners to Rule Them AllOne-Liners to Rule Them All
One-Liners to Rule Them All
 
Shellprogramming
ShellprogrammingShellprogramming
Shellprogramming
 
Mac OSX Terminal 101
Mac OSX Terminal 101Mac OSX Terminal 101
Mac OSX Terminal 101
 
Qore for the Perl Programmer
Qore for the Perl ProgrammerQore for the Perl Programmer
Qore for the Perl Programmer
 
Bash Scripting Workshop
Bash Scripting WorkshopBash Scripting Workshop
Bash Scripting Workshop
 
Unix tips and tricks
Unix tips and tricksUnix tips and tricks
Unix tips and tricks
 
Smoking docker
Smoking dockerSmoking docker
Smoking docker
 
Perl In The Command Line
Perl In The Command LinePerl In The Command Line
Perl In The Command Line
 
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
 
Fundamental of Shell Programming
Fundamental of Shell ProgrammingFundamental of Shell Programming
Fundamental of Shell Programming
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from inside
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
 
Get your teeth into Plack
Get your teeth into PlackGet your teeth into Plack
Get your teeth into Plack
 
Tips and Tricks for Increased Development Efficiency
Tips and Tricks for Increased Development EfficiencyTips and Tricks for Increased Development Efficiency
Tips and Tricks for Increased Development Efficiency
 
Os Wilhelm
Os WilhelmOs Wilhelm
Os Wilhelm
 
Toolbox of a Ruby Team
Toolbox of a Ruby TeamToolbox of a Ruby Team
Toolbox of a Ruby Team
 
Flame Graphs for MySQL DBAs - FOSDEM 2022 MySQL Devroom
Flame Graphs for MySQL DBAs - FOSDEM 2022 MySQL DevroomFlame Graphs for MySQL DBAs - FOSDEM 2022 MySQL Devroom
Flame Graphs for MySQL DBAs - FOSDEM 2022 MySQL Devroom
 
Open mp intro_01
Open mp intro_01Open mp intro_01
Open mp intro_01
 
Cs3430 lecture 15
Cs3430 lecture 15Cs3430 lecture 15
Cs3430 lecture 15
 

Perl - laziness, impatience, hubris, and one liners

  • 1. Perl - laziness, impatience, hubris, and one liners by Kirk Kimmel
  • 2. WARNING: ● The slides for this presentation are available online < kimmel.github.com > licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. ● All images and comics are copyright their respective owners. xkcd rocks
  • 3. Further Reading (all free) ● Andy Lester's “A Field Guide To The Perl Command Line” http://speakerdeck.com/u/petdance/p/a-field- guide-to-the-perl-command-line ● The “Modern Perl” book https://github.com/chromatic/modern_perl_book ● Higher Order Perl http://hop.perl.plover.com/#free
  • 4.
  • 5. Basic Perl tools ● Syntax Check - perl -c program.pl ● perldoc - Think man pages ● cpan shell ● https://metacpan.org/ ● Perl::Tidy - for code beautification ● Perl::Critic - Static code analysis. Fully configurable for new rulesets. ● Devel::NYTProf - Powerful feature-rich perl source code profiler
  • 6. perldoc in the browser ● Yes you can use perldoc in the browser for a more familiar browsing environment ● cpan Pod::Webserver ● Start the server with: podwebserver ● http://localhost:8020/
  • 7.
  • 8. A preface to One-liners ● perldoc perlrun ● perl -e 'one line program' for Perl prior to 5.10 ● perl -E 'one line program' to use all the new features ● perl -E 'say “Hello World”' ● An alternative with less quoting perl -E 'say q(Hello, World)'
  • 9. command flags ● perl -n ● Tells Perl to add the following loop around your program while (<>) { ... } ● perl -p while (<>) { ... print $_; }
  • 10. perl -l Enables automatic line-ending processing. If used with -n it will chomp the input record separator $/ ● Example: find . -name '*.whatever' -exec rm{} ; find . -name '*.whatever' | perl -lne unlink
  • 11. Calculator ● perl -ple '$_=eval' ● 10+26 36 ● 7e2 100 ● sqrt 9 3 ● exit
  • 12. What is ack? ● Ack is designed as a replacement for 99% of the uses of grep. ● By default, ack prints the matching lines and can do colorized output. ● Ack can list files that would be searched, without actually searching them, to let you take advantage of ack's file-type filtering capabilities. ● ack has very smart defaults which make common search tasks faster.
  • 13. Helping with the transition ● Shell::Command is a library that emulates common shell commands and is cross platform. ● cat ● eqtime ● rm_rf ● rm_f ● touch ● mv ● cp ● chmod ● mkpath ● test_f ● test_d ● dos2unix
  • 14. a quick example #!/usr/bin/perl use warnings; use strict; use Shell::Command; my $name = 'newfile.pl'; rm_rf '*.tar.gz'; touch $name; mkpath 'cars'; # https://gist.github.com/1306010
  • 15. ExtUtils::Command ● Shell::Command is a wrapper around ExtUtils::Command ● The module is used to replace common UNIX commands. In all cases the functions work from @ARGV rather than taking arguments ● perl -MExtUtils::Command -e touch files... ● perl -MExtUtils::Command -e rm_f files... ● perl -MExtUtils::Command -e rm_rf directories... ● perl -MExtUtils::Command -e mkpath directories... ● perl -MExtUtils::Command -e eqtime source destination ● perl -MExtUtils::Command -e test_f file ● perl -MExtUtils::Command -e test_d directory
  • 17. Making Perl look pretty ● cpan Perl::Tidy ● perltidy -pbp script.pl > new.pl ● .perltidyrc if you want to make a certain style the default for all Perl scripts. ● perltidy -b filename.pl ● https://gist.github.com/1305940
  • 18. before: Regex::Common t/test_balanced.t # VOODOO LINE-NOISE my($C,$M,$P,$N, $S);END{print"1..$Cn$M";print"nfailed: $Nn"if$N} sub ok{$C++; $M.= ($_[0]||!@_)?"ok $Cn":($N+ +,"not ok $C (". ((caller 1)[1]||(caller 0)[1]).":".((caller 1)[2]||(caller 0) [2]).")n")} sub try{$P=qr/^$_[0]$/}sub fail{ok($S=$_[0]!~$P)}sub pass{ok($S=$_[0]=~$P)}
  • 19. after: # VOODOO LINE-NOISE my ( $C, $M, $P, $N, $S ); END { print "1..$Cn$M"; print "nfailed: $Nn" if $N } sub ok { $C++; $M .= ( $_[0] || !@_ ) ? "ok $Cn" :( $N++, "not ok $C (" . ( ( caller 1 )[1] || ( caller 0 )[1] ) . ":" . ( ( caller 1 )[2] || ( caller 0 )[2] ) . ")n" ); } sub try { $P = qr/^$_[0]$/ } sub fail { ok( $S = $_[0] !~ $P ) } sub pass { ok( $S = $_[0] =~ $P ) }
  • 20.
  • 21. Fair warning. It is going to be like this:

Hinweis der Redaktion

  1. \n \n \n \n \n
  2. \n \n \n \n \n
  3. \n \n \n \n \n
  4. \n \n \n \n \n
  5. \n \n \n \n \n
  6. \n \n \n \n \n
  7. \n \n \n \n \n
  8. \n \n \n \n \n
  9. \n \n \n \n \n
  10. \n \n \n \n \n
  11. \n \n \n \n \n
  12. \n \n \n \n \n
  13. \n \n \n \n \n
  14. \n \n \n \n \n
  15. \n \n \n \n \n
  16. \n \n \n \n \n
  17. \n \n \n \n \n
  18. \n \n \n \n \n
  19. \n \n \n \n \n
  20. \n \n \n \n \n
  21. \n \n \n \n \n
  22. \n \n \n \n \n