0

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

4

1 回答 1

4

您的构造函数是继承的,因此可以正常工作。不起作用的是您对 的使用%config,它单独存在于每个包中。因为您正在调用基类中定义的构造函数,%config所以使用该版本。在您的特定情况下,配置哈希是不必要的,因为您可以使用传递给构造函数name的变量来初始化成员:$class

sub new {
    my $class = shift;
    my $self = { };     # initialize the object as a reference to an empty hash
    $self->{"name"} = $class;
    bless ($self, $class);
    return $self;
};

这将起作用(尽管没有必要;您始终可以使用 获取对象的类Scalar::Util::blessed)。

但更普遍的问题似乎是关于如何在继承的构造函数中使用特定于类的配置信息。一种方法是使用可以在子类中覆盖的单独初始化步骤。

package tstbase;

# we don't use Exporter for OO code; exporting methods is highly counterproductive.
# we should also turn on strict and warnings.
use strict;
use warnings;

my %config = (
    "class" => "tstbase",
);

sub new {
    my $class = shift;
    my $self;
    bless $self, $class;
    $self->_init( %config );
    return $self;
};

sub _init { 
    my $self = shift;
    my %args = @_;
    $self->{name} = $args{class};
}

1;

接着:

package tstsubclass;
use parent 'tstbase';   # we have to say what class we're extending

my %config = (
  "class" => "tstsubclass",
);

sub _init { 
    my $self = shift;
    $self->SUPER::_init( %config );
}

1;

在这种情况下,您的子类的_init方法将被父类中的构造函数调用,该构造函数调用父类的_init方法,但传入其 local %config

一种更简单的处理方法是使用 mixin 或 Moose 角色。

于 2013-10-11T17:57:44.320 回答