I have the following.
package A;
sub new {
my ($class) = @_;
my $self = { };
bless $self, $class;
return($self);
}
sub run() {
die "Task: ",__PACKAGE__, "requires a run method";
}
package B;
use A;
our @ISA = qw(A);
sub new {
my ($class) = @_;
my $self = { };
bless $self, $class;
return($self);
}
package C;
use A;
my @Tasks;
sub new {
my ($class) = @_;
my $self = { };
bless $self, $class;
return($self);
}
sub add{
my($self,$tempTask) = @_ ;
push(@Tasks,$tempTask);
$arraysize = @Tasks;
}
sub execute{
foreach my $obj (@Tasks)
{
$obj->run();
}
}
1;
Script
#!/usr/local/bin/perl
use strict;
use C;
use B;
my $tb = new C();
my $task = new B();
$tb->add($task);
$tb->execute();
Package B doesn’t have a run method so it defaults to the Package A run method which is what I want. At this point I want it to print out the name of Package B (there will be many different packages inheriting Package A, but it doesnt.
Currently it prints out Package A using the __PACKAGE__ variable.
Any help?
An object is a blessed reference.
__PACKAGE__will always equal the name of the current package. Butref( $object )will give you the name of the class of object. There is alsoScalar::Util::blessed, which will not give you false positives for non-blessed references.So in your particular case: