访问 Moose 数组

发布于 2024-09-15 02:40:58 字数 602 浏览 3 评论 0原文

无法弄清楚推送到 Moose 数组的语法(我确信这是显而易见的,而且我很愚蠢)。这是此问题的延续。在我看来,对于我的具体情况,我需要的不仅仅是一个简单的值。尝试使用驼鹿式的方式来实现它(也许这是错误的?),但我显然做得不对。

use Moose::Role;
has 'tid_stack' => (
    traits => ['Array'],
    is     => 'rw',
    isa    => 'ArrayRef[Str]',
    default => sub { [] },
);


around 'process' => sub {
    my $orig = shift;
    my $self = shift;
    my ( $template ) = @_;

    $self->tid_stack->push( get_hrtid( $template ) );

    $self->$orig(@_)
};

Having trouble figuring out the syntax (which I'm sure is obvious and I'm stupid) for pushing to a Moose array. This is a continuation of this question. it seems to me that I need to more than a simple value for my specific case. Trying to implement it using a Moose-ish way (maybe that's wrong?) but I'm obviously not doing it right.

use Moose::Role;
has 'tid_stack' => (
    traits => ['Array'],
    is     => 'rw',
    isa    => 'ArrayRef[Str]',
    default => sub { [] },
);


around 'process' => sub {
    my $orig = shift;
    my $self = shift;
    my ( $template ) = @_;

    $self->tid_stack->push( get_hrtid( $template ) );

    $self->$orig(@_)
};

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

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

发布评论

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

评论(1

谁的年少不轻狂 2024-09-22 02:40:58

您误解了 traits => 的含义['Array'] 确实如此。这允许您设置handles方法。它不允许您直接调用诸如 push 之类的方法。您需要使用 Moose::Autobox 来实现这一点(并且不需要 Array 特征)。

或者你可以这样做:

has 'tid_stack' => (
    traits => ['Array'],
    is     => 'rw',
    isa    => 'ArrayRef[Str]',
    default => sub { [] },
    handles => {
      push_tid => 'push',
    },
);

...

    $self->push_tid( get_hrtid( $template ) );

You've misunderstood what traits => ['Array'] does. That allows you to set up handles methods. It does not allow you to call methods like push directly. You need to use Moose::Autobox for that (and you don't need the Array trait).

Or you could do:

has 'tid_stack' => (
    traits => ['Array'],
    is     => 'rw',
    isa    => 'ArrayRef[Str]',
    default => sub { [] },
    handles => {
      push_tid => 'push',
    },
);

...

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