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
Your module now doespackage 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;
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.use DataDefs::Factory; my $thingy = DataDefs::Factory->first_thing;
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.
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.... use Clone qw(clone); ... sub first_thing { my($class) = @_; return clone($hash_thing); }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |