the source_er’s apprentice: powerful tips & techniques in perl you can start using today
DESCRIPTION
The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today. Matthew Heusser xndev.com - [email protected] Presented to the West Michigan Perl Mongrels – 8/25/2006. Techniques. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/1.jpg)
The Source_er’s Apprentice:Powerful Tips & Techniques in Perl you can start using today
Matthew Heusserxndev.com - [email protected] to the West Michigan Perl Mongrels – 8/25/2006
![Page 2: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/2.jpg)
Techniques
… there is a distinct difference between learning to use Perl, and learning to use it well. In my opinion, the best way to learn any language well is to see how others have used it to solve problems
- Some Dude on Amazon.com
![Page 3: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/3.jpg)
Velocity & Pinball skills
![Page 4: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/4.jpg)
Trick #1:Use Parameters
my $new = convert('616-555-1212');print "New Number is $new\n";
sub convert { my $num = shift; $num=~s/^616-5/269-5/g; $num=~s/^616-31/269-31/g; $num=~s/^616-32/269-32/g; return $num;}_
![Page 5: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/5.jpg)
Trick #2:Use Interpolation
my $cash = 50;
my $one = 'The $cash variable is $' . $cash;
my $two = "The \$cash variable is \$$cash";
print $one . "\n";
print "$two \n";
![Page 6: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/6.jpg)
Trick #3:use strict
$str = "Hello, World\n";
print "The value in str is $Str";– BAD!
use strict;my $str = "Hello, World\n";print "The value in str is $Str";
- GOOD!
![Page 7: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/7.jpg)
Trick #4:Become a scope master
use strict;
{
my $name = "joe";
}
print $name;
![Page 8: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/8.jpg)
Trick #5: File Handles
my $f = open_file("TRICK1.TXT");while (<$f>) { print $_;}
sub open_file { my $file = shift; open INFILE, $file
|| die "Could not open $f for read"; return(\*INFILE);}
![Page 9: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/9.jpg)
Trick #6: use croakuse Carp;my $f = open_file("TRICK2.TXT");while (<$f>) { print $_;}
sub open_file { my $file = shift; open INFILE, $file or croak "Could not open $file for read"; return(\*INFILE);}
![Page 10: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/10.jpg)
Trick #7: Handle Exceptions with eval
use Carp;
eval(run());
if ($@) {
print "Died with message $@\n";
}
sub run {
croak "ribbet. ribbet.\n";
}
![Page 11: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/11.jpg)
Trick #8:Use Warnings
use warnings;
my $val;
$val = $val+5;
# or ($val = val + 5);
print "val is $val\n";
![Page 12: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/12.jpg)
Trick #9 To create an error log, re-direct STDERR
trick8.pl 2>err.txt
![Page 13: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/13.jpg)
Trick #10:ArrayRefs as output
my $rasquares = get_squares(16);print "The square of 8 is $rasquares->[8]\n";
sub get_squares { my $num = shift; my @arr; if ($num<1) { croak "get_squares must be a number"; } for (my $idx=0; $idx<$num; $idx++) { $arr[$idx]=$idx*$idx; } return \@arr;}
![Page 14: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/14.jpg)
Trick #11:Avoid C-Style for loops … use foreach
my $rasquares = get_squares(16); my $idx; foreach my $val (@$rasquares) { print "$val\n"; }
sub get_squares { my $num = shift; my @arr; if ($num<1) { croak "get_squares must be a number"; } for (my $idx=0; $idx<$num; $idx++) { $arr[$idx]=$idx*$idx; } return \@arr;}
![Page 15: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/15.jpg)
Trick #12:Use ‘Named Parameters’
my %params = ('height',10,'length',5,'width',3);print volume(%params);
sub volume { my %param = @_; return $param{'height'}*$param{'width'}*$param{'length'}; }
![Page 16: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/16.jpg)
Trick #13:Direct-Attack your RegExps
sub convert { my $num = shift; $num=~s/^616-5/269-5/g; $num=~s/^616-31/269-31/g; $num=~s/^616-32/269-32/g; return $num;}
![Page 17: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/17.jpg)
Trick #14:Use Regular Expression Memory
my %switches;open INFILE, "trick12.txt" or croak "failed to open trick12.txt for read";
while(my $str = <INFILE>) { $str=~ /^\d\d\d-(\d\d\d)-\d\d\d\d/; my $switch = $1; if (!defined($switches{$switch})) { $switches{$switch} = 0; } $switches{$switch}++; print "$switch\n"; }
![Page 18: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/18.jpg)
Trick #15:Create lists of lists with referencesmy $tictac;
my ($idx, $jdx);for ($idx=0; $idx<3; $idx++) { for ($jdx=0; $jdx<3;$jdx++) { $tictac->[$idx]->[$jdx] = "-";
}}
![Page 19: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/19.jpg)
Trick #16:Read a file into an array
chomp(my @data = <INFILE>);
![Page 20: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/20.jpg)
Trick #17:Turn off Warnings when you want
use warnings;my @arr;$arr[0] = 'Some';$arr[1] = 'Values';$arr[3] = 'And some whitespace';$arr[5] = 'To be Concatenated';my $str = join(',', @arr);print $str . "\n";
{ no warnings; my $str = join(',', @arr); print $str . "\n";}
![Page 21: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/21.jpg)
Trick #18:use backticks
my $str = `ls -l`;my @arr = split(/\n/, $str);my $file = $arr[0];$file=~/[-rwxa][-rwxa][-rwxa][-rwxa]\s*(\d*)\s/;my $size = $1;
$file=~/\s200\d\s\s([\w\W]*)/;my $name = $1;print "$name has a size of $size";
![Page 22: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/22.jpg)
Trick #19:‘Sniff’ files with –e and -s
• Or –x, -o, -d, -T,-B, -M …
if (-e 'trick19.pl') {
my $size = -s 'trick19.pl';
print "The size of trick19.pl is $size \n";
}
![Page 23: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/23.jpg)
Trick #20:Avoid manipulating @_ …
… Unless you really want to.
my $total = 6;double($total);print "Total is $total\n";
sub double { $_[0]*=2;}
![Page 24: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/24.jpg)
Trick #21:Named parameters via anonymous hashrefs
print volume({height=>10, length=>5, width=>3});
sub volume { my $rparam = shift; return $rparam->{'height'} *$rparam->{'width'} *$rparam->{'length'}; }
![Page 25: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/25.jpg)
Trick #22:Make your subs type-safe
sub volume { my $rparam = shift; if (!defined($rparam) || ref($rparam) ne 'HASH') { croak('volume function expects a hashref'); } return $rparam->{'height'} *$rparam->{'width'} *$rparam->{'length'}; }
![Page 26: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/26.jpg)
Trick #23 - Decode
• $foo = $str ? 'Y' : 'N';
![Page 27: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/27.jpg)
Trick #24Pull off parameters
• $isTest = $parameters =~ s/^(TEST)//;
![Page 28: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/28.jpg)
Trick #26Use map
@doubled = map { $_*=2} @single;
# Doubles the numerical
#value of a list
![Page 29: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/29.jpg)
Trick #27Use grep
$matches = grep /\$/, @costs;
@us_dollars = grep /\$/, @costs;
![Page 30: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/30.jpg)
Trick #28Learn to use pop, push, shift, unshift# AN ARRAY @coins = ("Quarter", "Dime", "Nickel");
# ADD ELEMENTS push(@coins, "Penny"); print "@coins""; unshift(@coins, "Dollar"); print "@coins"; # REMOVE ELEMENTS pop(@coins); print "@coins";shift(@coins);
![Page 32: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/32.jpg)
Trick #29:Use a tight-feedback-loop environment
• putty / vim
![Page 33: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/33.jpg)
What to do tomorrow
• Go to xndev.com
• Get this powerpoint
• Print it … read it … apply it
![Page 34: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/34.jpg)
What to do next week
• Buy a book
• Experiment with new techniques
![Page 35: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/35.jpg)
What to do next year
• Give a lightning talk
• Speak at PM’s?
• Attend a conference – YAPC::NA is cheap
![Page 36: The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today](https://reader034.vdocument.in/reader034/viewer/2022042822/568153d2550346895dc1c715/html5/thumbnails/36.jpg)
Bonus: What are your favs?
• Discuss the favorite tips & techniques of the audience.