Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today Matthew Heusser xndev.com - Presented to the West.

Similar presentations


Presentation on theme: "The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today Matthew Heusser xndev.com - Presented to the West."— Presentation transcript:

1 The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today Matthew Heusser xndev.com - Presented to the West Michigan Perl Mongrels – 8/25/2006

2 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

3 Velocity & Pinball skills

4 Trick #1: Use Parameters my $new = convert(' '); 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; } _

6 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!

7 Trick #4: Become a scope master use strict; { my $name = "joe"; } print $name;

8 Trick #5: File Handles my $f = open_file("TRICK1.TXT"); while ( ) { print $_; } sub open_file { my $file = shift; open INFILE, $file || die "Could not open $f for read"; return(\*INFILE); }

9 Trick #6: use croak use Carp; my $f = open_file("TRICK2.TXT"); while ( ) { print $_; } sub open_file { my $file = shift; open INFILE, $file or croak "Could not open $file for read"; return(\*INFILE); }

10 Trick #7: Handle Exceptions with eval use Carp; eval(run()); if { print "Died with message } sub run { croak "ribbet. ribbet.\n"; }

11 Trick #8: Use Warnings use warnings; my $val; $val = $val+5; # or ($val = val + 5); print "val is $val\n";

12 Trick #9 To create an error log, re-direct STDERR trick8.pl 2>err.txt

13 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; if ($num<1) { croak "get_squares must be a number"; } for (my $idx=0; $idx<$num; $idx++) { $arr[$idx]=$idx*$idx; } return }

14 Trick #11: Avoid C-Style for loops … use foreach my $rasquares = get_squares(16); my $idx; foreach my $val { print "$val\n"; } sub get_squares { my $num = shift; if ($num<1) { croak "get_squares must be a number"; } for (my $idx=0; $idx<$num; $idx++) { $arr[$idx]=$idx*$idx; } return }

15 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'}; }

16 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; }

17 Trick #14: Use Regular Expression Memory my %switches; open INFILE, "trick12.txt" or croak "failed to open trick12.txt for read"; while(my $str = ) { $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"; }

18 Trick #15: Create lists of lists with references my $tictac; my ($idx, $jdx); for ($idx=0; $idx<3; $idx++) { for ($jdx=0; $jdx<3;$jdx++) { $tictac->[$idx]->[$jdx] = "-"; }

19 Trick #16: Read a file into an array = );

20 Trick #17: Turn off Warnings when you want use warnings; $arr[0] = 'Some'; $arr[1] = 'Values'; $arr[3] = 'And some whitespace'; $arr[5] = 'To be Concatenated'; my $str = print $str. "\n"; { no warnings; my $str = print $str. "\n"; }

21 Trick #18: use backticks my $str = `ls -l`; = 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";

22 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"; }

23 Trick #20: Avoid … … Unless you really want to. my $total = 6; double($total); print "Total is $total\n"; sub double { $_[0]*=2; }

24 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'}; }

25 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'}; }

26 Trick #23 - Decode $foo = $str ? 'Y' : 'N';

27 Trick #24 Pull off parameters $isTest = $parameters =~ s/^(TEST)//;

28 Trick #26 Use = map { # Doubles the numerical #value of a list

29 Trick #27 Use grep $matches = = grep

30 Trick #28 Learn to use pop, push, shift, unshift # AN = ("Quarter", "Dime", "Nickel"); # ADD ELEMENTS "Penny"); print "Dollar"); print # REMOVE ELEMENTS print

31 Trick #28: Use CPAN / PPM Under win32, PPM

32 Trick #29: Use a tight-feedback-loop environment putty / vim

33 What to do tomorrow Go to xndev.com Get this powerpoint Print it … read it … apply it

34 What to do next week Buy a book Experiment with new techniques

35 What to do next year Give a lightning talk Speak at PM’s? Attend a conference –YAPC::NA is cheap

36 Bonus: What are your favs? Discuss the favorite tips & techniques of the audience.


Download ppt "The Source_er’s Apprentice: Powerful Tips & Techniques in Perl you can start using today Matthew Heusser xndev.com - Presented to the West."

Similar presentations


Ads by Google