在Perl / Moose中,如何在可以从子类访问的父类中创建静态变量?

时间:2011-09-14 19:40:42

标签: perl static moose

我想在基类中定义一个所有子类都可以读写的“注册表”哈希,如何使用Moose / Perl实现这一点?

3 个答案:

答案 0 :(得分:6)

这是一个简单的Perl OO风格的实现。

您有两个类,BaseClass具有全局变量$REGISTRYDerivedClass继承自BaseClass$REGISTRY可通过registry()方法从任何类实例读写。

#!/usr/bin/env perl

use 5.012;
use strict;

package BaseClass;

our $REGISTRY = {};

sub new {
    my $class = shift;
    my $self  = {};
    bless $self, $class;
    return $self;
}

sub registry {
    my $self = shift;
    return $REGISTRY;
}

package DerivedClass;

push our @ISA, 'BaseClass';

package main;

my $base    = BaseClass->new;
$base->registry->{ alpha } = 1;

my $derived = DerivedClass->new;
$derived->registry->{ beta } = 2;

say $_, ' -> ', $base->registry->{ $_ } foreach keys %{ $base->registry };

如果你运行这个程序,你会得到:

alpha -> 1
beta -> 2

如果你更喜欢全驼鹿解决方案,你应该试试这个:

#!/usr/bin/env perl

use 5.012;
use strict;

package BaseClass;
use Moose;

our $_REGISTRY = {};
has '_REGISTRY' => (
    is      => 'rw',
    isa     => 'HashRef',
    default => sub { return $_REGISTRY }
);

sub registry {
    my $self = shift;
    return $self->_REGISTRY;
}

__PACKAGE__->meta->make_immutable;
no Moose;

package DerivedClass;
use Moose;

use base 'BaseClass';

__PACKAGE__->meta->make_immutable;
no Moose;

package main;

my $base    = BaseClass->new;
$base->registry->{ alpha } = 1;

my $derived = DerivedClass->new;
$derived->registry->{ beta } = 2;

say $_, ' -> ', $base->registry->{ $_ } foreach keys %{ $base->registry };

它产生与OO Perl程序相同的结果。 请注意_REGISTRY属性的定义方式。 Moose不喜欢refs作为默认值:default => {}被禁止,你必须将任何引用包装为匿名子例程中的返回值。

答案 1 :(得分:5)

如何将其作为一种方法实施:

package BaseClass;

my $hash = {};
sub registry { $hash };

子类只使用$self->registry->{$key}来访问值,$self->registry->{$key} = $value来设置它们。

答案 2 :(得分:4)