I am trying to initialize a base class and a subclass without having to copy the constructor. This is what I got:
tstbase.pm:
package tstbase;
use Exporter qw(import);
our @EXPORT = qw(&new);
my %config = (
"class" => "tstbase",
);
sub new {
my $class = shift;
my $self;
$self->{"name"} = $config{"class"};
bless ($self, $class);
return $self;
};
1;
tstsubclass.pm:
package tstsubclass;
use tstbase;
my %config = (
"class" => "tstsubclass",
);
1;
tst.pl:
#!/usr/bin/perl
use tstsubclass;
my $baseobj = tstbase->new;
print "Testbase ".$baseobj->{"name"}."\n";
my $subobj = tstsubclass->new;
print "Testsubclass ".$subobj->{"name"}."\n";
The outout of tst.pl is
Testbase tstbase
Testsubclass tstbase
but I am looking for
Testbase tstbase
Testsubclass tstsubclass
which I get when I copy the "sub new { .. }" routine over to tstsubclass.pm. Is there a way to avoid that overhead? I have tried all combinations of my %config / our %config and exporting %config with no success.
Any help is greatly appreciated
Best, Marcus