Upload
olegmmiller
View
922
Download
3
Tags:
Embed Size (px)
DESCRIPTION
All you ever wanted to know but were afraid to ask
Citation preview
Object Oriented Perl
All you ever wanted to know but were afraid to ask
Leonard Miller
YAPC::NA 2009
June 23
print $self->{item};
print $self->item(‘item’);
print $self->item;
print $self->item->another_item
use strict;
use Data::Dumper;
my $x = 'ducks';
my @y = ('donald', 'daffy');
my %z = (donald => 'WB',
g => '');
$z{i} = 'j';
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';
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";
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";
$VAR1 = \'ducks';--$VAR1 = [ 'donald', 'daffy' ];--$VAR1 = { 'donald' => 'WB', 'g' => '', 'i' => 'j' };--
print $ref_x . "\n";print $ref_y . "\n";print $ref_z . "\n--\n";
SCALAR(0x180b578)
ARRAY(0x180b548)
HASH(0x180b56c)
--
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”
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
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”;
print "dereferencing\n";
print "scalar: ”;
print $$ref_x;
print . "\n";
print ”array: ”;
print @$ref_y;
print . "\n";dereferencing
scalar: ducks
array: donald daffy
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
--
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' };--
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
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;
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;
Usage of bless:
bless({}, # a variable reference (usually a hashref)$_[0] # a package name.)
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.
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";
use fubar;
my $oo_var = fubar->new();
$oo_var->bar('t');
print $oo_var->bar;t
$oo_var->bun; #calls sub bun
Thank you