Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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;

Comment on Re: import() when require-d module name is variable
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2016-02-08 06:16 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (268 votes), past polls