[Chicago-talk] Is there a way to simplify including multiple .pm files?

Mike Fragassi frag at ripco.com
Fri Mar 14 11:48:37 PDT 2008



On Fri, 14 Mar 2008, He Guangsheng-W16844 wrote:

> Good day.
>
> New to chicago.pm. I have a question:
>
> My scripts take a lot of constants, so I defined these constants in
> several ConstantVar.pm files and include them all. But more and more
> constant files coming, so I have to add one line 'use ...'  to each of
> my scripts. Is there a better way to manage this?
>
> In C, we can make one header include a lot of headers, the only need to
> maintain changes for this one. Is that similar approach in Perl?
>

This won't work with the standard Perl approach, i.e. using Exporter or 
Exporter::Lite; you'll still have to add 'use ...' to each script or 
package that needs the constants.

If all your scripts really, truly need _all_ the constants from all the 
.pm files, and it doesn't make sense to dump them all into a single and 
possibly huge module, you would be better off creating one intermediary 
pm, that itself uses Exporter.  This module could load all the .pm files 
with the constants, and then export all the constants via @EXPORT or 
%EXPORT_TAGS.  Then, each script would just use this module, and get 
everything.  When it comes time to add a new .pm, you add it in only one 
place, the new intermediary module, and not in each script.

Alternatively, you can do the same kind of thing in an OO style: the 
intermediate module would give you an object, say $constants, with the 
other modules as parents via 'use base'.  You'd call methods on this 
object to fetch constants from the parent classes.  This avoids having 
to fiddle with Exporter variables, but if any of your constants are 
class variables, you will have to write methods to access them.

-- Mike F.



More information about the Chicago-talk mailing list