Perl OOP

Summary: in this tutorial, you will learn about Perl Object-Oriented Programming or Perl OOP. You will learn how to create a simple Perl class and use it in other programs.

Besides procedural programming, Perl also provides you with object-orient programming paradigm.

Object-Oriented Programming Concepts

If you are not familiar with object-oriented programming, the following are the fundamental concepts to help you get started.

Object

An object is a single entity that combines both data and code.  Object is described in the following terms:

  • Actions or methods describe what it can do. It is the code part of the object.
  • Attributes or properties describe what information the object conveys. It is the data part of the object.

An object can be anything tangible or intangible. A phone is a tangible object that has attributes: serial, name, price, etc. You can change one of this information through methods that the phone object provides e.g.,  set_name, get_name, etc.

Class

A class is a blueprint or template of similar objects. A house is an object but the house blueprint is a class. From the house blueprint you can build as many houses as you want. We often say an object is an instance of a class e.g., a phone is an instance of Product class.

We typically use UML to model class including its attributes and methods. The following is the Product class in UML diagram.

Perl OOP Tutorial

Encapsulation

Through object, you can hide its complexity which is known as abstraction or encapsulation in object oriented programming. It means the client of the object does not need to care about the internal logic of the object but still can use the object through its interfaces (methods).

Inheritance

Inheritance means one class inherits both attributes and methods from another class. Inheritance enables you to reuse and extend existing classes without copy-n-paste the code or re-invent the wheel.

  • A class that other classes inherit from is called base class or superclass.
  • A class that inherits from other class called subclass or derived class.

Polymorphism

Polymorphism means “many forms” in Greek. It means a method call can behave differently depending on the type of the object that calls it.

Perl OOP rules

There are three important rules in Perl object oriented programming:

Defining the first Perl class

We are going to define the Product class in Perl. With rule #1: a class is a package so here is the definition of the Product class in Product.pm file:

package Product;

use strict;
use warnings;

sub new{
}

1;Code language: Perl (perl)

In Perl, we use a subroutine or method named  new() to construct the object. The subroutine name is not mandatory so you can use any subroutine name you want, but be consistent.

Let’s add some attributes to the Product class.

# init product with serial, name and price
sub new{
    my ($class,$args) = @_;
    my $self = bless { serial => $args->{serial},
                       name => $args->{name}, 
                       price => $args->{price}
                     }, $class;
}Code language: PHP (php)

Whenever you call the  new() method, Perl automatically passes the class name Product as the first argument to the special array @_.

When you create an object, you actually create a reference that knows what class it belongs to. The built-in function bless is used to bless the reference to the class and return an instance of the class.

The following illustrates the syntax of the  bless() function:

object = bless reference, classname;

We’ve passed a hash reference to the bless() function. You can pass any kind of reference to the bless function e.g., array reference, however,  it is much easier to work with a hash reference.

We are ready to use the Product class in other programs.

Let’s create a new program that uses the Product class.

#!/usr/bin/perl
use strict;
use warnings;

use Product;

my $iphone = Product->new({
                          serial =>"100",
                          name => "iPhone 5", 
                  price => 650.00});Code language: Perl (perl)

We called the method new() of the Product class and get an object $iphone. We passed a hash reference to the  new() method containing serial, name and price.

Let’s add some methods which are known as getters/setters to manipulate attributes of the Product class.

The following illustrates the complete Product class.

package Product;

use strict;
use warnings;

# init product with serial, name and price
sub new{
    my ($class,$args) = @_;
    my $self = bless { serial => $args->{serial},
                       name => $args->{name}, 
                       price => $args->{price}
                     }, $class;
}
# get name of the product
sub get_name{
   my $self = shift;
   return $self->{name};
}

# set new name for the product
sub set_name{
   my ($self,$new_name) = @_;
   $self->{name} = $new_name;
}

# get price of the product
sub get_price{
   my $self = shift;
   return $self->{price};
}

# set price for the product
sub set_price{
   my ($self,$new_price) = @_;
   $self->{price} = $new_price;
}
# get serial
sub get_serial{
   my $self = shift;
   return $self->{serial};   
}
# set serial
sub set_serial{
   my ($self,$new_price) = @_;
   $self->{price} = $new_price;
}
# return formatted string of the product
sub to_string{
   my $self = shift;
   return "Serial: $self->{serial}\nName: $self->{name}\nPrice: $self->{price}USD\n";
}

1;Code language: Perl (perl)

The following program illustrates how to use the Product class:

#!/usr/bin/perl
use strict;
use warnings;

use Product;

my $iphone = Product->new({
                          serial =>"100",
                          name => "iPhone", 
		          price => 650.00});
my $nexus = Product->new({ serial =>"101",
			   name => "Nexus",
                           price => 299.00});

print $iphone->to_string();
print $nexus->to_string();Code language: Perl (perl)

The output of the program is as follows:

Perl OOP Output

In this tutorial, we’ve shown you how to use Perl object-oriented feature. It is quite simple. We hope with this short tutorial, you can get the idea and explore Perl OOP further.

Was this tutorial helpful ?