X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlbot.pod;h=91723b70d787d6bf7f03976c516e6ed927ffb116;hb=d7d7d533fb289d8ec4fa1b5fb02f927ed0cb90cc;hp=17b3755336e30c3f83aa9aac347b2a01228ab0a3;hpb=769c28980682c9f2c20f6c60950b1b28469d23fa;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlbot.pod b/pod/perlbot.pod index 17b3755..91723b7 100644 --- a/pod/perlbot.pod +++ b/pod/perlbot.pod @@ -10,7 +10,8 @@ mechanics of object and class relationships. The reader is encouraged to consult relevant textbooks for discussion of Object Oriented definitions and methodology. This is not intended as a tutorial for object-oriented programming or as a comprehensive guide to Perl's object oriented features, -nor should it be construed as a style guide. +nor should it be construed as a style guide. If you're looking for tutorials, +be sure to read L, L, and L. The Perl motto still holds: There's more than one way to do it. @@ -82,13 +83,11 @@ variables. Named parameters are also demonstrated. package Foo; sub new { - my $type = shift; + my $type = shift; my %params = @_; - my $self = {}; - - $self->{High} = $params{High}; - $self->{Low} = $params{Low}; - + my $self = {}; + $self->{'High'} = $params{'High'}; + $self->{'Low'} = $params{'Low'}; bless $self, $type; } @@ -96,25 +95,23 @@ variables. Named parameters are also demonstrated. package Bar; sub new { - my $type = shift; + my $type = shift; my %params = @_; - my $self = []; - - $self->[0] = $params{Left}; - $self->[1] = $params{Right}; - + my $self = []; + $self->[0] = $params{'Left'}; + $self->[1] = $params{'Right'}; bless $self, $type; } package main; - my $a = Foo->new( High => 42, Low => 11 ); - print "High = $a->{High}\n"; - print "Low = $a->{Low}\n"; + $a = Foo->new( 'High' => 42, 'Low' => 11 ); + print "High=$a->{'High'}\n"; + print "Low=$a->{'Low'}\n"; - my $b = Bar->new( Left => 78, Right => 40 ); - print "Left = $b->[0]\n"; - print "Right = $b->[1]\n"; + $b = Bar->new( 'Left' => 78, 'Right' => 40 ); + print "Left=$b->[0]\n"; + print "Right=$b->[1]\n"; =head1 SCALAR INSTANCE VARIABLES @@ -124,15 +121,15 @@ An anonymous scalar can be used when only one instance variable is needed. sub new { my $type = shift; - my $self = shift; - + my $self; + $self = shift; bless \$self, $type; } package main; - my $a = Foo->new( 42 ); - print "a = $$a\n"; + $a = Foo->new( 42 ); + print "a=$$a\n"; =head1 INSTANCE VARIABLE INHERITANCE @@ -147,29 +144,25 @@ object. sub new { my $type = shift; my $self = {}; - - $self->{buz} = 42; - + $self->{'buz'} = 42; bless $self, $type; } package Foo; - our @ISA = qw( Bar ); + @ISA = qw( Bar ); sub new { my $type = shift; my $self = Bar->new; - - $self->{biz} = 11; - + $self->{'biz'} = 11; bless $self, $type; } package main; - my $a = Foo->new; - print "buz = $a->{buz}\n"; - print "biz = $a->{biz}\n"; + $a = Foo->new; + print "buz = ", $a->{'buz'}, "\n"; + print "biz = ", $a->{'biz'}, "\n"; @@ -183,9 +176,7 @@ relationships between objects. sub new { my $type = shift; my $self = {}; - - $self->{buz} = 42; - + $self->{'buz'} = 42; bless $self, $type; } @@ -194,18 +185,16 @@ relationships between objects. sub new { my $type = shift; my $self = {}; - - $self->{Bar} = Bar->new; - $self->{biz} = 11; - + $self->{'Bar'} = Bar->new; + $self->{'biz'} = 11; bless $self, $type; } package main; - my $a = Foo->new; - print "buz = $a->{Bar}->{buz}\n"; - print "biz = $a->{biz}\n"; + $a = Foo->new; + print "buz = ", $a->{'Bar'}->{'buz'}, "\n"; + print "biz = ", $a->{'biz'}, "\n"; @@ -219,17 +208,14 @@ where that method is defined. package Buz; sub goo { print "here's the goo\n" } - - package Bar; - our @ISA = qw( Buz ); + package Bar; @ISA = qw( Buz ); sub google { print "google here\n" } - package Baz; sub mumble { print "mumbling\n" } package Foo; - our @ISA = qw( Bar Baz ); + @ISA = qw( Bar Baz ); sub new { my $type = shift; @@ -251,12 +237,15 @@ where that method is defined. package main; - my $foo = Foo->new; + $foo = Foo->new; $foo->mumble; $foo->grr; $foo->goo; $foo->google; +Note that C refers to the superclasses of the current package +(C), not to the superclasses of C<$self>. + =head1 USING RELATIONSHIP WITH SDBM @@ -265,28 +254,24 @@ This example demonstrates an interface for the SDBM class. This creates a package Mydbm; - use SDBM_File; - use Tie::Hash; - - our @ISA = qw( Tie::Hash ); + require SDBM_File; + require Tie::Hash; + @ISA = qw( Tie::Hash ); sub TIEHASH { my $type = shift; my $ref = SDBM_File->new(@_); - bless { dbm => $ref }, $type; + bless {'dbm' => $ref}, $type; } - sub FETCH { my $self = shift; - my $ref = $self->{dbm}; + my $ref = $self->{'dbm'}; $ref->FETCH(@_); } - sub STORE { my $self = shift; - - if ( defined $_[0] ) { - my $ref = $self->{dbm}; + if (defined $_[0]){ + my $ref = $self->{'dbm'}; $ref->STORE(@_); } else { die "Cannot STORE an undefined key in Mydbm\n"; @@ -296,13 +281,13 @@ This example demonstrates an interface for the SDBM class. This creates a package main; use Fcntl qw( O_RDWR O_CREAT ); - tie my %foo, 'Mydbm', 'Sdbm', O_RDWR|O_CREAT, 0640; - $foo{bar} = 123; - print "foo-bar = $foo{bar}\n"; + tie %foo, "Mydbm", "Sdbm", O_RDWR|O_CREAT, 0640; + $foo{'bar'} = 123; + print "foo-bar = $foo{'bar'}\n"; - tie my %bar, 'Mydbm', 'Sdbm2', O_RDWR|O_CREAT, 0640; - $bar{Cathy} = 456; - print "bar-Cathy = $bar{Cathy}\n"; + tie %bar, "Mydbm", "Sdbm2", O_RDWR|O_CREAT, 0640; + $bar{'Cathy'} = 456; + print "bar-Cathy = $bar{'Cathy'}\n"; =head1 THINKING OF CODE REUSE @@ -320,7 +305,6 @@ that it is impossible to override the BAZ() method. my $type = shift; bless {}, $type; } - sub bar { my $self = shift; $self->FOO::private::BAZ; @@ -334,7 +318,7 @@ that it is impossible to override the BAZ() method. package main; - my $a = FOO->new; + $a = FOO->new; $a->bar; Now we try to override the BAZ() method. We would like FOO::bar() to call @@ -347,7 +331,6 @@ FOO::private::BAZ(). my $type = shift; bless {}, $type; } - sub bar { my $self = shift; $self->FOO::private::BAZ; @@ -360,9 +343,7 @@ FOO::private::BAZ(). } package GOOP; - - our @ISA = qw( FOO ); - + @ISA = qw( FOO ); sub new { my $type = shift; bless {}, $type; @@ -374,7 +355,7 @@ FOO::private::BAZ(). package main; - my $a = GOOP->new; + $a = GOOP->new; $a->bar; To create reusable code we must modify class FOO, flattening class @@ -387,7 +368,6 @@ method GOOP::BAZ() to be used in place of FOO::BAZ(). my $type = shift; bless {}, $type; } - sub bar { my $self = shift; $self->BAZ; @@ -398,21 +378,19 @@ method GOOP::BAZ() to be used in place of FOO::BAZ(). } package GOOP; - - our @ISA = qw( FOO ); + @ISA = qw( FOO ); sub new { my $type = shift; bless {}, $type; } - sub BAZ { print "in GOOP::BAZ\n"; } package main; - my $a = GOOP->new; + $a = GOOP->new; $a->bar; =head1 CLASS CONTEXT AND THE OBJECT @@ -435,12 +413,12 @@ method where that data is located. package Bar; - my %fizzle = ( Password => 'XYZZY' ); + %fizzle = ( 'Password' => 'XYZZY' ); sub new { my $type = shift; my $self = {}; - $self->{fizzle} = \%fizzle; + $self->{'fizzle'} = \%fizzle; bless $self, $type; } @@ -451,29 +429,27 @@ method where that data is located. # or %Foo::fizzle. The object already knows which # we should use, so just ask it. # - my $fizzle = $self->{fizzle}; + my $fizzle = $self->{'fizzle'}; - print "The word is $fizzle->{Password}\n"; + print "The word is ", $fizzle->{'Password'}, "\n"; } package Foo; + @ISA = qw( Bar ); - our @ISA = qw( Bar ); - - my %fizzle = ( Password => 'Rumple' ); + %fizzle = ( 'Password' => 'Rumple' ); sub new { my $type = shift; my $self = Bar->new; - $self->{fizzle} = \%fizzle; + $self->{'fizzle'} = \%fizzle; bless $self, $type; } package main; - my $a = Bar->new; - my $b = Foo->new; - + $a = Bar->new; + $b = Foo->new; $a->enter; $b->enter; @@ -496,8 +472,7 @@ object will be a BAR not a FOO, even though the constructor is in class FOO. } package BAR; - - our @ISA = qw(FOO); + @ISA = qw(FOO); sub baz { print "in BAR::baz()\n"; @@ -505,7 +480,7 @@ object will be a BAR not a FOO, even though the constructor is in class FOO. package main; - my $a = BAR->new; + $a = BAR->new; $a->baz; =head1 DELEGATION @@ -522,16 +497,14 @@ behavior by adding custom FETCH() and STORE() methods, if this is desired. package Mydbm; - use SDBM_File; - use Tie::Hash; - - our @ISA = qw( Tie::Hash ); - our $AUTOLOAD; + require SDBM_File; + require Tie::Hash; + @ISA = qw(Tie::Hash); sub TIEHASH { my $type = shift; - my $ref = SDBM_File->new(@_); - bless { delegate => $ref }; + my $ref = SDBM_File->new(@_); + bless {'delegate' => $ref}; } sub AUTOLOAD { @@ -547,12 +520,16 @@ behavior by adding custom FETCH() and STORE() methods, if this is desired. $AUTOLOAD =~ s/^Mydbm:://; # Pass the message to the delegate. - $self->{delegate}->$AUTOLOAD(@_); + $self->{'delegate'}->$AUTOLOAD(@_); } package main; use Fcntl qw( O_RDWR O_CREAT ); - tie my %foo, 'Mydbm', 'adbm', O_RDWR|O_CREAT, 0640; - $foo{bar} = 123; - print "foo-bar = $foo{bar}\n"; + tie %foo, "Mydbm", "adbm", O_RDWR|O_CREAT, 0640; + $foo{'bar'} = 123; + print "foo-bar = $foo{'bar'}\n"; + +=head1 SEE ALSO + +L, L, L.