Defining Packages
Let's get started with the basics of packages in this lesson.
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:
package Person;1;
Example snippet
Here’s an example snippet of a package named Shape
.
package Shape;sub new { # constructormy $class = shift;my $self = {# member variablesname => 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 nameShape
in a local scalar variable called$class
, usingshift
.
Line 5-9:
- The
my $self
here is a hash which is used to store key/value pairs of the passed member variablessides
andname
.
Line 11:
- The
bless $self, $class;
turns the hash reference stored in$self
into aShape
object by “blessing” it into theShape
class.
Line 12:
return $self;
statement returns the newly created object.
Adding methods to a package
package Shape;sub new { # constructormy $class = shift;my $self = {# member variablesname => 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:
$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:
package Shape;sub new { # constructormy $class = shift;my $self = {# member variablesname => 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:
package Shape;sub new { # constructormy $class = shift;my $self = {# member variablesname => 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: