I have many standalone scripts. The only thing they share, is that they use() a large set of CPAN modules (that each export several functions). I would like to centralize this list of modules. I found several methods. Which one is the best?
-
I could create SharedModules.pm that imports everything and then manually exports everything to main:: using Exporter.
-
I could create SharedModules.pm that starts with “package main;” so it will import directly into main::. It seems to work. Is it bad practice and why?
-
I could require() a sharedmodules.pl that seems to import everything into main:: as well. I don’t like this method as require() doesn’t work that well under mod_perl.
Number two looks best to me, however I wonder why for example Modern::Perl doesn’t work that way.
Edit: I figured this question has been asked before.
The problem with all three of your proposed solutions is that the module may be
used from another module, in which case the symbols should be exported into theuseing module’s package, not intomain.bvr’s solution of using
callerto import things directly into that package is a major step in the right direction, but prevents the ‘real’ package from usinguse ShareableModules qw( foo bar baz);to selectively import only what it actually needs.Unfortunately, preserving the ability to import selectively will require you to import all relevant symbols from the underlying modules and then re-export them from ShareableModules. You can’t just delegate the import to each underlying module’s
importmethod (asModern::Perldoes) becauseimportdies if it’s asked for a symbol that isn’t exported by that module. If that’s not an issue, though, thenModern::Perl‘s way of doing it is probably the cleanest and simplest option.