Just make sure than one of your link creation functions bails out if its already been set. For instance in the code below if the node is already in the taxon the taxon doesn't bother trying to set itself as that nodes taxon. The other option could be to have one more layer of inderiction. So you have set_taxon($taxon) that users use, it would automaticaly create the link, it would then call $taxon->_add_node($node) The private (underscored) versions of add_node and set_taxon don't automaticaly do anything.

use strict; use warnings; { package Taxon; sub new { bless {nodes=>[]}, shift; }; sub add_node { my $self = shift; my $node = shift; return 1 if $self->has_node($node); push @{$self->{nodes}}, $node; $node->set_taxon($self); } sub has_node { my $self = shift; my $node = shift; return grep { $_ eq $node } $self->get_nodes(); } sub get_nodes { @{shift->{nodes}} }; } { package Node; sub new { bless {_taxon=>''}, shift; }; sub taxon { my $self = shift; return $self->{'_taxon'}; } sub set_taxon { my $self = shift; my $taxon = shift; return if $self->{'_taxon'} eq $taxon; $self->{'_taxon'} = $taxon; $self->{'_taxon'}->add_node($self); } } my $node = new Node; my $taxon = new Taxon; print '$node->taxon() = ', $node->taxon(), "\n"; print '$taxon->get_nodes() = ', $taxon->get_nodes(), "\n"; $node->set_taxon($taxon); print '$node->taxon() = ', $node->taxon(), "\n"; print '$taxon->get_nodes() = ', $taxon->get_nodes(), "\n"; $node = new Node; $taxon = new Taxon; print '$node->taxon() = ', $node->taxon(), "\n"; print '$taxon->get_nodes() = ', $taxon->get_nodes(), "\n"; $taxon->add_node($node); print '$node->taxon() = ', $node->taxon(), "\n"; print '$taxon->get_nodes() = ', $taxon->get_nodes(), "\n";

Or the second way

use strict; use warnings; { package Taxon; sub new { bless {nodes=>[]}, shift; }; sub add_node { my $self = shift; my $node = shift; $self->_add_node($node); $node->_set_taxon($self); } sub _add_node { my $self = shift; my $node = shift; return 1 if $self->has_node($node); push @{$self->{nodes}}, $node; } sub has_node { my $self = shift; my $node = shift; return grep { $_ eq $node } $self->get_nodes(); } sub get_nodes { @{shift->{nodes}} }; } { package Node; sub new { bless {_taxon=>''}, shift; }; sub taxon { my $self = shift; return $self->{'_taxon'}; } sub set_taxon { my $self = shift; my $taxon = shift; $self->_set_taxon($taxon); $taxon->_add_node($self); } sub _set_taxon { my $self = shift; my $taxon = shift; $self->{'_taxon'} = $taxon; } } my $node = new Node; my $taxon = new Taxon; print '$node->taxon() = ', $node->taxon(), "\n"; print '$taxon->get_nodes() = ', $taxon->get_nodes(), "\n"; $node->set_taxon($taxon); print '$node->taxon() = ', $node->taxon(), "\n"; print '$taxon->get_nodes() = ', $taxon->get_nodes(), "\n"; $node = new Node; $taxon = new Taxon; print '$node->taxon() = ', $node->taxon(), "\n"; print '$taxon->get_nodes() = ', $taxon->get_nodes(), "\n"; $taxon->add_node($node); print '$node->taxon() = ', $node->taxon(), "\n"; print '$taxon->get_nodes() = ', $taxon->get_nodes(), "\n";

___________
Eric Hodges

In reply to Re: One to many, many to one relationships by eric256
in thread One to many, many to one relationships by rvosa

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.