X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlbot.pod;h=e4952661b0b76e5910aa08c1c6e5f93a25e5ed20;hb=21fff32650ffc8719558f625ca6c5cc49a442d92;hp=de2207a9617acca9699f88f7250727e6288f908d;hpb=c296029969658ed2c8d9a223d4b09026463ca970;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlbot.pod b/pod/perlbot.pod index de2207a..e495266 100644 --- a/pod/perlbot.pod +++ b/pod/perlbot.pod @@ -2,7 +2,7 @@ perlbot - Bag'o Object Tricks (the BOT) -=head1 INTRODUCTION +=head1 DESCRIPTION The following collection of tricks and hints is intended to whet curious appetites about such things as the use of instance variables and the @@ -57,7 +57,7 @@ See L. =item 7 -IO syntax is certainly less noisy, but it is also prone to ambiguities which +IO syntax is certainly less noisy, but it is also prone to ambiguities that can cause difficult-to-find bugs. Allow people to use the sure-thing OO syntax, even if you don't like it. @@ -104,14 +104,14 @@ variables. Named parameters are also demonstrated. package main; - $a = Foo->new( 'High' => 42, 'Low' => 11 ); - print "High=$a->{'High'}\n"; - print "Low=$a->{'Low'}\n"; - - $b = Bar->new( 'Left' => 78, 'Right' => 40 ); - print "Left=$b->[0]\n"; - print "Right=$b->[1]\n"; - + $x = Foo->new( 'High' => 42, 'Low' => 11 ); + print "High=$x->{'High'}\n"; + print "Low=$x->{'Low'}\n"; + + $y = Bar->new( 'Left' => 78, 'Right' => 40 ); + print "Left=$y->[0]\n"; + print "Right=$y->[1]\n"; + =head1 SCALAR INSTANCE VARIABLES An anonymous scalar can be used when only one instance variable is needed. @@ -127,8 +127,8 @@ An anonymous scalar can be used when only one instance variable is needed. package main; - $a = Foo->new( 42 ); - print "a=$$a\n"; + $x = Foo->new( 42 ); + print "a=$$x\n"; =head1 INSTANCE VARIABLE INHERITANCE @@ -159,9 +159,9 @@ object. package main; - $a = Foo->new; - print "buz = ", $a->{'buz'}, "\n"; - print "biz = ", $a->{'biz'}, "\n"; + $x = Foo->new; + print "buz = ", $x->{'buz'}, "\n"; + print "biz = ", $x->{'biz'}, "\n"; @@ -191,19 +191,18 @@ relationships between objects. package main; - $a = Foo->new; - print "buz = ", $a->{'Bar'}->{'buz'}, "\n"; - print "biz = ", $a->{'biz'}, "\n"; + $x = Foo->new; + print "buz = ", $x->{'Bar'}->{'buz'}, "\n"; + print "biz = ", $x->{'biz'}, "\n"; =head1 OVERRIDING SUPERCLASS METHODS -The following example demonstrates how one might override a superclass -method and then call the method after it has been overridden. The -Foo::Inherit class allows the programmer to call an overridden superclass -method without actually knowing where that method is defined. - +The following example demonstrates how to override a superclass method and +then call the overridden method. The B pseudo-class allows the +programmer to call an overridden superclass method without actually knowing +where that method is defined. package Buz; sub goo { print "here's the goo\n" } @@ -216,7 +215,6 @@ method without actually knowing where that method is defined. package Foo; @ISA = qw( Bar Baz ); - @Foo::Inherit::ISA = @ISA; # Access to overridden methods. sub new { my $type = shift; @@ -225,15 +223,15 @@ method without actually knowing where that method is defined. sub grr { print "grumble\n" } sub goo { my $self = shift; - $self->Foo::Inherit::goo(); + $self->SUPER::goo(); } sub mumble { my $self = shift; - $self->Foo::Inherit::mumble(); + $self->SUPER::mumble(); } sub google { my $self = shift; - $self->Foo::Inherit::google(); + $self->SUPER::google(); } package main; @@ -253,8 +251,8 @@ This example demonstrates an interface for the SDBM class. This creates a package Mydbm; require SDBM_File; - require TieHash; - @ISA = qw( TieHash ); + require Tie::Hash; + @ISA = qw( Tie::Hash ); sub TIEHASH { my $type = shift; @@ -267,7 +265,7 @@ This example demonstrates an interface for the SDBM class. This creates a $ref->FETCH(@_); } sub STORE { - my $self = shift; + my $self = shift; if (defined $_[0]){ my $ref = $self->{'dbm'}; $ref->STORE(@_); @@ -279,11 +277,11 @@ This example demonstrates an interface for the SDBM class. This creates a package main; use Fcntl qw( O_RDWR O_CREAT ); - tie %foo, Mydbm, "Sdbm", O_RDWR|O_CREAT, 0640; + tie %foo, "Mydbm", "Sdbm", O_RDWR|O_CREAT, 0640; $foo{'bar'} = 123; print "foo-bar = $foo{'bar'}\n"; - tie %bar, Mydbm, "Sdbm2", O_RDWR|O_CREAT, 0640; + tie %bar, "Mydbm", "Sdbm2", O_RDWR|O_CREAT, 0640; $bar{'Cathy'} = 456; print "bar-Cathy = $bar{'Cathy'}\n"; @@ -316,8 +314,8 @@ that it is impossible to override the BAZ() method. package main; - $a = FOO->new; - $a->bar; + $x = FOO->new; + $x->bar; Now we try to override the BAZ() method. We would like FOO::bar() to call GOOP::BAZ(), but this cannot happen because FOO::bar() explicitly calls @@ -353,8 +351,8 @@ FOO::private::BAZ(). package main; - $a = GOOP->new; - $a->bar; + $x = GOOP->new; + $x->bar; To create reusable code we must modify class FOO, flattening class FOO::private. The next example shows a reusable class FOO which allows the @@ -388,8 +386,8 @@ method GOOP::BAZ() to be used in place of FOO::BAZ(). package main; - $a = GOOP->new; - $a->bar; + $x = GOOP->new; + $x->bar; =head1 CLASS CONTEXT AND THE OBJECT @@ -406,7 +404,7 @@ This problem can be solved by using the object to define the context of the method. Let the method look in the object for a reference to the data. The alternative is to force the method to go hunting for the data ("Is it in my class, or in a subclass? Which subclass?"), and this can be inconvenient -and will lead to hackery. It is better to just let the object tell the +and will lead to hackery. It is better just to let the object tell the method where that data is located. package Bar; @@ -422,7 +420,7 @@ method where that data is located. sub enter { my $self = shift; - + # Don't try to guess if we should use %Bar::fizzle # or %Foo::fizzle. The object already knows which # we should use, so just ask it. @@ -446,10 +444,10 @@ method where that data is located. package main; - $a = Bar->new; - $b = Foo->new; - $a->enter; - $b->enter; + $x = Bar->new; + $y = Foo->new; + $x->enter; + $y->enter; =head1 INHERITING A CONSTRUCTOR @@ -478,8 +476,8 @@ object will be a BAR not a FOO, even though the constructor is in class FOO. package main; - $a = BAR->new; - $a->baz; + $x = BAR->new; + $x->baz; =head1 DELEGATION @@ -496,8 +494,8 @@ behavior by adding custom FETCH() and STORE() methods, if this is desired. package Mydbm; require SDBM_File; - require TieHash; - @ISA = qw(TieHash); + require Tie::Hash; + @ISA = qw(Tie::Hash); sub TIEHASH { my $type = shift; @@ -524,6 +522,6 @@ behavior by adding custom FETCH() and STORE() methods, if this is desired. package main; use Fcntl qw( O_RDWR O_CREAT ); - tie %foo, Mydbm, "adbm", O_RDWR|O_CREAT, 0640; + tie %foo, "Mydbm", "adbm", O_RDWR|O_CREAT, 0640; $foo{'bar'} = 123; print "foo-bar = $foo{'bar'}\n";