Quantcast
Channel: perl – Coding School
Viewing all articles
Browse latest Browse all 15

Perl OOP

$
0
0

Perl OOP

Why Object Oriented approach?

A major factor in the invention of Object-Oriented approach is to remove some of the flaws encountered with the procedural approach. In OOP, data is treated as a critical element and does not allow it to flow freely. It bounds data closely to the functions that operate on it and protects it from accidental modification from outside functions. OOP allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects. A major advantage of OOP is code re-usability.

Some important features of Object Oriented programming are as follows:

  • Emphasis on data rather than procedure
  • Programs are divided into Objects
  • Data is hidden and cannot be accessed by external functions
  • Objects can communicate with each other through functions
  • New data and functions can be easily added whenever necessary
  • Follows bottom-up approach

Concepts of OOP:

  • Objects
  • Classes
  • Data Abstraction and Encapsulation
  • Inheritance
  • Polymorphism

    Briefly on Concepts:

    Objects

    Objects are the basic run-time entities in an object-oriented system. Programming problem is analyzed in terms of objects and nature of communication between them. When a program is executed, objects interact with each other by sending messages. Different objects can also interact with each other without knowing the details of their data or code.

    Classes

    A class is a collection of objects of similar type. Once a class is defined, any number of objects can be created which belong to that class.

    Data Abstraction and Encapsulation

    Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes.

    Storing data and functions in a single unit (class) is encapsulation. Data cannot be accessible to the outside world and only those functions which are stored in the class can access it.

    Inheritance

    Inheritance is the process by which objects can acquire the properties of objects of other class. In OOP, inheritance provides reusability, like, adding additional features to an existing class without modifying it. This is achieved by deriving a new class from the existing one. The new class will have combined features of both the classes.

    Polymorphism

    Polymorphism means the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends on the data types used in the operation. Polymorphism is extensively used in implementing Inheritance.

    Advantages of OOP

    Object-Oriented Programming has the following advantages over conventional approaches:

    • OOP provides a clear modular structure for programs which makes it good for defining abstract datatypes where implementation details are hidden and the unit has a clearly defined interface.
    • OOP makes it easy to maintain and modify existing code as new objects can be created with small differences to existing ones.
    • OOP provides a good framework for code libraries where supplied software components can be easily adapted and modified by the programmer. This is particularly useful for developing graphical user interfaces.

    Perl Package

  • To create a class in Perl, we first create a

    package.

  • A package (module) is a module of 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.

    To declare a class named Person in Perl we do:

    package Person;

  • The scope of the package definition extends to the

    end of the file

  • or until another package keyword is encountered.
    Perl Methods

    A method (subroutine) is a way of accessing objects. In

    Perl, a method is just a subroutine defined within a

    particular package. So to define a method to print our

    Person object, we do:

    
    sub print {
    
        my ($self) = @_; ## creating reference
    
        #print Person info
    
        printf( "Name:%s %snn", $self->firstName, $self->lastName );
    
    }
    
    
    
    

    The subroutine print is now associated with the package

    Person. To call the method print on a Person object, we

    use the Perl “arrow” notation. If the variable $mike

    contains a Person object, we would call print on that

    object by writing:

    $mike->print();

    When the object method is invoked, a reference to the

    object is passed in along with any other arguments

    (including class name). This is important since the

    method now has access to the object on which it is to

    operate.

    How do we create object?
  • To create an instance of a class (an object) we need

    an object constructor.

  • This constructor is a method defined within the

    package.

  • Most programmers choose to name this object

    constructor method new, but in Perl one can use any

    name.

  • One can use any kind of Perl variable as an object

    in Perl.

  • Most Perl programmers choose either

    references to arrays or hashes.

    Let’s create our constructor for our Person class using

    a Perl hash reference;

    
    package Person;
    
    sub new {
    
        my $self = {
    
            _firstName => undef,
    
            _lastName  => undef,
    
            _ssn       => undef,
    
            _address   => undef,
    
            _salary    => undef,
    
            _dept      => undef
    
        };
    
        bless $self, 'Person';
    
        return $self;
    
    }
    
    
    
    sub print {
    
        my ($self) = @_; ## creating reference
    
        #print Person info
    
        printf( "Name:%s %snn", $self->firstName, $self->lastName );
    
    }
    
    
  • We created a subroutine (method) called new

    associatedwith the package Person.

  • Other method is print (as discussed earlier).
  • The entries of the hash reference $self become the

    attributes of our object. We then use the bless function

    on the hash reference.

  • The bless function takes two arguments: a reference

    to the variable to be marked and a string containing the

    name of the class. This indicates that the variable now

    belongs to the class Person.

    To create an instance of our Person object:

    my $mike = new Person();

    We have not defined accessor methods or done any error

    checking on the input values or keys or the anonymous

    hash reference, but we have the start of a Perl Person

    OO framework. To make our constructor more flexible and

    to make our class inheritable (more on that later), we

    can define it to use the $class variable to bless the

    hash reference.

    
    #constructor
    
    sub new {
    
        my ($class) = @_;
    
        my $self = {
    
            _firstName => undef,
    
            _lastName  => undef,
    
            _ssn       => undef,
    
            _address   => undef,
    
            _salary    => undef,
    
            _dept      => undef
    
        };
    
        bless $self, $class;
    
        return $self;
    
    }
    
    

    Other object-oriented languages have the concept of

    security of data to prevent a programmer from changing

    an object data directly and so provide accessor methods

    to modify object data. Perl does not have private

    variables but we can still use the concept of accessor

    methods and ask programmers to not mess with our object

    innards.

    For our Person class, we should provides accessor

    methods for our object attributes; name, address, title,

    ssn,salary,dept.

    
    package Person;
    
    use strict;
    
    use Address;  #Person class will contain an Address
    
    
    
    #constructor
    
    sub new {
    
        my ($class) = @_;
    
        my $self = {
    
            _firstName => undef,
    
            _lastName  => undef,
    
            _ssn       => undef,
    
            _address   => undef
    
            _salary    => undef,
    
            _dept      => undef
    
        };
    
        bless $self, $class;
    
        return $self;
    
    }
    
    
    
    #accessor method for Person first name
    
    sub firstName {
    
        my ( $self, $firstName ) = @_;
    
        $self->{_firstName} = $firstName if defined ($firstName);
    
        return $self->{_firstName};
    
    }
    
    
    
    #accessor method for Person last name
    
    sub lastName {
    
        my ( $self, $lastName ) = @_;
    
        $self->{_lastName} = $lastName if defined($lastName);
    
        return $self->{_lastName};
    
    }
    
    
    
    #accessor method for Person address
    
    sub address {
    
        my ( $self, $address ) = @_;
    
        $self->{_address} = $address if defined($address);
    
        return $self->{_address};
    
    }
    
    
    
    #accessor method for Person social security number
    
    sub ssn {
    
        my ( $self, $ssn ) = @_;
    
        $self->{_ssn} = $ssn if defined($ssn);
    
        return $self->{_ssn};
    
    }
    
    
    
    sub salary {
    
        my ( $self, $salary ) = @_;
    
        $self->{_salary} = $salary if defined($salary);
    
        return $self->{_salary};
    
    }
    
    
    
    sub dept {
    
        my ( $self, $dept ) = @_;
    
        $self->{_dept} = $dept if defined($dept);
    
        return $self->{_dept};
    
    }
    
    
    
    
    
    
    
    sub print {
    
        my ($self) = @_;
    
        #print Person info
    
        printf( "Name:%s %snn", $self->firstName, $self->lastName );
    
    }
    
    
    
    1;
    
    
    
    
    Making Objects

    Object-oriented programming sometimes involves

    inheritance. Inheritance simply means allowing one class

    called the Child to inherit methods and attributes from

    another, called the Parent, so you don’t have to write

    the same code again and again. For example, we can have

    a class Employee which inherits from Person. This is

    referred to as an “isa” relationship because an employee

    is a person. Perl has a special variable, @ISA, to help

    with this.

    @ISA governs (method) inheritance. So to create a new

    Employee class that will inherit methods and attributes

    from our Person class, we simply code:

    
    # class Employee
    
    package Employee;
    
    use Person;
    
    use strict;
    
    our @ISA = qw(Person);    # inherits from Person
    
    

    What we have done is load the Person class and declare

    that Employee class inherits methods from it. We have

    declared no methods for Employee but an Employee object

    will behave just like a Person object. We should be able

    to write code:

    
    #create Employee class instance
    
    my $mike =  new Employee();
    
     
    
    #set object attributes
    
    $mike->firstName('mike');
    
    $mike->lastName('Weis');
    
    

    without any other changes.

    Now let’s add some methods.

    
    # class Employee
    
    package Employee;
    
    use Person;
    
    use strict;
    
    our @ISA = qw(Person);    # inherits from Person
    
    
    
    #constructor
    
    sub new {
    
        my ($class) = @_;
    
        #call the constructor of the parent class, Person.
    
        my $self = $class->SUPER::new();
    
        $self->{_id}   = undef;
    
        $self->{_title} = undef;
    
        bless $self, $class;
    
        return $self;
    
    }
    
    
    
    #accessor method for  id
    
    sub id {
    
        my ( $self, $id ) = @_;
    
        $self->{_id} = $id if defined($id);
    
        return ( $self->{_id} );
    
    }
    
    
    
    #accessor method for  title
    
    sub title {
    
        my ( $self, $title ) = @_;
    
        $self->{_title} = $title if defined($title);
    
        return ( $self->{_title} );
    
    }
    
    
    
    sub print {
    
        my ($self) = @_;
    
    
    
        # we will call the print method of the parent class
    
        $self->SUPER::print;
    
        $self->address->print;
    
    }
    
    
    
    1;
    
    

    Looking at the code, you will notice that we have a new

    method and a print method. Both the child class and its

    parent class have the same method defined. We have

    overridden the parent class’ methods with the ones from

    the child. When those methods are called on an Employee

    object, we will get the Employee class’ version of the

    method. This concept of using the methods of an existing

    object and modifying them is known as polymorphism.

    Putting it together

    So now that we have a complete set of classes, we can

    write a small program to test them.

    
    use strict;
    
    use warnings;
    
    use diagnostics;
    
    use Employee;
    
    
    
    #create Employee class instance
    
    my $mike =  eval { new Employee(); }  or die ($@);
    
     
    
    #set object attributes
    
    $mike->firstName('mike');
    
    $mike->lastName('Weis');
    
    $mike->id(0731034);
    
    $mike->title('Perl Programmer');
    
    $mike->address( new Address() );
    
    $mike->address->street('30 Hudson court');
    
    $mike->address->city('Jersey City');
    
    $mike->address->state('NJ');
    
    $mike->address->zip('665030');
    
    
    
    #diplay Employee info
    
    $mike->print();
    
    

    Let’s execute our code and see the output:

    $ ./test.pl

    Name:mike Weis

    Address:30 Hudson court

    Jersey City, NJ 665030


  • Viewing all articles
    Browse latest Browse all 15

    Trending Articles