Hi! My name is Nelo and my job is none of your business!
Destroying Employee=SCALAR(0x188f4c4)
Deleted job ...
Destroying Employee=SCALAR(0x188f4c4)
Deleted name ...
Hi! My name is Amelie and my job is none of your business!
Destroying Employee=SCALAR(0x22606c)
Deleted job ...
Destroying Employee=SCALAR(0x22606c)
Deleted name ...
Hi! My name is Wole and my job is none of your business!
Destroying Employee=SCALAR(0x183296c)
Deleted job ...
Destroying Employee=SCALAR(0x183296c)
Deleted name ...
Hi! My name is Hacer and my job is none of your business!
Destroying Employee=SCALAR(0x188f4c4)
Deleted job ...
Destroying Employee=SCALAR(0x188f4c4)
Deleted name ...
####
Hi! My name is Nelo and my job is none of your business!
Destroying Employee=SCALAR(0x188f4c4)
Deleted job ...
Hi! My name is Amelie and my job is none of your business!
Destroying Employee=SCALAR(0x22606c)
Deleted job ...
Hi! My name is Wole and my job is none of your business!
Destroying Employee=SCALAR(0x183296c)
Deleted job ...
Hi! My name is Hacer and my job is none of your business!
Destroying Employee=SCALAR(0x188f4c4)
Deleted job ...
####
#!/usr/bin/perl -w
use strict;
package main;
{
for my $name ( qw( nelo amelie wole hacer ) ) {
my $person = Employee->new(name=>$name, job=>"none of your business!");
print "\n", 'Hi! My name is ', ucfirst $person->get_name, ' and my job is ', $person->get_job, "\n";
}
}
1;
package Person;
{
use Carp qw( &carp &croak );
use Scalar::Util qw( &refaddr );
# object's attributes
my (%name_of);
# construct a new object
sub new {
my ($class, %args) = @_;
ref $class and croak "Incorrect Usage: $class->new(%args)";
my $self = bless \do{my $scalar}, $class;
$self->_init(%args);
return $self;
}
# initialise the new object's attributes
sub _init {
my ($self, %args) = @_;
ref $self or croak "Incorrect Usage: $self->_init(%args)";
# set values with reasonable defaults
$self->set_name( $args{name} || 'unknown' );
}
# object accessor methods
sub set_name {
my ($self, $name) = @_;
ref $self or croak "Incorrect Usage: $self->set_name";
$name or croak "No argument provided to set_name";
$name_of{refaddr $self} = $name;
}
sub get_name {
my ($self) = @_;
ref $self or croak "Incorrect Usage: $self->get_name";
@_ == 1 or carp "No arguments required by get_name";
return $name_of{refaddr $self};
}
# class accessor methods
# private (internal) methods
# utility (external) methods
# destroy the object
sub DESTROY {
my ($self) = @_;
print "Destroying $self\n";
delete $name_of{refaddr $self} and print "Deleted name ...\n";
}
}
1;
package Employee;
{
use Carp qw( &carp &croak );
use Scalar::Util qw( &refaddr );
# inherits from ...
use base qw( Person ); # works with this :)
#~ our @ISA = qw( Person ); # breaks with this :(
# object's attributes
my (%job_of);
# construct new object (inherited)
# initialise the new object's attributes
sub _init {
my ($self, %args) = @_;
ref $self or croak "Incorrect Usage: $self->_init(%args)";
# set values with reasonable defaults
$self->set_job( $args{job} || 'unknown' );
# initialise the parent class attributes
$self->SUPER::_init(%args);
}
# object accessor methods
sub set_job {
my ($self, $job) = @_;
ref $self or croak "Incorrect Usage: $self->set_job";
$job or croak "No argument provided to set_job";
$job_of{refaddr $self} = $job;
}
sub get_job {
my ($self) = @_;
ref $self or croak "Incorrect Usage: $self->get_job";
@_ == 1 or carp "No arguments required by get_job";
return $job_of{refaddr $self};
}
# class accessor methods
# private (internal) methods
# utility (external) methods
# destroy the object
sub DESTROY {
my ($self) = @_;
print "Destroying $self\n";
delete $job_of{refaddr $self} and print "Deleted job ...\n";
# call parent destructor
$self->SUPER::DESTROY; # comment out and "name" hangs around ... i think :)
}
}
1;