linux poison RSS
linux poison Email

Perl Script: Creating Class (package) Inheritance

Inheritance provides a powerful and natural mechanism for organizing and structuring your code.In object-oriented programming, inheritance is a way to form new classes using classes that have already been defined. The new classes, known as derived classes, take over (or inherit) attributes and behavior of the pre-existing classes, which are referred to as base classes (or ancestor classes). It is intended to help reuse existing code with little or no modification.

In Perl, Inheritance is accomplished by placing the names of parent classes into a special array called @ISA in your child class.

Below is simple Perl script which demonstrate the usage of Package Inheritance:


Source: Inheritance.pl
#!/usr/bin/perl

package mother;
  sub a {
    print "Inside the package mother \n";
  }
package father;
  sub b {
    print "Inside the package father \n";
  }
 
# Inheritance is accomplished by placing the names of parent classes into a special array called @ISA.
# The elements of @ISA are searched left to right for any missing methods


package child;
  @ISA = (father, mother);
 
package main;
  child->a();
  child->b();

Output: perl Inheritance.pl
Inside the package mother
Inside the package father





0 comments:

Post a Comment

Related Posts with Thumbnails