I think we need to look at what data_defs.pl does.

Do you want one copy of this data that should be consistent across all of your code (i.e., if package A asks for the data defs and fiddles with them, should only package A see those changes)? Or do you want a per-package copy? It looks like you want #2.

I'd suggest creating a factory class that gives you the data you want when you need it. As a for-instance, let's assume we have some data that we want to get consistently right, but which we don't necessarily need to have all of in every module, and which shouldn't be shared with other callers. So something like this

package DataDefs::Factory; use strict; use warnings; # define data here in 'my' variables ... my $hash_thing = { foo => 1, bar => 2, baz => 3, }; # and so on sub unshared_thing { # Called as a class method... my($class) = @_; # Make a copy of the data so that we'll not have # trouble elsewhere if the caller modifies it. my %copy = %{ $hash_thing }; return \%copy; } 1;
Your module now does
use DataDefs::Factory; my $thingy = DataDefs::Factory->first_thing;
to get a "personal" copy of the data. If you wanted to have a singleton of a data item (that is, one copy that everyone shares), then you'd just return a direct reference to it instead of copying it to a second variable.

If you have really complicated, deeper data structures, I'd recommend using Clone to make the copy that you return. Heck, you might as well just use Clone in the first place; that way if you change how complex the data structure is, you'll not have to alter the code.

... use Clone qw(clone); ... sub first_thing { my($class) = @_; return clone($hash_thing); }
If your data's at all complex and you do similar operations over it in a number of places, you might want to consider making the package that you get the data from into a full-fledged class, and use accessors to get the data you want rather than rewriting the same code to crawl through multiple levels of data structure in multiple places.

I could go into details about callbacks for iterating over depp structures and the like, but Mark-Jason Dominus has done it much better than I could in this space in "Higher-Order Perl". Check out http://hop.perl.plover.com// for lots on this topic.


In reply to Re: Require in modules...? by pemungkah
in thread Require in modules...? by DickieC

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.