Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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?


In reply to Moose and arrays of objects by cellofan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (11)
    As of 2014-07-25 10:20 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (170 votes), past polls