|Pathologically Eclectic Rubbish Lister|
Interesting behavior in eval blockby l2kashe (Deacon)
|on Jun 19, 2008 at 18:25 UTC||Need Help??|
l2kashe has asked for the
wisdom of the Perl Monks concerning the following question:
I have run into a situation with dynamically load available modules at runtime via eval. There is magic afoot I don't fully appreciate, so I am hoping someone can point to the right doc/direction on what is going on.
I have a fairly large codebase which needs to be deployed in a variety of situations/environments. Given the nature of the code, it can have an excessive number of module requirements to cover all the scenarios it can be leveraged for. Instead of doing some type of preinspection of the available modules and then reconfiguring the codebase on the fly or trying to download modules from CPAN, I wanted to wait until runtime and let user configurable data drive what modules should be loaded.
There are 2 main usage patterns, simply use()ing a module, and use()ing a module with a list of keywords to import into the application (IE. use Time::HiRes; vs use Time::HiRes qw(gettimeofday tv_interval). So a simple hash gets the trick done in terms of handling the data requirements The issue comes in with using eval causing keywords to not be imported into the namespace cleanly as far as I can tell.
After a number of posts about the syntax of the code not being valid I have replaced the code with the actual method being used. This is within a package which uses strict. I have added a third example which a contrived object using this information
Changing the line now = line to [gettimeofday()] it works, I assume by hinting to the runtime environment that this is in fact a routine and not a string.