in reply to Loading an unknown module

Firstly the BEGIN block is going to happen at compile-time so it's irrelevant that's it's in the function. If you want to include a module at run-time you could use the tried and tested eval() & use()
sub routine { my $module = shift; my $routine = shift; my @params = @_; eval qq{ use "Foo::Bar::$module"; }; no strict 'refs'; return &{"Foo::Bar::$module::$routine"}(@params); }
There's also the very handy autouse module which should do the magic for you
sub routine { my $module = shift; my $routine = shift; my @params = @_; require autouse; autouse->import( "Foo::Bar::$module", "Foo::Bar::$module::$routine" ); no strict 'refs'; return &{"Foo::Bar::$module::$routine"}(@params); }
I'm not sure if that's exactly how it should be used so checkout merlyn's Objects of runtime-determined classes for reference.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Loading an unknown module
by lestrrat (Deacon) on Jun 28, 2002 at 00:05 UTC

    I don't think there's any point in using autouse that way, as the main purpose of autouse is to delay loading of the module from compile time to run time and if you call autouse dynamically, that's pretty much the same thing as using a straight eval.

      OMG, someone recommending against modules! <simpsons voice="Chief Wiggum">Get 'em boys</simpsons>. Seriously though the reason for using autouse in this case is the same as any situation in where you can use a module over rolling your own. There's also the fact that autouse != eval 'use Module'. Although it's not necessary I think I'd prefer to use a core module over my own code, but as always TIMTOWTDI.
      HTH

      _________
      broquaint