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


In reply to Re: Writing a Perl module that dynamically loads other modules by stephen
in thread Writing a Perl module that dynamically loads other modules by mgad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.