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

发布于 12-04 21:09 字数 60 浏览 1 评论 0原文

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

I want to define a "registry" hash in the base class that all subclasses can read and write to, how do I accomplish this with Moose/Perl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的往事2024-12-11 21:09:51

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

您有两个类:带有全局变量 $REGISTRYBaseClass 和继承自 BaseClassDerivedClass
$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

如果您更喜欢全 Moose 解决方案,您应该尝试这个:

#!/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 => {} 是禁止的,您必须将任何引用作为返回值包装在匿名子例程中。

Here is an implementation with plain Perl OO-style.

You have two classes, BaseClass with global variable $REGISTRY, and DerivedClass which inherits from BaseClass.
$REGISTRY is readable and writable from any class instance via registry() method.

#!/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 };

If you run this program you get:

alpha -> 1
beta -> 2

If you prefer an all-Moose solution you should try this one:

#!/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 };

It yields the same result of the OO Perl program.
Note how the _REGISTRY attribute is defined. Moose doesn't like refs as default values: default => {} is forbidden, you have to wrap any reference as a return value in an anonymous subroutine.

和影子一齐双人舞2024-12-11 21:09:51

如何将其作为方法来实现:

package BaseClass;

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

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

How about just implement it as a method:

package BaseClass;

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

Sub-classes just use $self->registry->{$key} to access values and $self->registry->{$key} = $value to set them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文