Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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.
HTH

_________
broquaint


Comment on Re: Is dynamic loading of pm's a bad thing?
Download Code
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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2015-07-30 05:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls