代码之家  ›  专栏  ›  技术社区  ›  xenoterracide

访问Moose数组

  •  3
  • xenoterracide  · 技术社区  · 15 年前

    this question . 在我看来,我需要的不仅仅是一个简单的值为我的具体情况。试图用驼鹿式的方式实现它(可能是错的?),但我显然做得不对。

    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(@_)
    };
    
    1 回复  |  直到 8 年前
        1
  •  9
  •   cjm    15 年前

    你误解了什么 traits => ['Array'] handles 方法。它不允许您调用 push 直接。你需要 use Moose::Autobox 为此(您不需要数组特性)。

    或者你可以:

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