Tanktalus has asked for the wisdom of the Perl Monks concerning the following question:
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);
eval "require $module"; my $obj = $module->new($options);
(my $filename = $module) =~ s[::][/]g; $filename .= '.pm'; eval { require $filename }; my $obj = $module->new($options);
Just looking to broaden my thought processes. :-)
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Loading arbitrary modules
by ikegami (Patriarch) on Mar 16, 2010 at 16:00 UTC | |
|
Re: Loading arbitrary modules
by Anonymous Monk on Mar 16, 2010 at 17:13 UTC | |
|
Re: Loading arbitrary modules
by shmem (Chancellor) on Mar 16, 2010 at 20:05 UTC | |
|
Re: Loading arbitrary modules
by dsheroh (Monsignor) on Mar 17, 2010 at 11:00 UTC |