Showing posts with label class. Show all posts
Showing posts with label class. Show all posts

Jun 28, 2013

Package or Class Example in Perl

Packages or Classes in Perl (OOPS Concept)

Definition :
In Perl, a class is corresponds to a Package.
To create a class in Perl, we first build a package.
A package is a self-contained unit of user-defined variables and subroutines, which can be re-used over and over again.
They provide a separate namespace within a Perl program that keeps subroutines and variables from conflicting with those in other packages.

Rules to be followed for a module in Perl :
The module in perl in general terms means a namespace defined in a file. Certain modules are only collections of function. In perl the modules must follow the following guidelines:
- The file name of a module must the same as the package name.
- The general naming convention of naming a package is to begin with a capital letter, but not mandatory always.
- All the file name should have the extension of "pm".
- Last line of a package is 1 i.e., returning always 1 as mentioned in the example below.
- In case no object oriented technique is used the package should be derived from the Exporter class.
- Also if no object oriented techniques are used the module should export its functions and variables to the main namespace using the @EXPORT and @EXPOR_OK arrays.
The use directive is used to load the modules.

Creating/Defining Object :
To create an instance of a class (an object) we need an object constructor.
This constructor is a method defined within the package.

What is Bless : 
You create a hash object and bless the hash object to who ever calling the constructor (new method) of a package/class.

E.g.,
Blessing an object based on the parameters passed to the constructor method
my $self = {
        _firstName => shift,
        _lastName  => shift,
        _ssn       => shift,
    };
 bless $self, $class;

Blessing an empty object is also possible
my $self = {};
bless $self , $class

E.g.,
Package Person;
sub new {
    my $class = shift;
    my $self = {
        _firstName => shift,
        _lastName  => shift,
        _ssn       => shift,
    };
    bless $self, $class;
    return $self;
}
1;

Creating Instance of Object :
In order to access package level methods, you need to create object of that class and then start accessing package level variables/methods.

my $object = new Person( "David", "Johnson", 23234345);
In the above example, we are creating object of Person from main.pl, here we are passing required parameters and the constructor (new method) of Person class will return a formatted hash object.

You can even bless an empty hash object as well.        
In case of empty object, the following returns empty object
my $object = new Person();

E.g.,
use Person;
use Data::Dumper;
my $object = new Person( "David", "Johnson", 23234345);

print "\n Person Object is : " . Dumper($object);

Information Hiding : 
This should not allow the users from modifying the object data.
You should allow the users to access the core object using setter/getter methods, there by achieving hiding object data from outside the world.

Let's see the below example for better understanding :
Support.pm has defined some getter(setFirstName) and setter(getFirstName) for accessing firstName
main.pl is accessing the firstName using getter(setFirstName) and setter(getFirstName)

support.pm

#!/usr/bin/perl

package support;

use strict;
use warnings;
use Data::Dumper;

sub new
{
    my $class = shift;
    print "\nClass Name : " . $class;
    my $self = {
        _firstName => shift,
        _lastName  => shift,
        _ssn       => shift,
    };
    # Print all the values just for clarification.
    print "\nFirst Name is $self->{_firstName}";
    print "\nLast Name is $self->{_lastName}";
    print "\nSSN is $self->{_ssn}";
    bless $self, $class;
    return $self;
}

sub setFirstName {
    my ( $self, $firstName ) = @_;
    $self->{_firstName} = $firstName if defined($firstName);
    return $self->{_firstName};
}

sub getFirstName {
    my( $self ) = @_;
    return $self->{_firstName};
}

1;
  

main.pl

#!/usr/bin/perl
use support;

use strict;
use warnings;
use Data::Dumper;

my $object = new support( "David", "Johnson", 23234345);

# Get first name which is set using constructor.
my $firstName = $object->getFirstName();

print "\n\nBefore Setting First Name is : $firstName";

# Now Set first name using helper function.
$object->setFirstName( "Mohd" );

# Now get first name set by helper function.
$firstName = $object->getFirstName();

print "\nAfter Setting First Name is : $firstName\n";  


Output :

Class Name : support
First Name is David
Last Name is Johnson
SSN is 23234345

Before Setting First Name is : David
After Setting First Name is : Mohd