Utility Modules That You Should Know About

5,377 views

Published on

This slideshow was presented at Frozen Perl 2008 and covers a small portion of CPAN that any Perl programmer should be familiar with.

Published in: Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,377
On SlideShare
0
From Embeds
0
Number of Embeds
47
Actions
Shares
0
Downloads
58
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Utility Modules That You Should Know About

  1. 1. josh mcadams doubleclick/performics
  2. 2. use strict;
  3. 3. use strict; $perl = 'frozen'; $ref = 'perl'; print $$ref, quot;nquot;; $ perl strict_refs.pl frozen
  4. 4. use strict; use strict quot;refsquot;; $perl = 'frozen'; $ref = 'perl'; print $$ref, quot;nquot;; $ perl strict_refs.pl Can't use string (quot;perlquot;) as a SCALAR ref while quot;strict refsquot; in use at strict_refs.pl line 4.
  5. 5. use strict; use strict quot;varsquot;; $perl = 'frozen'; $ref = 'perl'; print $$ref, quot;nquot;; $ perl strict_vars.pl Global symbol quot;$perlquot; requires explicit package name at strict_vars.pl line 2. Global symbol quot;$refquot; requires explicit package name at strict_vars.pl line 3. Global symbol quot;$refquot; requires explicit package name at strict_vars.pl line 4. Execution of strict_vars.pl aborted due to compilation errors.
  6. 6. use strict; use strict quot;varsquot;; our $perl = 'frozen'; my $ref = 'perl'; print $$ref, quot;nquot;; $ perl strict_vars.pl frozen
  7. 7. use strict; use strict quot;subsquot;; $person = Josh; $directory{mcadams} = [ first => $person ]; print $directory{mcadams}->[1], quot;nquot;; $ perl strict_subs.pl Bareword quot;Joshquot; not allowed while quot;strict subsquot; in use at strict_subs.pl line 2. Execution of strict_subs.pl aborted due to compilation errors.
  8. 8. use strict; use strict quot;subsquot;; $person = ‘Josh’; $directory{mcadams} = [ first => $person ]; print $directory{mcadams}->[1], quot;nquot;; $ perl strict_subs.pl Josh
  9. 9. use strict; - Typically ‘use strict;’ is all that you’ll need - You can ‘no strict “refs”;’ in your code - Use the strict pragma in all of your Perl code
  10. 10. use warnings;
  11. 11. use warnings; perl -w script.pl #/usr/bin/perl -w
  12. 12. use warnings; use warnings; no warnings;
  13. 13. use base;
  14. 14. use base; push @ISA, ‘SomeModule’;
  15. 15. use base; use base qw(SomeModule);
  16. 16. use constant;
  17. 17. use constant; use warnings; use strict; use constant DO_NOT_DISTURB => 1; print quot;leave me alonenquot; if DO_NOT_DISTURB; > perl constant.pl leave me alone
  18. 18. use Exporter;
  19. 19. use Exporter; use MyPackage; MyPackage::my_subroutine();
  20. 20. use Exporter; package MyPackage; use warnings; use strict; use Exporter qw(import); our @EXPORT_OK = qw(my_subroutine); sub my_subroutine { print quot;hellonquot;; } 1;
  21. 21. use Exporter; use warnings; use strict; use MyPackage qw(my_subroutine); my_subroutine();
  22. 22. use Data::Dumper;
  23. 23. use Data::Dumper; use warnings; use strict; use Data::Dumper; print Dumper @ARGV; > perl datadumper.pl hello frozen perl $VAR1 = [ 'hello', 'frozen', 'perl' ];
  24. 24. use Class::Accessor;
  25. 25. use Class::Accessor; sub name { my ($self, $value) = @_; $self->{name} = $value if $value; return $self->{name}; }
  26. 26. use Class::Accessor; package MyClass; use base qw(Class::Accessor); __PACKAGE__->mk_accessors(qw(name));
  27. 27. use Carp;
  28. 28. use Carp; 1 package PackageOne; 2 3 sub y { z(); } 4 sub z { die('oops'); } 5 6 package PackageTwo; $ perl die.pl 7 oops at die.pl line 4. 8 sub a { b(); } 9 sub b { PackageOne::y(); } 10 11 package main; 12 13 PackageTwo::a();
  29. 29. use Carp; 1 package PackageOne; 2 3 use Carp; 4 5 sub y { z(); } 6 sub z { croak('oops'); } 7 $ perl croak.pl 8 package PackageTwo; oops at croak.pl line 11 9 10 sub a { b(); } 11 sub b { PackageOne::y(); } 12 13 package main; 14 15 PackageTwo::a();
  30. 30. use Carp; 1 package PackageOne; 2 3 use Carp qw(confess); 4 $ perl confess.pl 5 sub y { z(); } oops at confess.pl line 6 6 sub z { confess('oops'); } PackageOne::z() called at 7 confess.pl line 5 PackageOne::y() called at 8 package PackageTwo; confess.pl line 11 9 PackageTwo::b() called at 10 sub a { b(); } confess.pl line 10 11 sub b { PackageOne::y(); } PackageTwo::a() called at 12 confess.pl line 15 13 package main; 14 15 PackageTwo::a();
  31. 31. use Carp; caller’s standard full stacktrace perspective warn carp cluck die croak confess
  32. 32. use Carp::Assert;
  33. 33. use Carp::Assert; use Carp::Assert; assert( 1 > 0 ); affirm { my $name = 'Josh'; my @attendees = qw( Josh Heather Addy ); grep { $_ eq $name } @attendees; } > perl assert.pl
  34. 34. use Carp::Assert; use Carp::Assert; assert( 1 > 2); affirm { my $name = 'Josh'; my @attendees = qw( Josh Heather Addy ); grep { $_ eq $name } @attendees; } > perl assert.pl Assertion failed! at /opt/local/lib/perl5/site_perl/5.8.8/Carp/Assert.pm line 281 Carp::Assert::assert('') called at assert.pl line 5
  35. 35. use Carp::Assert; use Carp::Assert; assert( 1 > 0); affirm { my $name = 'Joshua'; my @attendees = qw( Josh Heather Addy ); grep { $_ eq $name } @attendees; } > perl assert.pl Assertion ({ use warnings; use strict 'refs'; my $name = 'Joshua'; my(@attendees) = ('Josh', 'Heather', 'Addy'); grep {$_ eq $name;} @attendees; }) failed! at /opt/local/lib/perl5/site_perl/5.8.8/Carp/Assert.pm line 340 Carp::Assert::affirm('CODE(0x1800ee0)') called at assert.pl line 11
  36. 36. use File::Spec;
  37. 37. use File::Spec; use warnings; use strict; use File::Spec; my @dirs = File::Spec->splitdir($0); print '[', join('][', @dirs), quot;]nquot;; my $dir = File::Spec->join(@dirs); print $dir, quot;nquot;; > perl /Users/joshua/examples/filespec.pl [][Users][joshua][examples][filespec.pl] /Users/joshua/examples/filespec.pl
  38. 38. use File::stat;
  39. 39. use File::stat; my ($dev,$ino,$mode,$nlink,$uid, $gid,$rdev,$size,$atime,$mtime, $ctime,$blksize,$blocks) = stat ($filename);
  40. 40. use File::stat; use warnings; use strict; use File::stat; my $s = stat($0); print $s->size, quot;nquot;; > perl filestat.pl 84
  41. 41. use File::Slurp;
  42. 42. use File::Slurp; { local(*INPUT, $/); open (INPUT, $file) || die quot;can't open $file: $!quot;; $var = <INPUT>; }
  43. 43. use File::Slurp; my $text = read_file( $file ); my @lines = read_file( $file );
  44. 44. use File::Slurp; write_file( $filename, @data );
  45. 45. use File::Copy;
  46. 46. use File::Copy; use warnings; use strict; use File::Copy; my $command = quot;cp $0 $0.bkupquot;; `$command`;
  47. 47. use File::Copy; use warnings; use strict; use File::Copy; copy $0, $0 . '.bkup' or die $!;
  48. 48. use File::Temp;
  49. 49. use File::Temp; use warnings; use strict; use File::Temp; my $fh = File::Temp->new( UNLINK => 1 ); print $fh->filename, quot;nquot;; > perl tempfile.pl /tmp/yZin81tl6z
  50. 50. use File::Temp; use warnings; use strict; use File::Temp; my $fh = File::Temp->newdir( CLEANUP => 1 ); print $fh->dirname, quot;nquot;; > perl tempdir.pl /tmp/HggvehyAyw
  51. 51. use File::Find;
  52. 52. use File::Find; use warnings; use strict; use File::Find; find( sub { print quot;$File::Find::namenquot; }, '/Users/joshua/' ); > perl filefind.pl /Users/joshua/...
  53. 53. use File::Next;
  54. 54. use File::Next; use warnings; use strict; use File::Next; my $files = File::Next::files( '/Users/joshua/' ); while ( defined ( my $file = $files->() ) ) { print quot;$filenquot;; } > perl filenext.pl /Users/joshua/...
  55. 55. use File::Basename;
  56. 56. use File::Basename; use warnings; use strict; use File::Basename; my $file = basename $0; my $path = dirname $0; my ($file2, $path2) = fileparse $0; print join(quot;nquot;, $file, $path, $file2, $path2), quot;nquot;; > perl /Users/joshua/examples/basename.pl basename.pl /Users/joshua/examples basename.pl /Users/joshua/examples/
  57. 57. use File::HomeDir;
  58. 58. use File::HomeDir; use warnings; use strict; use File::HomeDir; print File::HomeDir->my_home(), quot;nquot;; print File::HomeDir->my_data(), quot;nquot;; > perl filehomedir.pl /Users/joshua /Users/joshua/Library/Application Support
  59. 59. use IO::File;
  60. 60. use IO::File; use warnings; use strict; my $fh; open( $fh, '<', $0 ) or die $!; print while(<$fh>); close $fh;
  61. 61. use IO::File; use warnings; use strict; use IO::File; my $fh = IO::File->new( $0, 'r' ); print while(<$fh>); $fh->close;
  62. 62. use FindBin;
  63. 63. use FindBin; package MyModule; use warnings; use strict; sub hello { print quot;hellonquot;; } 1;
  64. 64. use FindBin; use warnings; use strict; use lib 'mylib'; use MyModule; MyModule::hello(); > perl findbin.pl hello
  65. 65. use FindBin; > perl ~/bin/findbin.pl Can't locate MyModule.pm in @INC (@INC contains: mylib / opt/local/lib/perl5/5.8.8/darwin-2level /opt/local/lib/ perl5/5.8.8 /opt/local/lib/perl5/site_perl/5.8.8/ darwin-2level /opt/local/lib/perl5/site_perl/5.8.8 /opt/ local/lib/perl5/site_perl /opt/local/lib/perl5/ vendor_perl/5.8.8/darwin-2level /opt/local/lib/perl5/ vendor_perl/5.8.8 /opt/local/lib/perl5/vendor_perl .) at Utility Modules That You Should Know About/findbin.pl line 4. BEGIN failed--compilation aborted at Utility Modules That You Should Know About/findbin.pl line 4.
  66. 66. use FindBin; use warnings; use strict; use FindBin; use lib quot;$FindBin::Bin/../mylibquot;; use MyModule; MyModule::hello();
  67. 67. use Getopt::Long;
  68. 68. use Getopt::Long; use Getopt::Long; GetOptions( 'greeting=s', 'person=s', 'prefix:s' ) or die('error'); print join( q[ ], $opt_greeting, ($opt_prefix || q[]), $opt_person), quot;nquot;; > perl getopt1.pl --greeting=hello --person=josh --prefix= hello josh
  69. 69. use Getopt::Long; use warnings; use strict; use Getopt::Long; my ($greeting, $person, $prefix); GetOptions( 'greeting=s' => $greeting, 'person=s' => $person, 'prefix:s' => $prefix, ) or die('error'); print join( q[ ], $greeting, ($prefix || q[]), $person), quot;nquot;;
  70. 70. use Getopt::Long; use warnings; use strict; use Getopt::Long; my %options; GetOptions( %options, 'greeting=s', 'person=s', 'prefix:s', ) or die('error'); print join( q[ ], @options{ qw(greeting prefix person) } ), quot;nquot;;
  71. 71. use Pod::Usage;
  72. 72. use Pod::Usage; use Getopt::Long; use Pod::Usage; GetOptions('help', 'man') or pod2usage(2); pod2usage(1) if $opt_help; pod2usage(-verbose => 2) if $opt_man; __END__
  73. 73. use Pod::Usage; =head1 NAME sample - Using GetOpt::Long and Pod::Usage =head1 SYNOPSIS sample [options] Options: -help brief help message -man full documentation =head1 OPTIONS =over 8 =item B<-help> Print a brief help message and exits. =item B<-man> Prints the manual page and exits. =back =head1 DESCRIPTION Sample is exactly that, a sample. =cut
  74. 74. use Pod::Usage; > perl pod_usage.pl -h Usage: pod_usage [options] Options: -help brief help message -man full documentation Options: -help Print a brief help message and exits. -man Prints the manual page and exits.
  75. 75. use Pod::Usage; > perl pod_usage.pl -m POD_USAGE(1) User Contributed Perl Documentation POD_USAGE(1) NAME pod_usage - Using GetOpt::Long and Pod::Usage SYNOPSIS pod_usage [options] Options: -help brief help message -man full documentation OPTIONS -help Print a brief help message and exits. -man Prints the manual page and exits. DESCRIPTION pod_usage just an example. perl v5.8.8 2008-02-16 POD_USAGE(1)
  76. 76. thank you
  77. 77. yapc.org/America

×