Including modules and pragmas in caller's scope via useby wanna_code_perl (Friar)
|on Jul 29, 2014 at 20:49 UTC||Need Help??|
wanna_code_perl has asked for the wisdom of the Perl Monks concerning the following question:
I already have several Local::... modules that do things too specific for general public consumption. Now I have a "smaller" problem:
Like many programmers, I have an ever-growing repository of useful subroutines that are either too isolated or too simple to (yet...) merit their own module (in the Local:: module namespace or otherwise). I also have a list of modules and pragmas I use in almost every script. However, these are things I'd like to have available in most of my internal-use scripts.
The subroutines? Easy. Just pile them in a new module (say, Local::Junk and @EXPORT them by default (or with Exporter::Easy and qw(:all) if I'm feeling extra pedantic...).
In other words, I'd like to be able to simply do something like this:use Local::Junk qw(:all);
I tried this in Local/Junk.pm:
With the above import() code, I can turn on warnings and strict just by using my module. But autodie doesn't work in the caller, and neither does List::Util, not even if I fully qualify the subroutines like List::Util::sum(...) in the caller.
Hopefully with the above, you get an idea of what I'm trying to accomplish. Again, in short, I want to be able to use Local::Junk and have that behave as if I'd called use ... on the list of modules and pragmas above (strict, List::Util, et. al.) Assuming it's possible to do so, how would I accomplish that?
Some other minor notes:
(The rest is not essential to the core question, but I anticipate a few of these points might come up):
My scripts are run on several servers. In-house modules are automatically distributed on our network via a local CPAN repository, so it would be more convenient to use a module, rather than separately copy a library and require '/path/to/mylib.pl'; on multiple OSes. (And even then, while require 'mylib.pl' did work with importing List::Util, it did not work with autodie. That is, I could call sum but open $fh, '<', 'nonexistent' failed silently).
I'm aware of distributions like Modern::Perl and common::sense. Without making value judgments on these types of things, I will say that, a) these modules only deal with basic pragmas and features, not more elaborate pragmas (e.g., autodie) and full modules (e.g., List::Util), and b) As a Local:: module, Local::Junk and any scripts/internal modules that use it will never see the (public) light of day, so there is no danger of this module imposing my will on unsuspecting programmers. Lastly, c) if I ever do decide to package up one of my scripts for wider-scale distribution (CPAN or otherwise), yes, I will have to expand the use statements and possibly pull some code into the script or a public module, but that amount of work would typically be tiny in comparison to the larger scope of expanding the documentation, unit tests, and the act of distribution itself.
Besides, now that I think of it, it'd probably be pretty easy to export a sub that would take the source code of $0 and s/use Local::Junk;/$expanded_use/s and output that for release...