home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / perlman / man / perlbot.txt < prev    next >
Encoding:
Text File  |  1999-09-09  |  16.4 KB  |  514 lines

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