Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Sharing an attribute between multiple objects with moose

by docdurdee (Beadle)
on Jul 28, 2011 at 16:12 UTC ( #917291=perlquestion: print w/ replies, xml ) Need Help??
docdurdee has asked for the wisdom of the Perl Monks concerning the following question:

Dearest Monks,

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): pus +h @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;

Comment on Sharing an attribute between multiple objects with moose
Download Code
Re: Sharing an attribute between multiple objects with moose
by metaperl (Curate) on Jul 28, 2011 at 16:55 UTC
Re: Sharing an attribute between multiple objects with moose
by Boldra (Deacon) on Jul 29, 2011 at 08:05 UTC
    Although you already suggest you're abstracting too much, it sounds like you want to add a "cursor" class, and share the cursor between different AoAs.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://917291]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-09-30 12:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (368 votes), past polls