A whirlwind tour of the modules that any perl hacker, from beginner to experienced, should use and why.
Handout: List of modules in the talk along with many more: https://sites.google.com/site/perlhercynium/TEPHT-List2.pdf?attredirects=0
Contents
Introduction
The Little Things
Slightly Bigger Things
Handling Files and Filesystems
Wrangling Data Structures and Types
Dealing with Dates and Times
Reading and Writing File Formats
Potpourri
Conclusion
Introduction
The Little Things
Slightly Bigger Things
Handling Files and Filesystems
Wrangling Data Structures and Types
Dealing with Dates and Times
Reading and Writing File Formats
Potpourri
Conclusion
Introduction
The Little Things
Slightly Bigger Things
Handling Files and Filesystems
Wrangling Data Structures and Types
Dealing with Dates and Times
Reading and Writing File Formats
Potpourri
Conclusion
# overrides various built-ins...
use autodie;
# Now, they all succeed or die!
open my $foo, '>', 'file.txt';
chdir 'foo/bar';
pipe my($baz), my($buh);
readline $foo;
print “Do you ever check for print
errors???”;
Introduction
The Little Things
Slightly Bigger Things
Handling Files and Filesystems
Wrangling Data Structures and Types
Dealing with Dates and Times
Reading and Writing File Formats
Potpourri
Conclusion
Stop messing up inheritance,
destructors, accessors and all
those little things that you have to
write over and over and over
again...
Introduction
The Little Things
Slightly Bigger Things
Handling Files and Filesystems
Wrangling Data Structures and Types
Dealing with Dates and Times
Reading and Writing File Formats
Potpourri
Conclusion
use Path::Class qw(dir file);
# create dir and file objects...
my $dir = dir('foo', 'bar');
my $file = file('bob', 'file.txt');
# Stringifies with proper separator on
# each supported platform
print "dir: $dirn";
print "file: $filen";
# methods do what you expect...
my $subdir = $dir->subdir('baz');
my $parent = $subdir->parent;
my $dir2 = $file->dir;
Can you remember this?
my ($dev, $ino, $mode, $nlink, $uid,
$gid, $rdev, $size, $atime, $mtime,
$ctime, $blksize, $blocks) =
stat $file;
Maybe this?
my $dev = (stat $file)[0];
my $ino = (stat $file)[1];
my $mode = (stat $file)[2];
my $nlink = (stat $file)[3];
my $uid = (stat $file)[4];
my $gid = (stat $file)[5];
my $rdev = (stat $file)[6];
my $size = (stat $file)[7];
# etc...
Try this, instead...
use File::stat;
my $s = stat $file;
$s->dev;
$s->ino;
$s->size;
Much nicer, no?
Introduction
The Little Things
Slightly Bigger Things
Handling Files and Filesystems
Wrangling Data Structures and Types
Dealing with Dates and Times
Reading and Writing File Formats
Potpourri
Conclusion
use Hash::Merge::Simple qw(merge);
my $a = {
a => 1, c => 3,
d => { i => 2 }, r => {}
};
my $b = {
b => 2, a => 100,
d => { l => 4 }
};
my $c = merge $a, $b;
# $c is now {
# a => 100, b => 2, c => 3,
# d => { i => 2, l => 4 }, r => {} }
use Params::Util qw(_SCALAR _HASH _INSTANCE);
sub foo {
my $object = _INSTANCE(shift, 'Foo') or die;
my $image = _SCALAR(shift) or die;
my $opts = _HASHLIKE(shift) || {};
}
Introduction
The Little Things
Slightly Bigger Things
Handling Files and Filesystems
Wrangling Data Structures and Types
Dealing with Dates and Times
Reading and Writing File Formats
Potpourri
Conclusion
Simply works harder to find dates
and times in text, at the cost of
possibly false-positives.
Introduction
The Little Things
Slightly Bigger Things
Handling Files and Filesystems
Wrangling Data Structures and Types
Dealing with Dates and Times
Reading and Writing File Formats
Potpourri
Conclusion
use Text::CSV;
my $csv = Text::CSV->new({ binary => 1 });
open my $fh, "<:encoding(utf8)", "test.csv";
while ( my $row = $csv->getline( $fh ) ) {
$row->[2] =~ m/pattern/ or next;
push @rows, $row;
}
Introduction
The Little Things
Slightly Bigger Things
Handling Files and Filesystems
Wrangling Data Structures and Types
Dealing with Dates and Times
Reading and Writing File Formats
Potpourri
Conclusion
use IPC::System::Simple
qw(system systemx);
# overrides system() built-in...
# Succeed or die, avoid shell if @args
system("some_command",@args);
# NEVER invokes the shell
systemx("some_command",@args);
use IPC::System::Simple
qw(capture capturex);
# Use capture() instead of backticks:
(also works in list context)
# Succeeds or dies,
# avoids shell if @args
my $output =
capture("some_command",@args);
# NEVER invokes the shell
My $output =
capturex("some_command",@args);
Introduction
The Little Things
Slightly Bigger Things
Handling Files and Filesystems
Wrangling Data Structures and Types
Dealing with Dates and Times
Reading and Writing File Formats
Potpourri
Conclusion