Perl - OOP - Creating classes with methods

As every language, OOP is a plus. And who says OOP, says classes and methods.

Perl is able to manage such of things. That's what we are going to see in this Perl tutorial.

Let's create classes and their methods.

Explanation

Our Perl program will show us who is the boss and who is the employee. And who the boss is hiring.

In Perl, a class is called a Package.
The name of the package is the same as the file name.
Its extension is ".pm".

Every class has to finish by "1;" because the Perl compiler need it.

Every method has to return something. In our examples we don't need to return a number nor a string, but the compiler need a return function.
So we are doing it, but just with "return;".

Of course if you need to return a number or a string, just do it, it works as well.

We can include another class with the use keyword. For example, if we need to include the Employee class we do:

use Employee;

To create a method, we have to use the sub keyword.

To create a constructor, we could use any word, but by convention, we called it "new".
Thus a minimal constructor must have:

  1. a list of parameters to retrieve (the first one is the class, by convention we called it $class):
    my($class, $myName, $myCountry)=@_;
    
  2. an empty variable named, by convention, $this (our instance): 
    my $this={};
  3. a native function called bless() to link the instance and the class:
    bless($this, $class);
  4. a "return $this;" at the end of the code:
    return $this;

But if we want to catch arguments from the constructor, we have to use: $this->{property} = $parameter.
Something like this:

    $this->{name}   = $myName || $nameDefault;
    $this->{country}= $myCountry || $countryDefault;
    $this->{number} = ++$number;

In this code, we retrieve the $myName argument from those given to the constructor during the creation of the instance (in the main.pl file).

So the constructor of our Boss class will be as this:

sub new {
    my($class, $myName, $myCountry)=@_;
    my $this={};
    bless($this, $class);
    $this->{name}    = $myName || $nameDefault;
    $this->{country}= $myCountry || $countryDefault;
    $this->{number}    = ++$number;
    return $this;
}

Let's create these classes (or packages).

Creating classes and methods

Boss.pm

package Boss;

use Employee;

use strict;
use warnings;

my $nameDefault="Unknown";
my $countryDefault="Somewhere";
my $number=0;

sub new {
    my($class, $myName, $myCountry)=@_;
    my $this={};
    bless($this, $class);
    $this->{name}    = $myName || $nameDefault;
    $this->{country}= $myCountry || $countryDefault;
    $this->{number}    = ++$number;
    return $this;
}

sub displayName {
    my($this)=@_;
    print("The name of the boss $this->{number} is $this->{name}.\n");
    print("He/She's from $this->{country}.\n\n");
    return;
}

sub hireEmployee {
    my($this, $employee)=@_;
    print("$this->{name} is hiring $employee->{name}.\n");
    return;
}

1;

Employee.pm

package Employee;

use strict;
use warnings;

my $nameDefault="Unknown";
my $countryDefault="Somewhere";
my $number=0;

sub new {
    my($class, $myName, $myCountry)=@_;
    my $this={};
    bless($this, $class);
    $this->{name}    = $myName || $nameDefault;
    $this->{country}= $myCountry || $countryDefault;
    $this->{number}    = ++$number;
    return $this;
}

sub displayName {
    my($this)=@_;
    print("The name of the employee $this->{number} is $this->{name}.\n");
    print("He/She's from $this->{country}.\n\n");
    return;
}

1;

main.pl

use strict;
use warnings;

use Boss;
use Employee;

# Declaring and initializing variables
my $boss1 = Boss->new();
my $employee1 = Employee->new();

my $boss2 = Boss->new("Kimiko", "Japan");
my $employee2 = Employee->new("Richard", "England");

# Calling of classe's methods on correct objects
$boss1->displayName();
$employee1->displayName();

$boss2->displayName();
$employee2->displayName();

$boss2->hireEmployee($employee2);

Execution

perl main.pl

Output

The name of the boss 1 is Unknown.
He/She's from Somewhere.

The name of the employee 1 is Unknown.
He/She's from Somewhere.

The name of the boss 2 is Kimiko.
He/She's from Japan.

The name of the employee 2 is Richard.
He/She's from England.

Kimiko is hiring Richard.

Finally

You've just created your first OOP Perl program.
Well done, you rock! cool

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.