http://www.perlmonks.org?node_id=369537


in reply to Is dynamic loading of pm's a bad thing?

With a little help from you friendly neighbour CPAN modules this is a pretty standard plugin loader
use Carp 'croak'; use Module::Locate 'locate'; sub get_a_parser { my $mod = "EP::Parser::$_[0]"; require locate $mod or croak "Couldn't locate module '$mod'"; return $mod->new( @_[ 1 .. $#_ ] ); }
See. Module::Locate and Carp for more info.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re^2: Is dynamic loading of pm's a bad thing?
by CassJ (Sexton) on Jun 28, 2004 at 17:00 UTC
    If anyone's still interested, I did it like this in the end...
    sub create_data_parser{ my $class = shift; my %params = @_; my $parser = "EP::Core::Data::Parser::".$params{data_type}; require (locate $parser) ? return $parser->new(%params) : die "Cou +ldn't load parser"; }

    Cxx

Re^2: Is dynamic loading of pm's a bad thing?
by CassJ (Sexton) on Jun 28, 2004 at 14:57 UTC
    Hi,

    Thanks for your suggestion. I had a look at Module::Locate, but I'm not sure I quite understand how it works.

    Does locate $mod do what was suggested in one of the above posts and change a blah::blah string into a blah/blah.pm string then see if that pm exists? If so, does this mean that I don't have to worry about the fact that $mod is user defined cos unless the path actually corresponds to a pm file then locate'll fail?

    Thanks again,

    Cxx

      What it does is searches for a given module in the library path i.e it will find the path to the blah::blah module, or return false. Under the covers it will munge blah::blah into the appropriate format. And yes, you don't have to worry that $mod is user defined, if it exists in the path, you will get that path returned, otherwise, false.
      HTH

      _________
      broquaint