Defining Packages

Syntax for defining packages

We define our own package by starting with the keyword package followed by the name you want to give to your new package. It should end with the ; sign. We use 1; at the end of the package definition to mark the end of the scope.

Here’s the general syntax:

Press + to interact
package Person;
1;

Example snippet

Here’s an example snippet of a package named Shape.

Press + to interact
package Shape;
sub new { # constructor
my $class = shift;
my $self = {
# member variables
name => shift,
sides => shift,
};
bless $self, $class;
return $self;
}
1;

Explanation

Line 3:

  • The sub new method here is an initializer subroutine to hold the member variables. We have defined this because we can only initialize member variables if we define an initializer subroutine first.

Line 4:

  • The my $class = shift; statement stores the class name Shape in a local scalar variable called $class, using shift.

Line 5-9:

  • The my $self here is a hash which is used to store key/value pairs of the passed member variables sides and name.

Line 11:

  • The bless $self, $class; turns the hash reference stored in $self into a Shape object by “blessing” it into the Shape class.

Line 12:

  • return $self; statement returns the newly created object.

Adding methods to a package

Press + to interact
package Shape;
sub new { # constructor
my $class = shift;
my $self = {
# member variables
name => shift,
sides => shift,
};
bless $self, $class;
return $self;
}
sub Description {
my ($self) = @_;
print "A $self->{name} has $self->{sides} sides.";
}
1;

Explanation

We have declared a method named Description, which prints the name and sides of the Shape. To access the object for which this method is called, we use $self and point it to the respective object using @_.

Creating the object

Once the class Shape is defined, you can create an object of the Shape class. For example, an instance of a Shape, say, a Square would be an object and so would be the other instances of the class Shape like Circles or Triangles, etc. Hence, you can have multiple instances of a package, just like you can have multiple Shapes.

Here’s the basic syntax of creating an object of a class:

Press + to interact
$objectName = new ClassName();

Here, we use the new keyword to create a new instance of the class.

In the case of our Shape class example, this is how we’d make an object:

Press + to interact
package Shape;
sub new { # constructor
my $class = shift;
my $self = {
# member variables
name => shift,
sides => shift,
};
bless $self, $class;
return $self;
}
sub Description {
my ($self) = @_;
print "A $self->{name} has $self->{sides} sides.";
}
1;
$Square = new Shape("square", 4);
$Square->Description();

Creating multiple objects of a class

We can create two more objects of the same class, i.e., a hexagon and a triangle:

Press + to interact
package Shape;
sub new { # constructor
my $class = shift;
my $self = {
# member variables
name => shift,
sides => shift,
};
bless $self, $class;
return $self;
}
sub Description {
my ($self) = @_;
print "A $self->{name} has $self->{sides} sides.\n";
}
1;
$Square = new Shape("Square", 4);
$Square->Description();
$Triangle = new Shape("Triangle", 3);
$Triangle->Description();
$Hexagon = new Shape("Hexagon", 6);
$Hexagon->Description();

Let’s look at the illustration for a better understanding: