utility modules that you should know about
DESCRIPTION
This slideshow was presented at Frozen Perl 2008 and covers a small portion of CPAN that any Perl programmer should be familiar with.TRANSCRIPT
josh mcadamsdoubleclick/performics
use strict;
use strict;
$perl = 'frozen';$ref = 'perl';print $$ref, "\n";
$ perl strict_refs.pl frozen
use strict;
use strict "refs";$perl = 'frozen';$ref = 'perl';print $$ref, "\n";
$ perl strict_refs.pl Can't use string ("perl") as a SCALAR ref while "strict refs" in use at strict_refs.pl line 4.
use strict;
use strict "vars";$perl = 'frozen';$ref = 'perl';print $$ref, "\n";
$ perl strict_vars.pl Global symbol "$perl" requires explicit package name at strict_vars.pl line 2.Global symbol "$ref" requires explicit package name at strict_vars.pl line 3.Global symbol "$ref" requires explicit package name at strict_vars.pl line 4.Execution of strict_vars.pl aborted due to compilation errors.
use strict;
use strict "vars";our $perl = 'frozen';my $ref = 'perl';print $$ref, "\n";
$ perl strict_vars.pl frozen
use strict;
use strict "subs";$person = Josh;$directory{mcadams} = [ first => $person ];print $directory{mcadams}->[1], "\n";
$ perl strict_subs.pl Bareword "Josh" not allowed while "strict subs" in use at strict_subs.pl line 2.Execution of strict_subs.pl aborted due to compilation errors.
use strict;
use strict "subs";$person = ‘Josh’;$directory{mcadams} = [ first => $person ];print $directory{mcadams}->[1], "\n";
$ perl strict_subs.pl Josh
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
use warnings;
use warnings;
perl -w script.pl
#/usr/bin/perl -w
use warnings;
use warnings;
no warnings;
use base;
use base;
push @ISA, ‘SomeModule’;
use base;
use base qw(SomeModule);
use constant;
use constant;
use warnings;use strict;use constant DO_NOT_DISTURB => 1;
print "leave me alone\n" if DO_NOT_DISTURB;
> perl constant.pl leave me alone
use Exporter;
use Exporter;
use MyPackage;
MyPackage::my_subroutine();
use Exporter;
package MyPackage;
use warnings;use strict;use Exporter qw(import);
our @EXPORT_OK = qw(my_subroutine);
sub my_subroutine { print "hello\n";}
1;
use Exporter;
use warnings;use strict;use MyPackage qw(my_subroutine);
my_subroutine();
use Data::Dumper;
use Data::Dumper;
use warnings;use strict;use Data::Dumper;
print Dumper \@ARGV;
> perl datadumper.pl hello frozen perl$VAR1 = [ 'hello', 'frozen', 'perl' ];
use Class::Accessor;
use Class::Accessor;
sub name { my ($self, $value) = @_; $self->{name} = $value if $value; return $self->{name};}
use Class::Accessor;
package MyClass;use base qw(Class::Accessor);__PACKAGE__->mk_accessors(qw(name));
use Carp;
use Carp;
1 package PackageOne; 2 3 sub y { z(); } 4 sub z { die('oops'); } 5 6 package PackageTwo; 7 8 sub a { b(); } 9 sub b { PackageOne::y(); }10 11 package main;12 13 PackageTwo::a();
$ perl die.pl oops at die.pl line 4.
use Carp;
1 package PackageOne; 2 3 use Carp; 4 5 sub y { z(); } 6 sub z { croak('oops'); } 7 8 package PackageTwo; 9 10 sub a { b(); }11 sub b { PackageOne::y(); }12 13 package main;14 15 PackageTwo::a();
$ perl croak.pl oops at croak.pl line 11
use Carp;
1 package PackageOne; 2 3 use Carp qw(confess); 4 5 sub y { z(); } 6 sub z { confess('oops'); } 7 8 package PackageTwo; 9 10 sub a { b(); }11 sub b { PackageOne::y(); }12 13 package main;14 15 PackageTwo::a();
$ perl confess.pl oops at confess.pl line 6 PackageOne::z() called at confess.pl line 5 PackageOne::y() called at confess.pl line 11 PackageTwo::b() called at confess.pl line 10 PackageTwo::a() called at confess.pl line 15
use Carp;
standardcaller’s
perspectivefull stacktrace
warn carp cluck
die croak confess
use Carp::Assert;
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
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
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
use File::Spec;
use File::Spec;
use warnings;use strict;use File::Spec;
my @dirs = File::Spec->splitdir($0);print '[', join('][', @dirs), "]\n";my $dir = File::Spec->join(@dirs);print $dir, "\n";
> perl /Users/joshua/examples/filespec.pl [][Users][joshua][examples][filespec.pl]/Users/joshua/examples/filespec.pl
use File::stat;
use File::stat;
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($filename);
use File::stat;
> perl filestat.pl 84
use warnings;use strict;use File::stat;
my $s = stat($0);
print $s->size, "\n";
use File::Slurp;
use File::Slurp;
{ local(*INPUT, $/); open (INPUT, $file) || die "can't open $file: $!"; $var = <INPUT>;}
use File::Slurp;
my $text = read_file( $file );my @lines = read_file( $file );
use File::Slurp;
write_file( $filename, @data );
use File::Copy;
use File::Copy;
use warnings;use strict;use File::Copy;
my $command = "cp $0 $0.bkup";`$command`;
use File::Copy;
use warnings;use strict;use File::Copy;
copy $0, $0 . '.bkup' or die $!;
use File::Temp;
use File::Temp;
use warnings;use strict;use File::Temp;
my $fh = File::Temp->new( UNLINK => 1 );
print $fh->filename, "\n";
> perl tempfile.pl /tmp/yZin81tl6z
use File::Temp;
use warnings;use strict;use File::Temp;
my $fh = File::Temp->newdir( CLEANUP => 1 );
print $fh->dirname, "\n";
> perl tempdir.pl /tmp/HggvehyAyw
use File::Find;
use File::Find;
use warnings;use strict;use File::Find;
find( sub { print "$File::Find::name\n" }, '/Users/joshua/' );
> perl filefind.pl/Users/joshua/...
use File::Next;
use File::Next;
use warnings;use strict;use File::Next;
my $files = File::Next::files( '/Users/joshua/' );
while ( defined ( my $file = $files->() ) ) { print "$file\n";}
> perl filenext.pl/Users/joshua/...
use File::Basename;
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("\n", $file, $path, $file2, $path2), "\n";
> perl /Users/joshua/examples/basename.pl basename.pl/Users/joshua/examplesbasename.pl/Users/joshua/examples/
use File::HomeDir;
use File::HomeDir;
use warnings;use strict;use File::HomeDir;
print File::HomeDir->my_home(), "\n";print File::HomeDir->my_data(), "\n";
> perl filehomedir.pl /Users/joshua/Users/joshua/Library/Application Support
use IO::File;
use IO::File;
use warnings;use strict;
my $fh;open( $fh, '<', $0 ) or die $!;print while(<$fh>);close $fh;
use IO::File;
use warnings;use strict;use IO::File;
my $fh = IO::File->new( $0, 'r' );print while(<$fh>);$fh->close;
use FindBin;
use FindBin;
package MyModule;
use warnings;use strict;
sub hello { print "hello\n";}
1;
use FindBin;
use warnings;use strict;use lib 'mylib';use MyModule;
MyModule::hello();
> perl findbin.pl hello
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.
use FindBin;
use warnings;use strict;use FindBin;use lib "$FindBin::Bin/../mylib";use MyModule;
MyModule::hello();
use Getopt::Long;
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), "\n";
> perl getopt1.pl --greeting=hello --person=josh --prefix= hello josh
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), "\n";
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)} ),
"\n";
use Pod::Usage;
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__
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
use Pod::Usage;
> perl pod_usage.pl -hUsage: 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.
use Pod::Usage;
> perl pod_usage.pl -mPOD_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)
thank you
yapc.org/America