简体   繁体   English

如何在多个Moose对象之间共享属性?

[英]How to share an attribute between multiple Moose objects?

I am in need of guidance. 我需要指导。 I've been switching my lifestyle over to Moose as of late. 最近我一直在将自己的生活方式切换到Moose。 Overall, I've found that moose makes life much more interesting and fun. 总的来说,我发现驼鹿使生活变得更加有趣和有趣。 At the same time, I still haven't gotten to a point where I feel more productive... probably because I keep losing myself in abstraction and still haven't learned enough to be able to parse the moose docs effectively enough to know what other smarter folks have solved already. 同时,我还没有达到更高的感觉……可能是因为我一直迷失于抽象,并且仍然没有足够的知识去有效地解析驼鹿文档以了解什么其他更聪明的人已经解决了。 I did zero OO programming before moose walked into my life. 在驼鹿走进我的生活之前,我做了零OO编程。

Here's my question: Is there an easy way to share an attribute (the same memory location) between objects? 这是我的问题:是否有一种简单的方法可以在对象之间共享属性(相同的内存位置)? Is that bad practice? 那是不好的做法吗? In the AoA example below, I use the AoA icol attribute to access what I want in the underlying array. 在下面的AoA示例中,我使用AoA icol属性访问基础数组中所需的内容。 The other approach is to have the attribute i and and icol reference the same value (change one and change all). 另一种方法是让属性i和icol引用相同的值(更改一个并全部更改)。 I'm leaning toward the solution below because it seems clearer, but I would really appreciate if anyone would give it a look and help me improve my thinking. 我倾向于下面的解决方案,因为它看起来更清晰,但是如果有人可以看看并帮助我改善自己的想法,我将不胜感激。 Another question: do i have to set the handles for ents attribute in the MyArray class? 另一个问题:我必须在MyArray类中设置ents属性的句柄吗? I tried commenting out, and lost those methods. 我尝试注释掉,并丢失了这些方法。

{

    package AoA;
    use namespace::autoclean;
    use Moose;

    has [qw(icol irow)] => (
        is      => 'rw',
        isa     => 'Int',
        default => 0,
    );

    has 'rows' => (
        traits  => ['Array'],
        is      => 'rw',
        isa     => 'ArrayRef',
        default => sub { [] },
        handles => {
            add_row   => 'push',
            get_row   => 'get',
            set_row   => 'set',
            all_rows  => 'elements',
          count_rows  => 'count',
        },
    );


    sub get_element {
      my $self = shift;
      @_ == 2 ?
        return $self->get_row($_[0])->get($_[1]) :
        return $self->get_row($self->irow)->get($self->icol);
    }

    sub add_col {
      my $self=shift;
      my $nrows = $self->count_rows-1;
      foreach my $i (0 .. $nrows){
        $_[$i] ?  $self->rows->[$i]->push($_[$i]) : $self->rows->[$i]->push(undef);
      }
    }

    sub get_col {
      my $self = shift;
      my $icol = shift || $self->icol;
      my $nrows = $self->count_rows-1;
      my @column;

      foreach (0 .. $nrows){
        my $row = $self->get_row($_); 
        $icol <= $row->count ? push @column, $row->get($icol): push @column, undef;
      }
      return \@column;
    }

    __PACKAGE__->meta->make_immutable;
}

{
    package MyArray;
    use namespace::autoclean;
    use Moose;

    has 'i' => (
        is      => 'rw',
        isa     => 'Int',
        default => 0,
    );

    has 'ents' => (
        traits  => ['Array'],
        is      => 'rw',
        isa     => 'ArrayRef',
        default => sub { [] },
        handles => {
            push      => 'push',
            get       => 'get',
            set       => 'set',
            elements  => 'elements',
            count     => 'count',
        },
    );

    __PACKAGE__->meta->make_immutable;

}

use Modern::Perl;

my $a0 = MyArray->new( ents => [ 0, [ 0, 0, 0 ], [1,2,3] ] ) ;
my $a1 = MyArray->new( ents => [ 1, [ 1, 1, 1 ], [4,5,6] ] ) ;
my $a2 = MyArray->new( ents => [ 2, [ 2, 2, 2 ], [7,8,9] ] ) ;

my $a = AoA->new( rows => [ $a0, $a1] )  ;

$a->add_row($a2);
$a->add_col([3,3,3],[4,4,4],[5,5,5]);

my $row0        = $a->get_row(0);
my $row1        = $a->get_row(1);
my $row2        = $a->get_row(2);

my $element_22   = $a->get_element(2,2);

my $col2        = $a->get_col(1);

use Data::Dumper;

print Dumper $row0;
print Dumper $row1;
print Dumper $row2;
print Dumper $col2;
print Dumper $element_22;

$a0->set(0,'cat');
print Dumper $row0;


1;

我认为您想使用MooseX :: ClassAttribute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM