advanced perl - webgui · cpan modules comprehensive perl archive network. decentralized repository...
TRANSCRIPT
![Page 1: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/1.jpg)
Advanced PerlAndy Lester
![Page 2: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/2.jpg)
CPAN modulesComprehensive Perl Archive Network.
Decentralized repository of modules
If you have a coding problem, someone's probably already handled it.
Databases, XML, CGI, templating, file access, parsing, networking, debugging, natural language, etc etc etc
![Page 3: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/3.jpg)
Finding modulesCPAN is the distribution channel.
Currently 6,000 modules
http://cpan.perl.org
http://search.cpan.org
![Page 4: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/4.jpg)
Installing a moduleGet the module and unpack it
wget http://search.cpan.org/....tar zxvf distro.tar.gzcd distro
The magic incantationperl Makefile.PLmakemake testsudo make install
Let's see that in action...
![Page 5: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/5.jpg)
Now we can use it
Get a page with Mechuse WWW::Mechanize;my $m = WWW::Mechanize->new();$m->get( "http://plainblack.com/" );print $m->title, "\n";
$ ./fetch.plPlain Black, makers of WebGUI - Home
![Page 6: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/6.jpg)
That's where we're going
![Page 7: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/7.jpg)
ReferencesScalars that refer to other data.
Any data item can be referred to.
Similar to pointers in C, but safer.
![Page 8: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/8.jpg)
ReferencesBackslash is the reference operator
my $score = 12;my $ref = \$score;print $ref, "\n";print $$ref, "\n";print $score, "\n";
$ perl ref.plSCALAR(0x806f24)1212
![Page 9: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/9.jpg)
Array referencesReference anything with its sigil
my @stooges = qw( Larry Moe Curly );my $ref = \@stooges;print join( ", ", @$ref );
Larry, Moe, Curly
![Page 10: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/10.jpg)
Hash referencesSame as arrays
my %names = ( Moe => "Howard", Larry => "Fine", Iggy => "Pop",);my $ref = \%names;
![Page 11: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/11.jpg)
Dereferencing elements
Use an arrow, and $ for the sigilmy $href = \%names;print $href->{Moe};
my $aref = \@list;print $aref->[2];
![Page 12: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/12.jpg)
Anonymous arraysSometimes you want a ref immediately
my @list = qw( Larry Moe Curly );my $stooges = \@list;
Make the reference anonymousmy $stooges = [ qw( Larry Moe Curly ) ];-or-my $stooges = [ 'Larry','Moe','Curly' ];
No need for an intermediate array variablesThe array itself has no name.
![Page 13: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/13.jpg)
Anonymous hashesSame as with anonymous arrays
my %names = ( Larry => 'Fine', ... );my $stooges = \%names;
Make the reference anonymousmy $stooges = { Larry => 'Fine', ... );print $stooges->{Larry};
No need for an intermediate hashes variables
![Page 14: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/14.jpg)
StructuresStructures are typically anonymous hashes
my $user = { id => 'mhoward', firstname => 'Moe', lastname => 'Howard', email => '[email protected]',};
print $user->{id};
![Page 15: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/15.jpg)
Nested structuresHash values are scalars, which can also be refs
my $user = { id => 'mhoward', firstname => 'Moe', lastname => 'Howard', email => '[email protected]', partners => [ 'lfine', 'cneal' ],};
my @numbskulls = @{$user->{partners}};print $numbskulls[0]; #1 numbskull
print $user->{partners}->[0]; #another way
![Page 16: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/16.jpg)
Circular structuresCan even refer to other references
my $larry = { id => 'lfine', ... };my $curly = { id => 'curly', ... };my $moe = { id => 'mhoward', partners => [ $larry, $curly ],};
print $moe->{partners}[0]{id}; # prints "lfine"
But beware of memory leaks!
![Page 17: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/17.jpg)
Data::DumperAll these references can get hard to read...
use Data::Dumper;print Dumper( $stooge );
$VAR1 = { 'email' => '[email protected]', 'firstname' => 'Moe', 'lastname' => 'Howard', 'id' => 'mhoward', 'partners' => [ 'lfine', 'cneal' ] };
![Page 18: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/18.jpg)
Take a breath
![Page 19: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/19.jpg)
Reusable codeDefine your own subroutines.
sub underlined { my $msg = shift; my $lines = $msg; $lines =~ s/\S/-/g; return "$msg\n$lines\n";}
print underline( "WebGUI Conference 2004" );
WebGUI Conference 2004------ ---------- ----
![Page 20: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/20.jpg)
Define a libraryCreate underline.pl
sub underlined { my $msg = shift; my $lines = $msg; $lines =~ s/\S/-/g; return "$msg\n$lines\n";}
1;
![Page 21: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/21.jpg)
Use the libraryCreate test.pl
#!/usr/bin/perl -w
use strict;require "underline.pl";
print underlined( "WebGUI Conference 2004" );
![Page 22: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/22.jpg)
PackagesA package is a namespace to group vars & subsYour package may want to hide its variables
package Underline;our $underline = "-";sub underlined { my $msg = shift; my $lines = $msg; $lines =~ s/\S/$underline/g; return "$msg\n$lines\n";}1; # end of Underline.pm
![Page 23: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/23.jpg)
Using a packageUse the package
use Underline;$Underline::underliner = "*";print Underline::underlined( "WebGUI Conference 2004" );
WebGUI Conference 2004****** ********** ****
It works, but what a pain.
![Page 24: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/24.jpg)
ExportingMake your module export some symbols
package Underline;use Exporter;our @ISA = qw( Exporter );our @EXPORT = qw( $underline underlined );...1; # end of Underline.pm
![Page 25: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/25.jpg)
Using exported symbols
Export variablesuse Underline;$underliner = "*";print underlined( "WebGUI Conference 2004" );
WebGUI Conference 2004****** ********** ****
Much nicer!
![Page 26: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/26.jpg)
ObjectsObjects are just blessed structures.
Inheritance is based on packages.
![Page 27: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/27.jpg)
ConstructorsCreate a structure and bless it.
package Stooge;
sub new { my $class = shift; my $name = shift || die "Must pass name!"; my $self = { name => $name, bonk_count => 0, }; bless $self, $class; return $self;}
![Page 28: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/28.jpg)
InstantiatingCall the constructor
use Stooge;
my $larry = Stooge->new( "Larry" );print Dumper( $larry );
$VAR1 = bless( { 'bonk_count' => 0, 'name' => 'Larry', }, 'Stooge' );
![Page 29: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/29.jpg)
MethodsA method is a subroutine that expects a $self
package Stooge;
sub bonk { my $self = shift; ++$self->{bonks}; print "Ow!\n";}
sub bonk_count { my $self = shift; return $self->{bonks};}
![Page 30: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/30.jpg)
Calling methodsCall a method indirectly
my $curly = Stooge->new( 'cneal' );$curly->bonk();$curly->bonk();print "Bonked ", $curly->bonk_count, " times\n";
Ow!Ow!Bonked 2 times
![Page 31: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/31.jpg)
Use your accessorsDon't look at internals directly
print $stooge->{bonks}, " bonks"; # BAD
Keep interface to objects opaqueObjects should eat their own dog food
sub status { my $self = shift;
print "I've got a headache!" if $self->bonk_count > 10;}
Performance geeks say it's slower. Ignore them.
![Page 32: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/32.jpg)
InheritanceSome classes may inherit from others
package Stooge::Moe;
our @ISA = qw( Stooge );
sub new { my $class = shift; my $self = $class->SUPER::new(@_); $self->{hairstyle} = 'bowl'; return $self;}
Uses parent's constructor, and adds its own stuff
![Page 33: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/33.jpg)
Overriding methodsCreate a new bonk method
sub bonk { my $self = shift;
print "Why you..."; $self->{bonk_count}++;}
Instantiate with appropriate constructormy $curly = Stooge->new( 'cneal' );my $moe = Stooge::Moe->new( 'mhoward' );
![Page 34: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/34.jpg)
Overridden methodsPerl checks the @ISA list to find method
$curly->bonk; # Stooge::bonk$moe->bonk; # Stooge::Moe::bonk
$curly->bonk_count # Stooge::bonk_count$moe->bonk_count # Stooge::bonk_count
Method calls are determined at runtimeYou can modify @ISA on the fly, but don't.
![Page 35: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/35.jpg)
Where to Get More Information
• Learning Perl Objects, References & Modules, by Randal Schwartz
• Object-Oriented Perl, by Damian Conway
![Page 36: Advanced Perl - WebGUI · CPAN modules Comprehensive Perl Archive Network. Decentralized repository of modules If you have a coding problem, someone's probably already handled it](https://reader034.vdocument.in/reader034/viewer/2022050511/5f9bfeb648b68f07f47490e7/html5/thumbnails/36.jpg)
Bonus topics if time allows
map/grep
anonymous subroutines
Writing a module
Command-line wizardry
Handy magic variables
POD