13.1. Constructing an ObjectProblemYou want to create a way for your users to generate new objects. SolutionMake a constructor. In Perl, the constructor method must not only initialize its object, but must also first allocate memory for it, typically using an anonymous hash. C++ constructors, on the other hand, are called with memory already allocated. The rest of the object-oriented world would call C++'s constructors initializers . Here's the canonical object constructor in Perl:
sub new {
my $class = shift;
my $self = { };
bless($self, $class);
return $self;
}
This is the equivalent one-liner:
sub new { bless( { }, shift ) }
Discussion
Any method that allocates and initializes a new object acts as a constructor. The most important thing to remember is that a reference isn't an object until
sub new { bless({}) }
Let's add some initialization:
sub new {
my $self = { }; # allocate anonymous hash
bless($self);
# init two sample attributes/data members/fields
$self->{START} = time();
$self->{AGE} = 0;
return $self;
}
This constructor isn't very useful because it uses the single-argument form of
To solve this, have the constructor heed its first argument. For a class method, this is the package name. Pass this class name as the second argument to
sub new {
my $classname = shift; # What class are we constructing?
my $self = {}; # Allocate new memory
bless($self, $classname); # Mark it of the right type
$self->{START} =
Now the constructor can be correctly inherited by a derived class. You might also want to separate the memory allocation and blessing step from the instance data initialization step. Simple classes won't need this, but it makes inheritance easier; see Recipe 13.10 .
sub new {
my $classname = shift; # What class are we constructing?
my $self = {}; # Allocate new memory
bless($self, $classname); # Mark it of the right type
$self->_init(@_); # Call _init with remaining args
return $self;
}
# "private" method to initialize fields. It always sets START to
# the current time, and AGE to 0. If called with arguments, _init
# interprets them as key+value pairs to initialize the object with.
sub _init {
my $self = shift;
$self->{START} =
See Alsoperltoot (1) and perlobj (1); Chapter 5 of Programming Perl ; Recipe 13.6 ; Recipe 13.9 ; Recipe 13.10 ![]() Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|