SlideShare a Scribd company logo
My Perl
Bag of Tricks
       
        brian d foy
      The Perl Review
     YAPC::Brasil 2011




 http://slidesha.re/s06nuv
Bag of tricks
Bag of tricks
Bag of tricks
⃠
• Eliminate special cases
• Remove distractions
• Know less
• Let Perl do the work
• Scale gracefully
% perl -e "print qq(Alo Mundon)"

% perl -le "print q(Alo Mundo)"
my $string = some_sub();

open my $fh, '<', $string;

while(<$fh>){
  chomp;
  ...;
  }
use 5.014;

my $new = $old =~ s/.../.../r;

foo( $old =~ s/.../.../r );

print "%s %sn",
  $old,
  $old =~ s/.../.../r
  ;
foreach my $file ( @files ) {
  open my($fh), '>', $file or do {
    warn "... $!n";
    next FILE;
    };
  ...
  }
my $data = do {
  local $/; <DATA>
  };
...;

__END__
<?xml version="1.0"?>
<root>
...
</root>
my $data = do { local $/; <DATA> };

s|.*(?=<elem>)||; s|(?<=</elem>).*||;

my $name =
  m|<elem>(.*?)</elem>|;

__END__
<?xml version="1.0"?>
<root>
...<elem>Buster</elem>
</root>
$g =
 q("Mimi","Bean,Buster","Roscoe");

my @gatos = split /(?<="),(?=")/, $g;
$g =
 q("Roscoe "","" Cat","Bean, Buster");

my @gatos =
 split
   /(?<!"")(?<="),(?=")(?!"")/, $g;
if( ref $r eq 'ARRAY' )

if( ref $r eq ref [] )

if( ref $r eq ref {} )

if( ref $r eq ref sub {} )

if( ref $r eq ref qr// )
join "n", @entries, '';

join "nt", "t", @entries;
BEGIN {
  use Foo;
  package Foo;
  no warnings qw(redefine);
  sub foo { ... }
  }
# Git::CPAN::Patch

% git cpan-init http://...
... hack hack hack ...
% git commit
% git cpan-sendpatch
my $file = MPEG::Info->new( ... );

print
  join $/,
  map {
   $file->$_()
   } qw(acodec
        acodecraw
        achans
        ...
        );
package Modulino {
  run(@ARGV) unless caller;

  sub run {
    my @args = @_;
    ...;
    }

 }
if( /p{IsUppercaseRoman}/ ) {
 ...}

# ⅠⅤⅩⅬⅭⅮⅯↁↂↇↈ
sub IsUppercaseRoman {
    return <<"CODE_NUMBERS";
2160
2164
2169
216C 216F
2181 2182
2187 2188
CODE_NUMBERS
    }
printf
 '%1$#b %1$#o %1$d %1$#xn",
 137;


# 0b10001001 0211 137 0x89
gato( nome => 'Buster' );

sub gato {
  my %defaults = (...);
  my %config = (%defaults, @_ );
  ...;
  }
BEGIN {
  my $fixed_time = 1234567890;
  no warnings qw(redefine);

    *CORE::GLOBAL::time =
       sub { $fixed_time };

    sub set_time {
      $fixed_time = $_[0]
      }
}

ok( time > $fixed_time );
sub my_grep (&@) {
  my( $sub ) = shift;

  foreach my $arg ( @_ ) {
    local $_ = $arg;
    push @output, $arg
      if $sub->()
    }
  }
my $ucfirst_and_trim =
    composer(
      &trim_front,
      &trim_back,
      &my_ucfirst
      );

$s = $ucfirst_and_trim->($s);
sub composer {
   my (@sub_refs) = @_;
   sub {
     my $string = shift;
     foreach my $sub_ref (@sub_refs) {
        $string = $sub_ref->($string);
        }
     return $string;
     };
   }
my @tests = (
  # ARG EXPECTED LABEL
  [ [1,2,3], 6, 'Sum is 6' ],
  [ [-1,0,9], 8, 'Sum is 8' ],
  ...
  );

foreach my $test ( @tests ) {
  is(
    target_sub( $test->[ARG] ),
    $test->[EXPECTED],
    $test->[LABEL]
    );
  }
package Local::Null::Logger {
  sub new { bless  my $x, $_[0] }
  sub AUTOLOAD { shift; print @_, $/ }
  sub DESTROY { 1 }
  }

sub _init_logger {
  my $log4perl_loaded =
    eval "require Log::Log4perl; 1";

 unless( $log4perl_loaded ){
   return Local::Null::Logger->new;
   }
 ...;
 }
$object->foo->bar->baz->quux;

package Class {
  sub bar {
      return Null->new if $error;
      }
  }

package Null {
  my $null = bless {}, __PACKAGE__;
  sub new { $null }
  sub AUTOLOAD { return $_[0] }
  sub DESTROY { 1 }
  }
Obrigado

http://slidesha.re/s06nuv
http://commons.wikimedia.org/wiki/File:Pelé_jump_1958.jpg




http://commons.wikimedia.org/wiki/File:Brazilian_National_Congress.jpg

More Related Content

PDF
Parsing JSON with a single regex
brian d foy
 
PDF
Advanced modulinos
brian d foy
 
PDF
Perl Bag of Tricks - Baltimore Perl mongers
brian d foy
 
PDF
Advanced modulinos trial
brian d foy
 
PDF
The Magic Of Tie
brian d foy
 
PDF
Learning Perl 6
brian d foy
 
PDF
Learning Perl 6 (NPW 2007)
brian d foy
 
PDF
Text in search queries with examples in Perl 6
Andrew Shitov
 
Parsing JSON with a single regex
brian d foy
 
Advanced modulinos
brian d foy
 
Perl Bag of Tricks - Baltimore Perl mongers
brian d foy
 
Advanced modulinos trial
brian d foy
 
The Magic Of Tie
brian d foy
 
Learning Perl 6
brian d foy
 
Learning Perl 6 (NPW 2007)
brian d foy
 
Text in search queries with examples in Perl 6
Andrew Shitov
 

What's hot (20)

PDF
20 modules i haven't yet talked about
Tatsuhiko Miyagawa
 
KEY
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
ODP
Perl5i
Marcos Rebelo
 
PDF
Perl 6 by example
Andrew Shitov
 
PDF
Perl6 grammars
Andrew Shitov
 
PDF
Melhorando sua API com DSLs
Augusto Pascutti
 
ODP
PHP pod mikroskopom
Saša Stamenković
 
PPTX
PHP PPT FILE
AbhishekSharma2958
 
PDF
The most exciting features of PHP 7.1
Zend by Rogue Wave Software
 
PDF
I, For One, Welcome Our New Perl6 Overlords
heumann
 
PDF
News of the Symfony2 World
Fabien Potencier
 
PDF
PHPCon 2016: PHP7 by Witek Adamus / XSolve
XSolve
 
PDF
Perl6 in-production
Andrew Shitov
 
PDF
The Perl6 Type System
abrummett
 
PDF
6 things about perl 6
brian d foy
 
PDF
Xlab #1: Advantages of functional programming in Java 8
XSolve
 
PDF
Functional php
Jean Carlo Machado
 
PDF
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
PDF
How to stand on the shoulders of giants
Ian Barber
 
PDF
Git avançado
Jean Carlo Machado
 
20 modules i haven't yet talked about
Tatsuhiko Miyagawa
 
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
Perl 6 by example
Andrew Shitov
 
Perl6 grammars
Andrew Shitov
 
Melhorando sua API com DSLs
Augusto Pascutti
 
PHP pod mikroskopom
Saša Stamenković
 
PHP PPT FILE
AbhishekSharma2958
 
The most exciting features of PHP 7.1
Zend by Rogue Wave Software
 
I, For One, Welcome Our New Perl6 Overlords
heumann
 
News of the Symfony2 World
Fabien Potencier
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
XSolve
 
Perl6 in-production
Andrew Shitov
 
The Perl6 Type System
abrummett
 
6 things about perl 6
brian d foy
 
Xlab #1: Advantages of functional programming in Java 8
XSolve
 
Functional php
Jean Carlo Machado
 
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
How to stand on the shoulders of giants
Ian Barber
 
Git avançado
Jean Carlo Machado
 
Ad

Viewers also liked (12)

PDF
I ❤ CPAN
brian d foy
 
PDF
6 more things about Perl 6
brian d foy
 
PDF
Tour of the Perl docs
brian d foy
 
PDF
Create and upload your first Perl module to CPAN
brian d foy
 
PDF
Perl Power Tools - Saint Perl 6
brian d foy
 
PDF
The Surprisingly Tense History of the Schwartzian Transform
brian d foy
 
PDF
Perl 5.28 new features
brian d foy
 
PDF
Making My Own CPAN
brian d foy
 
PDF
Making My Own CPAN
brian d foy
 
PDF
Perl Conferences for Beginners
brian d foy
 
PDF
CPAN Workshop, Chicago 2014
brian d foy
 
PDF
The Whitespace in the Perl Community
brian d foy
 
I ❤ CPAN
brian d foy
 
6 more things about Perl 6
brian d foy
 
Tour of the Perl docs
brian d foy
 
Create and upload your first Perl module to CPAN
brian d foy
 
Perl Power Tools - Saint Perl 6
brian d foy
 
The Surprisingly Tense History of the Schwartzian Transform
brian d foy
 
Perl 5.28 new features
brian d foy
 
Making My Own CPAN
brian d foy
 
Making My Own CPAN
brian d foy
 
Perl Conferences for Beginners
brian d foy
 
CPAN Workshop, Chicago 2014
brian d foy
 
The Whitespace in the Perl Community
brian d foy
 
Ad

Similar to Bag of tricks (20)

PDF
Descobrindo a linguagem Perl
garux
 
ODP
Perl Moderno
Tiago Peczenyj
 
PDF
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Andrea Telatin
 
PDF
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH
Bhavsingh Maloth
 
PPT
Cleancode
hendrikvb
 
PDF
Crafting Custom Interfaces with Sub::Exporter
Ricardo Signes
 
PDF
Utility Modules That You Should Know About
joshua.mcadams
 
PDF
Bioinformatica: Leggere file con Perl, e introduzione alle espressioni regola...
Andrea Telatin
 
ODP
Modern Perl
Marcos Rebelo
 
ODP
Whatsnew in-perl
daoswald
 
PDF
Scripting3
Nao Dara
 
PDF
Perl Fitxers i Directoris
frankiejol
 
PPTX
Bioinformatics p4-io v2013-wim_vancriekinge
Prof. Wim Van Criekinge
 
PPTX
Bioinformatica p4-io
Prof. Wim Van Criekinge
 
ODP
Introduction to Perl - Day 1
Dave Cross
 
ODP
Introduction to Perl
Dave Cross
 
PPT
Crash Course in Perl – Perl tutorial for C programmers
Gil Megidish
 
PDF
Lecture19-20
tutorialsruby
 
PDF
Lecture19-20
tutorialsruby
 
PDF
perl-pocket
tutorialsruby
 
Descobrindo a linguagem Perl
garux
 
Perl Moderno
Tiago Peczenyj
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Andrea Telatin
 
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH
Bhavsingh Maloth
 
Cleancode
hendrikvb
 
Crafting Custom Interfaces with Sub::Exporter
Ricardo Signes
 
Utility Modules That You Should Know About
joshua.mcadams
 
Bioinformatica: Leggere file con Perl, e introduzione alle espressioni regola...
Andrea Telatin
 
Modern Perl
Marcos Rebelo
 
Whatsnew in-perl
daoswald
 
Scripting3
Nao Dara
 
Perl Fitxers i Directoris
frankiejol
 
Bioinformatics p4-io v2013-wim_vancriekinge
Prof. Wim Van Criekinge
 
Bioinformatica p4-io
Prof. Wim Van Criekinge
 
Introduction to Perl - Day 1
Dave Cross
 
Introduction to Perl
Dave Cross
 
Crash Course in Perl – Perl tutorial for C programmers
Gil Megidish
 
Lecture19-20
tutorialsruby
 
Lecture19-20
tutorialsruby
 
perl-pocket
tutorialsruby
 

More from brian d foy (13)

PDF
Conferences for Beginners presentation
brian d foy
 
PDF
20 years in Perl
brian d foy
 
PDF
PrettyDump Perl 6 (London.pm)
brian d foy
 
PDF
Dumping Perl 6 (French Perl Workshop)
brian d foy
 
PDF
Perl v5.26 Features (AmsterdamX.pm)
brian d foy
 
PDF
Dumping Perl 6 (AmsterdamX.pm)
brian d foy
 
PDF
Reverse Installing CPAN
brian d foy
 
PDF
Backward to DPAN
brian d foy
 
PDF
Perl docs {sux|rulez}
brian d foy
 
PDF
Why I Love CPAN
brian d foy
 
PDF
What's wrong with the perldocs
brian d foy
 
PDF
Frozen Perl 2011 Keynote
brian d foy
 
PDF
brian d foy
brian d foy
 
Conferences for Beginners presentation
brian d foy
 
20 years in Perl
brian d foy
 
PrettyDump Perl 6 (London.pm)
brian d foy
 
Dumping Perl 6 (French Perl Workshop)
brian d foy
 
Perl v5.26 Features (AmsterdamX.pm)
brian d foy
 
Dumping Perl 6 (AmsterdamX.pm)
brian d foy
 
Reverse Installing CPAN
brian d foy
 
Backward to DPAN
brian d foy
 
Perl docs {sux|rulez}
brian d foy
 
Why I Love CPAN
brian d foy
 
What's wrong with the perldocs
brian d foy
 
Frozen Perl 2011 Keynote
brian d foy
 
brian d foy
brian d foy
 

Recently uploaded (20)

PDF
Doc9.....................................
SofiaCollazos
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Doc9.....................................
SofiaCollazos
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Software Development Methodologies in 2025
KodekX
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 

Bag of tricks