发信人: saka.bbs@bbs.neu.edu.cn (机器猫), 信区: cnlinux
标  题: perl(3)
发信站: 白山黑水BBS (Tue Apr  1 11:17:00 1997)
转信站: Lilac!ustcnews!ustcnews!sjtunews!neubbs
出  处: conger.neu.edu.cn

--------------624A15C24856
Content-Type: text/plain; charset=us-ascii; name="perlbot.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="perlbot.txt"

NAME

perlbot - Bag'o Object Tricks (the BOT)

---------------------------------------------------------------------------

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
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.

The Perl motto still holds: There's more than one way to do it.

---------------------------------------------------------------------------

OO SCALING TIPS

  1. Do not attempt to verify the type of $self. That'll break if the class
     is inherited, when the type of $self is valid but its package isn't
     what you expect. See rule 5.

  2. If an object-oriented (OO) or indirect-object (IO) syntax was used,
     then the object is probably the correct type and there's no need to
     become paranoid about it. Perl isn't a paranoid language anyway. If
     people subvert the OO or IO syntax then they probably know what
     they're doing and you should let them do it. See rule 1.

  3. Use the two-argument form of bless() . Let a subclass use your
     constructor. See INHERITING A CONSTRUCTOR .

  4. The subclass is allowed to know things about its immediate superclass,
     the superclass is allowed to know nothing about a subclass.

  5. Don't be trigger happy with inheritance. A ``using'', ``containing'',
     or ``delegation'' relationship (some sort of aggregation, at least) is
     often more appropriate. See OBJECT RELATIONSHIPS , USING RELATIONSHIP
     WITH SDBM , and ``DELEGATION''.

  6. The object is the namespace. Make package globals accessible via the
     object. This will remove the guess work about the symbol's home
     package. See CLASS CONTEXT AND THE OBJECT .

  7. IO syntax is certainly less noisy, but it is also prone to ambiguities
     which can cause difficult-to-find bugs. Allow people to use the
     sure-thing OO syntax, even if you don't like it.

  8. Do not use function-call syntax on a method. You're going to be bitten
     someday. Someone might move that method into a superclass and your
     code will be broken. On top of that you're feeding the paranoia in
     rule 2.

  9. Don't assume you know the home package of a method. You're making it
     difficult for someone to override that method. See THINKING OF CODE
     REUSE .

..

---------------------------------------------------------------------------

INSTANCE VARIABLES

An anonymous array or anonymous hash can be used to hold instance
variables. Named parameters are also demonstrated.

        package Foo;
        sub new {
                my $type = shift;
                my %params = @_;
                my $self = {};
                $self->{'High'} = $params{'High'};
                $self->{'Low'}  = $params{'Low'};
                bless $self, $type;
        }
        package Bar;
        sub new {
                my $type = shift;
                my %params = @_;
                my $self = [];
                $self->[0] = $params{'Left'};
                $self->[1] = $params{'Right'};
                bless $self, $type;
        }
        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";

---------------------------------------------------------------------------

SCALAR INSTANCE VARIABLES

An anonymous scalar can be used when only one instance variable is needed.

        package Foo;
        sub new {
                my $type = shift;
                my $self;
                $self = shift;
                bless \$self, $type;
        }
        package main;
        $a = Foo->new( 42 );
        print "a=$$a\n";

---------------------------------------------------------------------------

INSTANCE VARIABLE INHERITANCE

This example demonstrates how one might inherit instance variables from a
superclass for inclusion in the new class. This requires calling the
superclass's constructor and adding one's own instance variables to the new
object.

        package Bar;
        sub new {
                my $type = shift;
                my $self = {};
                $self->{'buz'} = 42;
                bless $self, $type;
        }
        package Foo;
        @ISA = qw( Bar );
        sub new {
                my $type = shift;
                my $self = Bar->new;
                $self->{'biz'} = 11;
                bless $self, $type;
        }
        package main;
        $a = Foo->new;
        print "buz = ", $a->{'buz'}, "\n";
        print "biz = ", $a->{'biz'}, "\n";

---------------------------------------------------------------------------

OBJECT RELATIONSHIPS

The following demonstrates how one might implement ``containing'' and
``using'' relationships between objects.

        package Bar;
        sub new {
                my $type = shift;
                my $self = {};
                $self->{'buz'} = 42;
                bless $self, $type;
        }
        package Foo;
        sub new {
                my $type = shift;
                my $self = {};
                $self->{'Bar'} = Bar->new;
                $self->{'biz'} = 11;
                bless $self, $type;
        }
        package main;
        $a = Foo->new;
        print "buz = ", $a->{'Bar'}->{'buz'}, "\n";
        print "biz = ", $a->{'biz'}, "\n";

---------------------------------------------------------------------------

OVERRIDING SUPERCLASS METHODS

The following example demonstrates how to override a superclass method and
then call the overridden method. The SUPER 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" }
        package Bar; @ISA = qw( Buz );
        sub google { print "google here\n" }
        package Baz;
        sub mumble { print "mumbling\n" }
        package Foo;
        @ISA = qw( Bar Baz );
        sub new {
                my $type = shift;
                bless [], $type;
        }
        sub grr { print "grumble\n" }
        sub goo {
                my $self = shift;
                $self->SUPER::goo();
        }
        sub mumble {
                my $self = shift;
                $self->SUPER::mum

--------------624A15C24856--

--
※ 来源:.白山黑水站 bbs.neu.edu.cn.[FROM: ygh@rose.dlut.edu.cn]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.247毫秒