访问 Moose 数组
无法弄清楚推送到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您误解了
traits => 的含义['Array']
确实如此。这允许您设置handles
方法。它不允许您直接调用诸如push
之类的方法。您需要使用 Moose::Autobox 来实现这一点(并且不需要 Array 特征)。或者你可以这样做:
You've misunderstood what
traits => ['Array']
does. That allows you to set uphandles
methods. It does not allow you to call methods likepush
directly. You need touse Moose::Autobox
for that (and you don't need the Array trait).Or you could do: