如何在多个Moose对象之间共享属性? [英] How to share an attribute between multiple Moose objects?
问题描述
这是我的问题:有一个简单的方法来共享对象之间的属性(相同的内存位置)这是不好的做法吗?在下面的AoA示例中,我使用AoA icol属性来访问底层数组中我想要的内容。另一种方法是使属性i和icol引用相同的值(更改一个并更改所有值)。我倾向于下面的解决方案,因为它看起来更加清晰,但我真的很感激,如果有人会给它看看,帮助我改善我的想法。另一个问题:我必须在MyArray类中设置ents属性的句柄吗?我尝试评论,并失去了这些方法。
{
包AoA;
使用namespace :: autoclean;
使用麋;
有[qw(icol irow)] => (
is =>'rw',
isa =>'Int',
default => 0,
);
有'rows'=> (
traits => ['Array'],
is =>'rw',
isa =>'ArrayRef',
default => sub { ]},
handle => {
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;
我的$ nrows = $ self-> count_rows-1;
foreach我的$ i(0 .. $ nrows){
$ _ [$ i]? $ self> rows-> [$ i] - > push($ _ [$ i]):$ self> rows-> [$ i] - > push(undef);
}
}
sub get_col {
my $ self = shift;
我的$ icol = shift || $ self> icol;
我的$ nrows = $ self-> count_rows-1;
我的@column;
foreach(0 .. $ nrows){
我的$ row = $ self-> get_row($ _);
$ icol< = $ row-> count? push @column,$ row-> get($ icol):push @column,undef;
}
返回\ @列;
}
__PACKAGE __-> meta-> make_immutable;
}
{
包MyArray;
使用namespace :: autoclean;
使用麋;
有'i'=> (
is =>'rw',
isa =>'Int',
default => 0,
);
有'ents'=> (
traits => ['Array'],
is =>'rw',
isa =>'ArrayRef',
default => sub { ]},
handle => {
push =>'push',
get =>'get',
set =>'set',
elements =>'elements',
count =>'count',
},
);
__PACKAGE __-> meta-> make_immutable;
}
使用Modern :: Perl;
我的$ 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]]);
我的$ a = AoA-> new(rows => [$ a0,$ a1]);
$ a-> add_row($ a2);
$ a-> add_col([3,3,3],[4,4,4],[5,5,5]);
我的$ row0 = $ a-> get_row(0);
我的$ row1 = $ a-> get_row(1);
我的$ row2 = $ a-> get_row(2);
我的$ element_22 = $ a-> get_element(2,2);
我的$ col2 = $ a-> get_col(1);
使用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 。
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,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.
这篇关于如何在多个Moose对象之间共享属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!