oo perl

24
Object Oriented Perl All you ever wanted to know but were afraid to ask Leonard Miller YAPC::NA 2009 June 23

Upload: olegmmiller

Post on 29-Oct-2014

920 views

Category:

Technology


3 download

DESCRIPTION

All you ever wanted to know but were afraid to ask

TRANSCRIPT

Page 1: Oo Perl

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

print $self->{item};

print $self->item(‘item’);

print $self->item;

print $self->item->another_item

Page 3: Oo Perl

use strict;

use Data::Dumper;

my $x = 'ducks';

my @y = ('donald', 'daffy');

my %z = (donald => 'WB',

g => '');

$z{i} = 'j';

Page 4: Oo Perl

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

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

my $ref_x = \$x;my $ref_y = \@y;my $ref_z = \%z;

Page 7: Oo Perl

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

print $ref_x . "\n";print $ref_y . "\n";print $ref_z . "\n--\n";

SCALAR(0x180b578)

ARRAY(0x180b548)

HASH(0x180b56c)

--

Page 9: Oo Perl

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

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

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

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

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

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

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

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

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

Usage of bless:

bless({}, # a variable reference (usually a hashref)$_[0] # a package name.)

Page 19: Oo Perl

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

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
Page 22: Oo Perl

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

Thank you

Page 24: Oo Perl