Effective perl programming involves using modules instead of reinventing the wheel. This keynote is a sightseeing tour in some core and cpan modules solving common problems
✤ $OS_ERROR instead of $! ✤ $EVAL_ERROR instead of $@ ✤ $PROGRAM_NAME instead of $0 use English; my $filename = $PROGRAM_NAME; open my $fh, "<", $filename or die $OS_ERROR; while (<$fh>) { print; } close $fh; Monday, December 12, 2011
fails ✤ Catch the exception using eval if needed ✤ For perl >= 5.10 use autodie instead use strict; use warnings; use Fatal qw/open close/; sub count_lines { my ($filename) = @_; my $lines = 0; open my $f, '<', $filename; $lines++ while (<$f>); close $f; return $lines; } Monday, December 12, 2011
block of code for each file in the tree ✤ Can use to find text in files, count files, and any other operation ✤ Takes a code ref and a list of directories to search Monday, December 12, 2011
= '.pl'; my $count = 0; # the wanted function is called once for every # path in the directory tree sub wanted { # $_ is the name of the current file $count++ if /$ext$/ } find(\&wanted, '.'); print "found $count pl files\n"; Monday, December 12, 2011
✤ Use a : after the letter to specify it taking an argument ✤ No : after the letter means a boolean option use strict; use warnings; use Getopt::Std; use Data::Dumper; my %opts; getopts('oif:', \%opts); print Dumper(\%opts); Monday, December 12, 2011
✤ use ‘print color’ to change terminal color ✤ use ‘print color reset’ to go back to normal use strict; use warnings; use Term::ANSIColor; my @colors = qw/red blue yellow magenta cyan/; foreach my $clr (@colors) { print color $clr; print "Cool im $clr\n"; print color 'reset'; } Monday, December 12, 2011
directory tree (starting with the current directory). For every file it finds, check if the file has the phrase “holy grail” in it. ✤ Print the total number of files with that phrase ✤ Improve your app: print only files that were not printed on the previous run Monday, December 12, 2011
dates ✤ Use for pretty printing and time calculations use strict; use warnings; use DateTime; my $now = DateTime->now; my $month = $now->month_name; print "We are in $month\n"; Monday, December 12, 2011
to treat exceptions differently ✤ Can also use finally ✤ perldoc Try::Tiny try { die "NetworkError"; } catch { if (/Network/) { warn 'network error'; } if (/File/) { warn 'file error'; } } Monday, December 12, 2011
behavior ✤ Attributes are provided in the call to new ✤ Use cget to read an attribute, Use configure to set ✤ Use pack to add a component to a container Monday, December 12, 2011
At its simplest, every key or mouse button pressed is an event ✤ More complex widgets produce special events ✤ Use bind to bind with an event Monday, December 12, 2011
event ✤ Events are marked inside << ... >> ✤ The second argument to bind is a subroutine ref used as calback use strict; use warnings; use Tk; my @colors = qw/red blue green white/; my $w = MainWindow->new; my $l = $w->Listbox->pack; $l->insert('end', @colors); $l->bind('<<ListboxSelect>>', sub { my ($index) = $l->curselection; print "Selected $index\n"; }); MainLoop; Monday, December 12, 2011
User starts the app with a file name and sees entire file contents on screen inside a Text widget ✤ Provide “Save” and “Quit” buttons ✤ Extra: Save only if modified ✤ Extra: Provide “Open” button that opens another file Monday, December 12, 2011