I need help creating objects that relate to one another as I would like. I am uncertain whether I have employed "use parent" correctly. I also cannot understand part of the output from one of my test scripts. (The modules, as I have written them, produce the right results, but I'm not at all certain why they do so or whether I have erred behind the scenes.)

I apologize for the lengthy question. If I knew more, the question would be shorter. I am writing a module to manage "riders" as they interact on and with a common "map." The map is a collection of nodes with various attributes. Riders can change the values of the attributes of the nodes and thereby change the common map.

The relation between my many rider objects and the single map object is what is puzzling me. I have put my modules together like this: A module for rider objects (package Transit), a module for node objects, and a module for the map object (Transit::Map), which is a collection of nodes. The map module "uses" the node module. (I organized the modules in this way and with these names because I wanted my users to worry only about the rider objects. In hindsight I probably should have chosen different names: Rider and Rider::Map make more sense.)

The map module is a parent of the rider module and when the rider module is loaded, it should create a single map object. Here are the relevant parts of the modules.

package Transit; use Moose; . . . use parent 'Transit::Map'; my $map = Transit::Map->new;
-----------------
package Transit::Map; use Moose; . . . has 'map_data' => ( is => 'ro', writer => '_map_data', ); sub BUILD { my $self = shift; my $args = shift; $self->_load_map_data; return; }

The rider module seemed like the correct place to create the single map object. (It didn't feel right to make Transit::Map the parent of the rider module, Transit, but it worked.) Despite my discomfort, my test scripts work fine, i.e. they give the right results.

Nonetheless, in addition to my uncertainty about whether "use parent" is correct, I'm not sure the modules are doing the right thing behind the scenes. When I create two riders, and then use them to print the map object, why don't I get the same hash reference?

use Transit; my $c = Transit->new( ); [change some values of node attributes] my $d = Transit->new( ); say "first map_info object: ", $c->map_data; say "second map_info object: ", $d->map_data;
The output is:
first map_info object: HASH(0xb559c10) second map_info object: HASH(0xae699a8)

I think this output says I have two different map objects, because I have different object references. I think I should only have one, because I should have only one map. And, I don't believe I have two different map objects because the data in the second ($d->map_data) shows the changes made by the first rider. If the map object were an attribute of the rider objects, I would understand, but since the map object should be created when Transit is loaded, I just don't grasp what is going on.


In reply to How Should I Relate Many Objects Of One Kind To A Single, Common Object Of Another by varanasi

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.