Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Is dynamic loading of pm's a bad thing?

by broquaint (Abbot)
on Jun 25, 2004 at 02:44 UTC ( #369537=note: print w/replies, xml ) Need Help??

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.


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"; }


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

    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/ 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,


      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.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2016-10-26 10:01 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (338 votes). Check out past polls.