One problem when calling require with a variable scalar, is that the format must be different: the magic conversion you get when writing require Foo::Bar; no longer works: you have to convert 'Foo::Bar' to 'Foo/Bar.pm' yourself. So this should, basically, work:
Note that any class method ("run") you call this way, will get one extra argument prepended, in front of any of your own: the class name, the string that's currently in $module.use strict; our $module = 'Foo::Bar'; my $file = join('/', split /::/, $module) . '.pm'; require $file; $module->run();
p.s. For reasons of sanity, I personally would prefer an extra root in front of the actual module names, for example 'MyDrivers' (bad example :-), so you actually load and run MyDrivers::Foo::Bar, if the user supplies the string 'Foo::Bar'. That way, you can make sure you can only load modules that are especially prepared to be used this way.
p.p.s. People using older perls than 5.6.0 should replace the 'our' with another appropriate solution (use vars or my), or simply not use strict :-). This has nothing to do with the functionality of the code I described here. I would think that should still work.
In reply to Re: Loading a module at runtime...
by bart
in thread Loading a module at runtime...
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |