194

Object Oriented Perl-gramming
Classes

  • A class needs an object constructor method which is simply a subroutine which returns a reference to a new class instance

  • A class may also contain other methods which are simply subroutines defined inside the same package's namespace
      package MyClass;
      
      sub new {
      	my $type = shift;           # Get the package/type name
      	my $self = {};              # Reference to empty hash
      	return bless $self, $type;  # Add and return the class instance
      }
      
      sub MyMethod {
      	print "   MyClass::MyMethod called!\n";
      }
      

  • A class constructor and methods can then be called

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