in reply to Require in modules...?

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.

Replies are listed 'Best First'.
Re^2: Require in modules...?
by DickieC (Initiate) on May 16, 2009 at 22:39 UTC

    Sorry I haven't looked back for a while - I did see the first few comments, but now there are more. It's suprising what we can learn from each other!

    Anyway, I didn't make my initial question clear enough (my mistake, sorry): None of the data structures in data_defs will change (I hope!), and will be shared by all of the other modules.

    I would also like to make them 'global' - I tried using our for them but the only way I can make it work is to use this at the top of each file that needs them (although there are only a couple):

    require 'data_defs.pl'; use vars qw/ ... /
    where '...' are the structures in data_defs.pl.
    I have also tried making data_defs a USEable module, but I'm not sure which would be best for what I want.
    This namespace stuff is confusing the hell out of me!

    Anyway, thank you all very much for your help.
    Soooo much still to learn, but I love learning and love Perl even more. What a wonderful language!