next up previous contents
Next: Static Versus Regular Methods Up: Objects in Perl Previous: Polymorphism

One Class Can Contain Another

Now that you have seen several objects in action, you probably realize that some class properties will be objects themselves. For example, you might have a billing object that contains an inventory object, or you might use a car object inside a warehouse object. The possibilities are endless.

The next example shows how to add a color object to the inventory system you've been building. It also shows you that Perl will execute statements that are not part of a function -- even those in packages other than main-as soon as they are seen by the interpreter.

The basic outline of the program is as follows:

The code is as follows:

package Inventory_item;

    sub new {

        my($class)  = shift;

        my(%params) = @_;

        bless {

            "PART_NUM"    => $params{"PART_NUM"},

            "QTY_ON_HAND" => $params{"QTY_ON_HAND"}

        }, $class;


package Pen;

    @ISA = (Inventory_item);

    sub new {

        my($class) = shift;

        my(%params) = @_;

        my($self) = Inventory_item->new(@_);

        $self->{"INK_COLOR"} = Color->new($params{"INK_COLOR"});

        return(bless($self, $class));


package Color;

    print("Executing Color statements\n");

    $colors{"blue"}  = "Die Lot 13";

    $colors{"red"}   = "Die Lot 5";

    sub new {

        my($class) = shift;

        my($param) = @_;

        my($self) = \$colors{$param};

        return(bless($self, $class));


package main;

    print("Executing main statements\n");

    $pen = Pen->new(

        "PART_NUM"    => "12A-34",

        "QTY_ON_HAND" => 34,

        "INK_COLOR"   => "blue");

    %properties = %{$pen};

    print("The part number is " . $properties{'PART_NUM'}     . "\n");

    print("The quantity is "    . $properties{'QTY_ON_HAND'}  . "\n");

    print("The ink color is "   . ${$properties{'INK_COLOR'}} . "\n");

This program displays:

Executing Color statements
Executing main statements
The part number is 12A-34
The quantity is 34
The ink color is Die Lot 13

This is faitly involved so where to start? You already know about the Inventory_item class and the @ISA array. Let's look at the assignment to the INK_COLOR entry of the Pen class. This line,

$self->{t;{"INK_COLOR"} = Color->new($params{"INK_COLOR"});,

is used to call the constructor for the Color class.

The expression $params{"INK_COLOR"} passes the value of "blue" to the Color constructor, which returns a reference to one of the colors in the %colors associative array.

You can tell that Perl executes all statements that are not inside functions because the print statement in the Color package is executed before the print statement in the main package. This is why you can define hash entries inside the Color class. When variables are defined inside a package but outside a function, they are called static variables. You can access one of the hash entries in the Color package like this: $Color::colors{"blue"}.

next up previous contents
Next: Static Versus Regular Methods Up: Objects in Perl Previous: Polymorphism