Perl for Newbies - Part 3 - Modules and ObjectsObjects in PerlMaking Your Own ObjectsThe Constructor

4.3.1. The Constructor

Here is an example, constructor for the class Foo:

package Foo;

use strict;
use warnings;

sub new
    # Retrieve the package's string.
    # It is not necessarily Foo, because this constructor may be
    # called from a class that inherits Foo.
    my $class = shift;

    # $self is the the object. Let's initialize it to an empty hash
    # reference.
    my $self = {};

    # Associate $self with the class $class. This is probably the most
    # important step.
    bless $self, $class;

    # Now we can retrieve the other arguments passed to the
    # constructor.

    my $name = shift || "Fooish";
    my $number = shift || 5;

    # Put these arguments inside class members
    $self->{'name'} = $name;
    $self->{'number'} = $number;

    # Return $self so the user can use it.
    return $self;


And here is an example script that uses this constructor:


use strict;
use warnings;

use Foo;

my $foo = Foo->new("MyFoo", 500);

print $foo->{'name'}, "\n";

Written by Shlomi Fish