Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: including modules during runtime and dealing with OS specific (constants) code block by armstd
in thread including modules during runtime and dealing with OS specific (constants) code block by periferral

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-16 17:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found