开发者

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. 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.

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. The other approach is to have the attribute i and and icol reference the same value (change one and change all). 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? 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开发者_StackOverflow,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;


I think you want to use MooseX::ClassAttribute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜