My PerlBag of Tricks               brian d foy      The Perl Review     YAPC::Brasil 2011 http://slidesha.re/s06nuv
⃠
• 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?roo...
$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;216021642169216C 216F2181 218...
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 };    su...
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...
my @tests = (  # ARG EXPECTED LABEL  [ [1,2,3], 6, Sum is 6 ],  [ [-1,0,9], 8, Sum is 8 ],  ...  );foreach my $test ( @tes...
package Local::Null::Logger {  sub new { bless  my $x, $_[0] }  sub AUTOLOAD { shift; print @_, $/ }  sub DESTROY { 1 }  }...
$object-foo-bar-baz-quux;package Class {  sub bar {      return Null-new if $error;      }  }package Null {  my $null = bl...
Obrigadohttp://slidesha.re/s06nuv
http://commons.wikimedia.org/wiki/File:Pelé_jump_1958.jpghttp://commons.wikimedia.org/wiki/File:Brazilian_National_Congres...
Bag of tricks
Bag of tricks
Bag of tricks
Upcoming SlideShare
Loading in...5
×

Bag of tricks

4,301

Published on

A collection of things I often use in my Perl scripts.

Published in: Technology
0 Comments
10 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,301
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
10
Embeds 0
No embeds

No notes for slide

Transcript of "Bag of tricks"

  1. 1. My PerlBag of Tricks brian d foy The Perl Review YAPC::Brasil 2011 http://slidesha.re/s06nuv
  2. 2.
  3. 3. • Eliminate special cases• Remove distractions• Know less• Let Perl do the work• Scale gracefully
  4. 4. % perl -e print qq(Alo Mundon)% perl -le print q(Alo Mundo)
  5. 5. my $string = some_sub();open my $fh, , $string;while($fh){ chomp; ...; }
  6. 6. use 5.014;my $new = $old =~ s/.../.../r;foo( $old =~ s/.../.../r );print %s %sn, $old, $old =~ s/.../.../r ;
  7. 7. foreach my $file ( @files ) { open my($fh), , $file or do { warn ... $!n; next FILE; }; ... }
  8. 8. my $data = do { local $/; DATA };...;__END__?xml version=1.0?root.../root
  9. 9. my $data = do { local $/; DATA };s|.*(?=elem)||; s|(?=/elem).*||;my $name = m|elem(.*?)/elem|;__END__?xml version=1.0?root...elemBuster/elem/root
  10. 10. $g = q(Mimi,Bean,Buster,Roscoe);my @gatos = split /(?=),(?=)/, $g;
  11. 11. $g = q(Roscoe , Cat,Bean, Buster);my @gatos = split /(?!)(?=),(?=)(?!)/, $g;
  12. 12. 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// )
  13. 13. join n, @entries, ;join nt, t, @entries;
  14. 14. BEGIN { use Foo; package Foo; no warnings qw(redefine); sub foo { ... } }
  15. 15. # Git::CPAN::Patch% git cpan-init http://...... hack hack hack ...% git commit% git cpan-sendpatch
  16. 16. my $file = MPEG::Info-new( ... );print join $/, map { $file-$_() } qw(acodec acodecraw achans ... );
  17. 17. package Modulino { run(@ARGV) unless caller; sub run { my @args = @_; ...; } }
  18. 18. if( /p{IsUppercaseRoman}/ ) { ...}# ⅠⅤⅩⅬⅭⅮⅯↁↂↇↈsub IsUppercaseRoman { return CODE_NUMBERS;216021642169216C 216F2181 21822187 2188CODE_NUMBERS }
  19. 19. printf %1$#b %1$#o %1$d %1$#xn, 137;# 0b10001001 0211 137 0x89
  20. 20. gato( nome = Buster );sub gato { my %defaults = (...); my %config = (%defaults, @_ ); ...; }
  21. 21. 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 );
  22. 22. sub my_grep (@) { my( $sub ) = shift; foreach my $arg ( @_ ) { local $_ = $arg; push @output, $arg if $sub-() } }
  23. 23. my $ucfirst_and_trim = composer( trim_front, trim_back, my_ucfirst );$s = $ucfirst_and_trim-($s);
  24. 24. sub composer { my (@sub_refs) = @_; sub { my $string = shift; foreach my $sub_ref (@sub_refs) { $string = $sub_ref-($string); } return $string; }; }
  25. 25. 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] ); }
  26. 26. 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; } ...; }
  27. 27. $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 } }
  28. 28. Obrigadohttp://slidesha.re/s06nuv
  29. 29. http://commons.wikimedia.org/wiki/File:Pelé_jump_1958.jpghttp://commons.wikimedia.org/wiki/File:Brazilian_National_Congress.jpg

×