195

Object Oriented Perl-gramming
SubClasses

  • A sub-class needs an object constructor method which is simply a subroutine which returns a reference to its parent-class instance

  • A sub-class may also contain methods which either override or inherit functionality from methods of its parent class
      package MySubClass;
      
      @ISA = qw( MyClass );
      
      sub new {
      	my $type = shift;           # Get the package/type name
      	my $self = MyClass->new;    # Reference to parent's hash
      	return bless $self, $type;  # Add and return the class instance
      }
      
      sub MyMethod {
      	my $self = shift;
      	$self->SUPER::MyMethod();  # Call the parent method if desired
      	print "   MySubClass::MyMethod called!\n";
      }
      

  • A class constructor and methods can then be called

      $myObject = MySubClass->new();
      $myObject->MyMethod();