Monday, September 7, 2015

Object Oriented Perl

    All about OO in Perl:
    • Perl's built-in OO system is very minimal. 
    • Object oriented Perl is all about "method invocation (using '->' operator)"
    • Perl's OO system is class-based, which is fairly common.
    • In Perl, any package can be a class. The difference between a package which is a class and one which isn't is based on how the package is used.
    • An object represents a single discrete thing.
    • A class defines the behavior of a category of objects.
    • All objects belong to a specific class.
    • When we want to create a specific object, we start with its class, and construct or instantiate an object.  


    • most Perl objects are hashes, but an object can be an instance of any Perl data type (scalar, array, etc.).
    • Turning a plain data structure into an object is done by blessing that data structure using Perl's bless function.
    • we sometimes say that an object has been "blessed into a class".  

    In Perl, constructor is just another method. new is used mostly as constructor name, but any other name can also be used.

    Class-based OO system terminology:

    Object (Instance of a class)        = Data + Subroutine
    Attribute    = Data
    Method      = Subroutine

    Suppose there is a package "File". And there is subroutine in this package called "getname".

    If we are not using OO feature, then we have to call subroutine as:

    use File;

    If we are using OO feature, then File->getname(), invokes subroutine method in subroutine "File". Thus "getname "is object oriented version of  subroutine:

    use File;
    And internally it is always invoked in the form of:


    And if there are some arguments, then:

     Class::method('Class', \%args);

    That's why, we write the methods as follows;

    Constructor example:

    Suppose there is a class or package and the constructor of this class is:

    sub new
        my $class = shift;
        my $self = {
                sel                       => undef,
                JT                        => undef,
                DSID_CHECK   => 1,
                proxy                  => undef,
                capabilities         => undef,
        bless( $self, $class );
        return $self;

    Now we create an object of Browser as follows:

    use Browser;
    my $objBrowser = Browser->new();

    Now suppose there is one method login() in and in other file we call it as:


    Now internally, it will be called as:

    Browser::login($objBrowser, \%args);  #object is passed as first argument

    So we have to write the login method in as follows:

    sub login
          my $self = shift;
          my $args = shift;

    So we can define OO in perl as following simplae points:

    1. A call like $obj->method(\%args) is internally called as: 
           $obj::method($obj, \%args)
    2. bless $self, $class.
        "bless", blesses $self into object of class $class, and returns $self.
        $self contains reference data of object.

    The above 2 points will make OO understanding easy.

    No comments:

    Post a Comment