Objects

Everything in Perl is meant to be simple yet advanced. You can use objects the simple way, or with more complex applications. Here is the basics of setting up a perl object.

package SampleObject;
 
sub new {
  my ($className, %parmHash) = @_;
  my $self = { created=>time(),\%parmHash };
  bless $self, $className;
  return $self;
}
 
sub printCreated {
  my ($self, @parms) = @_;
  print "created at $self->{created}.\n";
}
 
sub DESTROY {
  my $self = shift;
  print "Destructing and Cleaning up object\n";
}
1; ## End Package
 
package main; # the main program and namespace
use SampleObject; # If object resides in another file (SampleObject.pm)
 
my $sampObj = new SampleObject(Name=>'John');
$sampObj->printCreated();
 
programming/perl/oo.txt · Last modified: 2005/07/18 17:16 by allen