Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: import() when require-d module name is variable

by f00li5h (Chaplain)
on Jul 18, 2009 at 22:15 UTC ( #781385=note: print w/replies, xml ) Need Help??

in reply to import() when require-d module name is variable

eval qq{ use $module_name }; die "can't $module_name because: $@";

Then perl does all the "finding it" magic ... and you still get to decide the module name at run time...

You could also loop over many alternate modules and ignore failures until you find one you want, but for that you might want Module::Find or similar too ...

use Module::Find qw[ useall ]; my @plugins = useall q/Foo::Bar/; for my $plugin_name (@plugins) { eval qq/require $plugin_name/; my $plugin = $plugin_name->new(); }

Although here my modules are pure OO, so they don't get imported

@_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://781385]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2018-06-21 18:55 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.