home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl501m.zip / pod / perlbot.pod < prev    next >
Text File  |  1995-07-03  |  12KB  |  530 lines

  1. =head1 NAME
  2.  
  3. perlbot - Bag'o Object Tricks (the BOT)
  4.  
  5. =head1 INTRODUCTION
  6.  
  7. The following collection of tricks and hints is intended to whet curious
  8. appetites about such things as the use of instance variables and the
  9. mechanics of object and class relationships.  The reader is encouraged to
  10. consult relevant textbooks for discussion of Object Oriented definitions and
  11. methodology.  This is not intended as a tutorial for object-oriented
  12. programming or as a comprehensive guide to Perl's object oriented features,
  13. nor should it be construed as a style guide.
  14.  
  15. The Perl motto still holds:  There's more than one way to do it.
  16.  
  17. =head1 OO SCALING TIPS
  18.  
  19. =over 5
  20.  
  21. =item 1
  22.  
  23. Do not attempt to verify the type of $self.  That'll break if the class is
  24. inherited, when the type of $self is valid but its package isn't what you
  25. expect.  See rule 5.
  26.  
  27. =item 2
  28.  
  29. If an object-oriented (OO) or indirect-object (IO) syntax was used, then the
  30. object is probably the correct type and there's no need to become paranoid
  31. about it.  Perl isn't a paranoid language anyway.  If people subvert the OO
  32. or IO syntax then they probably know what they're doing and you should let
  33. them do it.  See rule 1.
  34.  
  35. =item 3
  36.  
  37. Use the two-argument form of bless().  Let a subclass use your constructor.
  38. See L<INHERITING A CONSTRUCTOR>.
  39.  
  40. =item 4
  41.  
  42. The subclass is allowed to know things about its immediate superclass, the
  43. superclass is allowed to know nothing about a subclass.
  44.  
  45. =item 5
  46.  
  47. Don't be trigger happy with inheritance.  A "using", "containing", or
  48. "delegation" relationship (some sort of aggregation, at least) is often more
  49. appropriate.  See L<OBJECT RELATIONSHIPS>, L<USING RELATIONSHIP WITH SDBM>,
  50. and L<"DELEGATION">.
  51.  
  52. =item 6
  53.  
  54. The object is the namespace.  Make package globals accessible via the
  55. object.  This will remove the guess work about the symbol's home package.
  56. See L<CLASS CONTEXT AND THE OBJECT>.
  57.  
  58. =item 7
  59.  
  60. IO syntax is certainly less noisy, but it is also prone to ambiguities which
  61. can cause difficult-to-find bugs.  Allow people to use the sure-thing OO
  62. syntax, even if you don't like it.
  63.  
  64. =item 8
  65.  
  66. Do not use function-call syntax on a method.  You're going to be bitten
  67. someday.  Someone might move that method into a superclass and your code
  68. will be broken.  On top of that you're feeding the paranoia in rule 2.
  69.  
  70. =item 9
  71.  
  72. Don't assume you know the home package of a method.  You're making it
  73. difficult for someone to override that method.  See L<THINKING OF CODE REUSE>.
  74.  
  75. =back
  76.  
  77. =head1 INSTANCE VARIABLES
  78.  
  79. An anonymous array or anonymous hash can be used to hold instance
  80. variables.  Named parameters are also demonstrated.
  81.  
  82.     package Foo;
  83.  
  84.     sub new {
  85.         my $type = shift;
  86.         my %params = @_;
  87.         my $self = {};
  88.         $self->{'High'} = $params{'High'};
  89.         $self->{'Low'}  = $params{'Low'};
  90.         bless $self, $type;
  91.     }
  92.  
  93.  
  94.     package Bar;
  95.  
  96.     sub new {
  97.         my $type = shift;
  98.         my %params = @_;
  99.         my $self = [];
  100.         $self->[0] = $params{'Left'};
  101.         $self->[1] = $params{'Right'};
  102.         bless $self, $type;
  103.     }
  104.  
  105.     package main;
  106.  
  107.     $a = Foo->new( 'High' => 42, 'Low' => 11 );
  108.     print "High=$a->{'High'}\n";
  109.     print "Low=$a->{'Low'}\n";
  110.  
  111.     $b = Bar->new( 'Left' => 78, 'Right' => 40 );
  112.     print "Left=$b->[0]\n";
  113.     print "Right=$b->[1]\n";
  114.  
  115. =head1 SCALAR INSTANCE VARIABLES
  116.  
  117. An anonymous scalar can be used when only one instance variable is needed.
  118.  
  119.     package Foo;
  120.  
  121.     sub new {
  122.         my $type = shift;
  123.         my $self;
  124.         $self = shift;
  125.         bless \$self, $type;
  126.     }
  127.  
  128.     package main;
  129.  
  130.     $a = Foo->new( 42 );
  131.     print "a=$$a\n";
  132.  
  133.  
  134. =head1 INSTANCE VARIABLE INHERITANCE
  135.  
  136. This example demonstrates how one might inherit instance variables from a
  137. superclass for inclusion in the new class.  This requires calling the
  138. superclass's constructor and adding one's own instance variables to the new
  139. object.
  140.  
  141.     package Bar;
  142.  
  143.     sub new {
  144.         my $type = shift;
  145.         my $self = {};
  146.         $self->{'buz'} = 42;
  147.         bless $self, $type;
  148.     }
  149.  
  150.     package Foo;
  151.     @ISA = qw( Bar );
  152.  
  153.     sub new {
  154.         my $type = shift;
  155.         my $self = Bar->new;
  156.         $self->{'biz'} = 11;
  157.         bless $self, $type;
  158.     }
  159.  
  160.     package main;
  161.  
  162.     $a = Foo->new;
  163.     print "buz = ", $a->{'buz'}, "\n";
  164.     print "biz = ", $a->{'biz'}, "\n";
  165.  
  166.  
  167.  
  168. =head1 OBJECT RELATIONSHIPS
  169.  
  170. The following demonstrates how one might implement "containing" and "using"
  171. relationships between objects.
  172.  
  173.     package Bar;
  174.  
  175.     sub new {
  176.         my $type = shift;
  177.         my $self = {};
  178.         $self->{'buz'} = 42;
  179.         bless $self, $type;
  180.     }
  181.  
  182.     package Foo;
  183.  
  184.     sub new {
  185.         my $type = shift;
  186.         my $self = {};
  187.         $self->{'Bar'} = Bar->new;
  188.         $self->{'biz'} = 11;
  189.         bless $self, $type;
  190.     }
  191.  
  192.     package main;
  193.  
  194.     $a = Foo->new;
  195.     print "buz = ", $a->{'Bar'}->{'buz'}, "\n";
  196.     print "biz = ", $a->{'biz'}, "\n";
  197.  
  198.  
  199.  
  200. =head1 OVERRIDING SUPERCLASS METHODS
  201.  
  202. The following example demonstrates how one might override a superclass
  203. method and then call the method after it has been overridden.  The
  204. Foo::Inherit class allows the programmer to call an overridden superclass
  205. method without actually knowing where that method is defined.
  206.  
  207.  
  208.     package Buz;
  209.     sub goo { print "here's the goo\n" }
  210.  
  211.     package Bar; @ISA = qw( Buz );
  212.     sub google { print "google here\n" }
  213.  
  214.     package Baz;
  215.     sub mumble { print "mumbling\n" }
  216.  
  217.     package Foo;
  218.     @ISA = qw( Bar Baz );
  219.     @Foo::Inherit::ISA = @ISA;  # Access to overridden methods.
  220.  
  221.     sub new {
  222.         my $type = shift;
  223.         bless [], $type;
  224.     }
  225.     sub grr { print "grumble\n" }
  226.     sub goo {
  227.         my $self = shift;
  228.         $self->Foo::Inherit::goo();
  229.     }
  230.     sub mumble {
  231.         my $self = shift;
  232.         $self->Foo::Inherit::mumble();
  233.     }
  234.     sub google {
  235.         my $self = shift;
  236.         $self->Foo::Inherit::google();
  237.     }
  238.  
  239.     package main;
  240.  
  241.     $foo = Foo->new;
  242.     $foo->mumble;
  243.     $foo->grr;
  244.     $foo->goo;
  245.     $foo->google;
  246.  
  247.  
  248. =head1 USING RELATIONSHIP WITH SDBM
  249.  
  250. This example demonstrates an interface for the SDBM class.  This creates a
  251. "using" relationship between the SDBM class and the new class Mydbm.
  252.  
  253.     package Mydbm;
  254.  
  255.     require SDBM_File;
  256.     require TieHash;
  257.     @ISA = qw( TieHash );
  258.  
  259.     sub TIEHASH {
  260.         my $type = shift;
  261.         my $ref  = SDBM_File->new(@_);
  262.         bless {'dbm' => $ref}, $type;
  263.     }
  264.     sub FETCH {
  265.         my $self = shift;
  266.         my $ref  = $self->{'dbm'};
  267.         $ref->FETCH(@_);
  268.     }
  269.     sub STORE {
  270.         my $self = shift; 
  271.         if (defined $_[0]){
  272.         my $ref = $self->{'dbm'};
  273.         $ref->STORE(@_);
  274.         } else {
  275.         die "Cannot STORE an undefined key in Mydbm\n";
  276.         }
  277.     }
  278.  
  279.     package main;
  280.     use Fcntl qw( O_RDWR O_CREAT );
  281.  
  282.     tie %foo, Mydbm, "Sdbm", O_RDWR|O_CREAT, 0640;
  283.     $foo{'bar'} = 123;
  284.     print "foo-bar = $foo{'bar'}\n";
  285.  
  286.     tie %bar, Mydbm, "Sdbm2", O_RDWR|O_CREAT, 0640;
  287.     $bar{'Cathy'} = 456;
  288.     print "bar-Cathy = $bar{'Cathy'}\n";
  289.  
  290. =head1 THINKING OF CODE REUSE
  291.  
  292. One strength of Object-Oriented languages is the ease with which old code
  293. can use new code.  The following examples will demonstrate first how one can
  294. hinder code reuse and then how one can promote code reuse.
  295.  
  296. This first example illustrates a class which uses a fully-qualified method
  297. call to access the "private" method BAZ().  The second example will show
  298. that it is impossible to override the BAZ() method.
  299.  
  300.     package FOO;
  301.  
  302.     sub new {
  303.         my $type = shift;
  304.         bless {}, $type;
  305.     }
  306.     sub bar {
  307.         my $self = shift;
  308.         $self->FOO::private::BAZ;
  309.     }
  310.  
  311.     package FOO::private;
  312.  
  313.     sub BAZ {
  314.         print "in BAZ\n";
  315.     }
  316.  
  317.     package main;
  318.  
  319.     $a = FOO->new;
  320.     $a->bar;
  321.  
  322. Now we try to override the BAZ() method.  We would like FOO::bar() to call
  323. GOOP::BAZ(), but this cannot happen because FOO::bar() explicitly calls
  324. FOO::private::BAZ().
  325.  
  326.     package FOO;
  327.  
  328.     sub new {
  329.         my $type = shift;
  330.         bless {}, $type;
  331.     }
  332.     sub bar {
  333.         my $self = shift;
  334.         $self->FOO::private::BAZ;
  335.     }
  336.  
  337.     package FOO::private;
  338.  
  339.     sub BAZ {
  340.         print "in BAZ\n";
  341.     }
  342.  
  343.     package GOOP;
  344.     @ISA = qw( FOO );
  345.     sub new {
  346.         my $type = shift;
  347.         bless {}, $type;
  348.     }
  349.  
  350.     sub BAZ {
  351.         print "in GOOP::BAZ\n";
  352.     }
  353.  
  354.     package main;
  355.  
  356.     $a = GOOP->new;
  357.     $a->bar;
  358.  
  359. To create reusable code we must modify class FOO, flattening class
  360. FOO::private.  The next example shows a reusable class FOO which allows the
  361. method GOOP::BAZ() to be used in place of FOO::BAZ().
  362.  
  363.     package FOO;
  364.  
  365.     sub new {
  366.         my $type = shift;
  367.         bless {}, $type;
  368.     }
  369.     sub bar {
  370.         my $self = shift;
  371.         $self->BAZ;
  372.     }
  373.  
  374.     sub BAZ {
  375.         print "in BAZ\n";
  376.     }
  377.  
  378.     package GOOP;
  379.     @ISA = qw( FOO );
  380.  
  381.     sub new {
  382.         my $type = shift;
  383.         bless {}, $type;
  384.     }
  385.     sub BAZ {
  386.         print "in GOOP::BAZ\n";
  387.     }
  388.  
  389.     package main;
  390.  
  391.     $a = GOOP->new;
  392.     $a->bar;
  393.  
  394. =head1 CLASS CONTEXT AND THE OBJECT
  395.  
  396. Use the object to solve package and class context problems.  Everything a
  397. method needs should be available via the object or should be passed as a
  398. parameter to the method.
  399.  
  400. A class will sometimes have static or global data to be used by the
  401. methods.  A subclass may want to override that data and replace it with new
  402. data.  When this happens the superclass may not know how to find the new
  403. copy of the data.
  404.  
  405. This problem can be solved by using the object to define the context of the
  406. method.  Let the method look in the object for a reference to the data.  The
  407. alternative is to force the method to go hunting for the data ("Is it in my
  408. class, or in a subclass?  Which subclass?"), and this can be inconvenient
  409. and will lead to hackery.  It is better to just let the object tell the
  410. method where that data is located.
  411.  
  412.     package Bar;
  413.  
  414.     %fizzle = ( 'Password' => 'XYZZY' );
  415.  
  416.     sub new {
  417.         my $type = shift;
  418.         my $self = {};
  419.         $self->{'fizzle'} = \%fizzle;
  420.         bless $self, $type;
  421.     }
  422.  
  423.     sub enter {
  424.         my $self = shift;
  425.     
  426.         # Don't try to guess if we should use %Bar::fizzle
  427.         # or %Foo::fizzle.  The object already knows which
  428.         # we should use, so just ask it.
  429.         #
  430.         my $fizzle = $self->{'fizzle'};
  431.  
  432.         print "The word is ", $fizzle->{'Password'}, "\n";
  433.     }
  434.  
  435.     package Foo;
  436.     @ISA = qw( Bar );
  437.  
  438.     %fizzle = ( 'Password' => 'Rumple' );
  439.  
  440.     sub new {
  441.         my $type = shift;
  442.         my $self = Bar->new;
  443.         $self->{'fizzle'} = \%fizzle;
  444.         bless $self, $type;
  445.     }
  446.  
  447.     package main;
  448.  
  449.     $a = Bar->new;
  450.     $b = Foo->new;
  451.     $a->enter;
  452.     $b->enter;
  453.  
  454. =head1 INHERITING A CONSTRUCTOR
  455.  
  456. An inheritable constructor should use the second form of bless() which allows
  457. blessing directly into a specified class.  Notice in this example that the
  458. object will be a BAR not a FOO, even though the constructor is in class FOO.
  459.  
  460.     package FOO;
  461.  
  462.     sub new {
  463.         my $type = shift;
  464.         my $self = {};
  465.         bless $self, $type;
  466.     }
  467.  
  468.     sub baz {
  469.         print "in FOO::baz()\n";
  470.     }
  471.  
  472.     package BAR;
  473.     @ISA = qw(FOO);
  474.  
  475.     sub baz {
  476.         print "in BAR::baz()\n";
  477.     }
  478.  
  479.     package main;
  480.  
  481.     $a = BAR->new;
  482.     $a->baz;
  483.  
  484. =head1 DELEGATION
  485.  
  486. Some classes, such as SDBM_File, cannot be effectively subclassed because
  487. they create foreign objects.  Such a class can be extended with some sort of
  488. aggregation technique such as the "using" relationship mentioned earlier or
  489. by delegation.
  490.  
  491. The following example demonstrates delegation using an AUTOLOAD() function to
  492. perform message-forwarding.  This will allow the Mydbm object to behave
  493. exactly like an SDBM_File object.  The Mydbm class could now extend the
  494. behavior by adding custom FETCH() and STORE() methods, if this is desired.
  495.  
  496.     package Mydbm;
  497.  
  498.     require SDBM_File;
  499.     require TieHash;
  500.     @ISA = qw(TieHash);
  501.  
  502.     sub TIEHASH {
  503.         my $type = shift;
  504.         my $ref = SDBM_File->new(@_);
  505.         bless {'delegate' => $ref};
  506.     }
  507.  
  508.     sub AUTOLOAD {
  509.         my $self = shift;
  510.  
  511.         # The Perl interpreter places the name of the
  512.         # message in a variable called $AUTOLOAD.
  513.  
  514.         # DESTROY messages should never be propagated.
  515.         return if $AUTOLOAD =~ /::DESTROY$/;
  516.  
  517.         # Remove the package name.
  518.         $AUTOLOAD =~ s/^Mydbm:://;
  519.  
  520.         # Pass the message to the delegate.
  521.         $self->{'delegate'}->$AUTOLOAD(@_);
  522.     }
  523.  
  524.     package main;
  525.     use Fcntl qw( O_RDWR O_CREAT );
  526.  
  527.     tie %foo, Mydbm, "adbm", O_RDWR|O_CREAT, 0640;
  528.     $foo{'bar'} = 123;
  529.     print "foo-bar = $foo{'bar'}\n";
  530.