Whether this is purely arbitrary, or following a specific format for, e.g., plugins, how do you load a module whose name is in a string? I'm also mostly thinking of OO-style modules where importing is not simply unnecessary, but undesirable. Arbitrarily loading strict is somewhat of a waste.
Off the top of my head, I see three basic code approaches. I'm not including checks for loading success since they're basically the same everywhere. I'm also not including how $module is determined since that, too, doesn't change here, e.g., you could be getting it from a template a la Template Toolkit, and then putting "Template::Plugin::" in front, or it could be a command-line parameter, or both (such as ack), or any number of other options.
eval "use $module";
my $obj = $module->new($options);
- Simply use the perl interpreter to interpret and run the exact code that you would type in if it were a static module name. Straight-forward, and, from what I can tell, it's also the most common approach. Calls $module->import under the covers which is likely unnecessary, perhaps polluting your namespace if the module does export stuff, which seems to me as it may introduce a surprise under some circumstances. And, in general, has all the downsides of eval STRING: code injection. Admittedly, there are ways around that, but I rarely see anyone try.
eval "require $module";
my $obj = $module->new($options);
- Same as before, but avoids importing anything. I rarely see this.
(my $filename = $module) =~ s[::][/]g;
$filename .= '.pm';
eval { require $filename };
my $obj = $module->new($options);
- Avoids eval STRING horrors. Probably is faster than re-invoking the perl interpreter for figuring out what to use, but that also probably gets swallowed up by re-invoking the perl interpreter to parse/compile the module itself, so speed probably is not a significant factor. Avoiding code injection would be. Also, since this is a require, like the previous option, it's more obvious that importing is not happening. Disadvantage is that it's extra code that the perl interpreter would do for you if you were using an eval STRING.
So, my questions are:
- What format do you use today, if any?
- If this node is making you think about it more than you have, which format are you now thinking of using?
- What would you recommend to others?
- Why? i.e., Why were you using the format you were using, if any; why are you thinking of changing, if you are; why would you make that recommendation, especially if it's different from what you'll use in the future?
Just looking to broaden my thought processes. :-)
Thanks!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.