Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Moose and arrays of objects

by cellofan (Initiate)
on Dec 03, 2012 at 09:54 UTC ( #1006833=perlquestion: print w/ replies, xml ) Need Help??
cellofan has asked for the wisdom of the Perl Monks concerning the following question:

small code:

has 'id' => ( is =>'ro', isa => 'Int', required => 1, builder => '_labelling', init_arg => undef, ); sub _labelling { $N++ } has 'verlet' => ( is =>'rw', isa => 'ArrayRef[Molecule::Atom]', required => 1, weak_ref => 1, default => sub { [] }, ); sub add_verlet{ my $self = shift; print $self->{id},"\n"; my $array = $self->{verlet}; foreach my $obj (@_){ print "adding :", $obj->{id},"\n"; push(@{$array},$obj); weaken(@{$array}[-1]); } print "Array dim = $#{$array}\n"; } sub print_verlet{ my $self = shift; my $array = $self->{verlet}; print "Verlet from Atom ", $self->{id}, "\n"; foreach my $obj (@{$array}){ print "Verlet list :", $obj->{id},"---->\n"; } }

in the main program:

$atom1a = Molecule::Atom->new(O); $atom1b = Molecule::Atom->new(X); $atom1c = Molecule::Atom->new(Y); $atom2a = Molecule::Atom->new(Z); $atom1a->add_verlet($atom1c,$atom1b); $atom1a->add_verlet($atom2a); $atom1a->print_verlet();

output:

8 adding :10 adding :9 Array dim = 1 8 adding :11 Array dim = 0 Verlet from Atom 8

What it should be:

8 adding :10 adding :9 Array dim = 1 8 adding :11 Array dim = 2 Verlet from Atom 8 Verlet list 10 Verlet list 9 Verlet list 11

What I am doing wrong?

I am starting to program in Moose, so the meaning of certain issues with Moose I don not fully understand. As far I know, this code should work. I also try:

has 'verlet' => ( traits => ['Array'], is =>'rw', isa => 'ArrayRef[Molecule::Atom]', required => 1, weak_ref => 1, default => sub { [] }, handles => { add_atom =>'add_verlet', } );

But I get compilation error like: I can not find add_atom in the package. So, definitively there is something I am doing wrong.

Is there anyone understand my problem?

Playing a bit found that "weak_ref => 0" works!!! If I do not understand bad, I need weak_ref => 1 because I do not want destroying the objects stored in the vector, so I see that the actual question is how I said to perl that I do not want to destroy the objects stored in the array?

Comment on Moose and arrays of objects
Select or Download Code
Re: Moose and arrays of objects
by moritz (Cardinal) on Dec 03, 2012 at 10:37 UTC
    has 'verlet' => ( is =>'rw', isa => 'ArrayRef[Molecule::Atom]', required => 1, weak_ref => 1, default => sub { [] }, );

    I'm pretty sure the weak_ref => 1 is wrong, because you don't keep another reference to the array. So when method add_verlet exits, the array reference is garbage-collected, and on the next call replaced by the default value.

    So, remove the weak_ref => 1.

    Once your object is destroyed, the array will also be destroyed. Since you weaken() the objects inside the array, they will go away as soon as your external references to them go away.

    so I see that the actual question is how I said to perl that I do not want to destroy the objects stored in the array?

    You don't have to do anything special. Remove all the weaken and weak_ref stuff. (You only need that if your object graph contains circles). The objects only go away once all references to them are gone, so usually you don't have to take care about it at all.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006833]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-08-20 11:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (111 votes), past polls