Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: including modules during runtime and dealing with OS specific (constants) code block

by armstd (Friar)
on Aug 02, 2011 at 17:14 UTC ( #918103=note: print w/ replies, xml ) Need Help??


in reply to including modules during runtime and dealing with OS specific (constants) code block

eval "use Win32::File";

You can just use 'require' instead if you just want it to happen at runtime.

require Win32::File;

I even go as far as using 'require' more than I use 'use' these days for modules it seems. You lose the questionable benefits of import(), but you're not taking advantage of import() in this example anyway. As chromatic already pointed out, you'll want to avoid barewords referring to code that hasn't been compiled yet, addressing them as functions by appending '()' will work fine. No need for the '&'.

In my experience, dynamically compiling during runtime is great, especially when loading a large number of modules over an NFS mount. Only loading what's needed as its needed will improve response time, reducing the initial compilation time. But compilation errors in the middle of execution can be bad, and that's why you really want the 'eval'. Not just to make use work at runtime, but to make sure runtime compilation errors are caught and handled in a user-friendly way.

This is like what I do in my Abstract Factories:

if( ! eval "require ${package}" ) ) { throw E_BadSubClass( "package '${package}' not found or failed + to compile.\n" . $@ . "\n" ); }

--Dave


Comment on Re: including modules during runtime and dealing with OS specific (constants) code block
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://918103]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2014-07-24 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (159 votes), past polls