You are viewing the version of this documentation from Perl 5.6.2. View the latest version

CONTENTS

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 that 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";