oo perl
DESCRIPTION
All you ever wanted to know but were afraid to askTRANSCRIPT
![Page 1: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/1.jpg)
Object Oriented Perl
All you ever wanted to know but were afraid to ask
Leonard Miller
YAPC::NA 2009
June 23
![Page 2: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/2.jpg)
print $self->{item};
print $self->item(‘item’);
print $self->item;
print $self->item->another_item
![Page 3: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/3.jpg)
use strict;
use Data::Dumper;
my $x = 'ducks';
my @y = ('donald', 'daffy');
my %z = (donald => 'WB',
g => '');
$z{i} = 'j';
![Page 4: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/4.jpg)
print "dumping x: ” . Dumper($x);
print "dumping y: ” . Dumper(@y);
print "dumping z: ” . Dumper(%z);
dumping x: $VAR1 = 'ducks';dumping y: $VAR1 = 'donald';$VAR2 = 'daffy';dumping z: $VAR1 = 'donald';$VAR2 = 'WB';$VAR3 = 'g';$VAR4 = '';$VAR5 = 'i';$VAR6 = 'j';
![Page 5: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/5.jpg)
my $ref_x = \$x;my $ref_y = \@y;my $ref_z = \%z;print Dumper($ref_x) . "--\n";print Dumper($ref_y) . "--\n";print Dumper($ref_z) . "--\n";print $ref_x . "\n";print $ref_y . "\n";print $ref_z . "\n--\n";
![Page 6: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/6.jpg)
my $ref_x = \$x;my $ref_y = \@y;my $ref_z = \%z;
![Page 7: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/7.jpg)
print Dumper($ref_x) . "--\n";print Dumper($ref_y) . "--\n";print Dumper($ref_z) . "--\n";
$VAR1 = \'ducks';--$VAR1 = [ 'donald', 'daffy' ];--$VAR1 = { 'donald' => 'WB', 'g' => '', 'i' => 'j' };--
![Page 8: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/8.jpg)
print $ref_x . "\n";print $ref_y . "\n";print $ref_z . "\n--\n";
SCALAR(0x180b578)
ARRAY(0x180b548)
HASH(0x180b56c)
--
![Page 9: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/9.jpg)
Symbol Table hints:$x is the same as ${x}
@y is the same as @{y}
my $item = ‘cow’; my $number=3;
print “there are $number $items\n”;Global symbol "$items" requires explicit package
name
print “there are $number ${item}s\n”;“there are three cows”
![Page 10: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/10.jpg)
Symbol Table hints:@y is the same as @{y}
@array = @{[ 1,2,3]};
#does exactly what you would expect
@array = @{[ fcn(1), fcn(2), fcn(3) ]};
#does also what you would expect
![Page 11: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/11.jpg)
Symbol Table hints:@y is the same as @{y}
@array = @{[ 1,2,3]};
#does exactly what you would expect
#same as:
#@array = (1,2,3)
@array = @{[ fcn(1), fcn(2), fcn(3) ]};
#does also what you would expect
print “the function returns @{[ fcn(1) ]} \n”;
![Page 12: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/12.jpg)
print "dereferencing\n";
print "scalar: ”;
print $$ref_x;
print . "\n";
print ”array: ”;
print @$ref_y;
print . "\n";dereferencing
scalar: ducks
array: donald daffy
![Page 13: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/13.jpg)
print "array element: ”;
print $$ref_y[0];
print . "\n”;print "hash: ”;
print %$ref_z;
print "\n";
print "hash element: ”;
print $$ref_z{donald}
print "\n--\n";array element: donald
hash: donaldWBgij
hash element: WB
--
![Page 14: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/14.jpg)
print Dumper($ref_x) . "--\n";print Dumper($ref_y) . "--\n";print Dumper($ref_z) . "--\n";
$VAR1 = \'ducks';--$VAR1 = [ 'donald', 'daffy' ];--$VAR1 = { 'donald' => 'WB', 'g' => '', 'i' => 'j' };--
![Page 15: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/15.jpg)
This is syntax that you are more likely to see in oo programs:
print "dereferencing with an arrow\n";print $ref_y->[0];print "\n";print $ref_z->{donald};print "\n";
dereferencing with an arrowdonaldWB
![Page 16: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/16.jpg)
package foo;my $bar;sub bar{ my $self = shift; #first argument is the #'blessed' variable if(@_) { $bar = $_[0] } return $bar; }sub new{ return bless({},$_[0])};1;
![Page 17: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/17.jpg)
package foo;my $bar;sub bar{ my $self = shift; #first argument is the #'blessed' variable if(@_) { $bar = $_[0] } return $bar; }sub new{ return bless({},$_[0])};1;
![Page 18: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/18.jpg)
Usage of bless:
bless({}, # a variable reference (usually a hashref)$_[0] # a package name.)
![Page 19: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/19.jpg)
Usage of bless:
bless({}, # a variable reference (usually a hashref)$_[0] # a package name.)
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
![Page 20: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/20.jpg)
use foo;$ref_z = foo::new(); #same as:#$ref_z = {};#bless($ref_z, 'foo');
#setting the variable bar:$ref_z->bar("yowza");#getting the variable bar:print $ref_z->bar;print "\n";
![Page 21: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/21.jpg)
![Page 22: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/22.jpg)
use fubar;
my $oo_var = fubar->new();
$oo_var->bar('t');
print $oo_var->bar;t
$oo_var->bun; #calls sub bun
![Page 23: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/23.jpg)
Thank you
![Page 24: Oo Perl](https://reader033.vdocuments.us/reader033/viewer/2022061110/54505518af79590a418ba6ce/html5/thumbnails/24.jpg)