Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Writing a Perl module that dynamically loads other modules

by stephen (Priest)
on Mar 01, 2013 at 20:17 UTC ( #1021330=note: print w/ replies, xml ) Need Help??


in reply to Writing a Perl module that dynamically loads other modules

I'm assuming that you have some way of going from your file type to a module name (i.e. if the filetype is "foo" you load the "My::Module::Foo" module). I'm also assuming that you're not too worried about security, or that you're somehow validating these inputs.

You have a couple of choices for how to dynamically load a module based on the name.

Before we get into the technical aspect of how to do this, first we should consider *why* you want to do dynamic loading. If you have a manageable number of file formats, you can just 'use' them in your script (or put them all in the same file) and just call them when you need them. Something like:

# up at the top: use My::Format::Foo; use My::Format::Bar; # Down later ###################################### # Parse the Filetype info ###################################### if ( /^FILETYPE=(.*)\s*$/ ) { $self->{'FILETYPE'} = "$1"; next; } my $parser_class = 'My::Format::' . ucfirst $self->{'FILETYPE'}; my $data = $parser_class->parse($_);

One way is to use the Module::Load module from CPAN. That would let you do:

# Up top use Module::Load; # Down in the parsing my $module_name = 'My::Format::' . ucfirst $self->{'FILETYPE'}; load $module_name; my $data = $module_name->parse($_);

Alternatively, you can use 'require', like so:

my $module_filename = 'My/Format/' . ucfirst($self->{'FILETYPE'}) . '. +pm'; require $module_filename; my $module_classname = 'My::Format::' . ucfirst($self->{'FILETYPE'}; $module_classname->import(); my $data = $module_classname->parse($_);

If you can avoid the dynamic loading, I'd recommend doing so, and just 'use' the expected modules up front. That way you'll reduce complexity and see any errors at load-time. Also, it's a good idea to validate the module names somehow-- a typo in a format type will otherwise show up as a missing module error, and that can get confusing. Good luck!

stephen


Comment on Re: Writing a Perl module that dynamically loads other modules
Select or Download Code
Re^2: Writing a Perl module that dynamically loads other modules
by mgad (Novice) on Mar 01, 2013 at 20:52 UTC

    Stephen - Thanks for the info, I'll definitely take a look at Module::Load. To answer your questions:

    >>if the filetype is "foo" you load the "My::Module::Foo"

    Thats correct, that is exactly what I was planning to do.

    >>you're not too worried about security, or that you're somehow validating these inputs

    Correct and correct.

    >>If you have a manageable number of file formats

    That's the problem and exactly why I'm looking at loading the other modules dynamically. I'm already at 30+ filetypes and by the time everything is finished there will be around 75 total, maybe even more. Loading them all "up front" would be a bit of a pain.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (17)
As of 2014-09-16 14:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (24 votes), past polls