home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Perl CookbookPerl CookbookSearch this book

13.1. Constructing an Object

13.1.3. 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 bless has been called on it. The simplest possible constructor, although not particularly useful, is the following:

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 bless, which always blesses the object into the current package. This means it can't be usefully inherited from; objects it constructs will always be blessed into the class that the new function was compiled into. With inheritance, this is not necessarily the class on whose behalf the method was invoked.

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 bless:

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}  = time( );       # init data fields
    $self->{AGE}    = 0;
    return $self;                     # And give it back
}

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 can sometimes make inheritance easier by separating memory allocation from initialization; see Recipe 13.11.

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 invoked with arguments, _init
# interprets them as key+value pairs to initialize the object with.
sub _init {
    my $self = shift;
    $self->{START} = time( );
    $self->{AGE}   = 0;
    if (@_) {
        my %extra = @_;
        @$self{keys %extra} = values %extra;
    }
}


Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.