Loading and using modules using eval

From: Papo Napolitano (papo_at_dosalcubo.com)
Date: 01/23/04


To: beginners@perl.org
Date: Fri, 23 Jan 2004 18:23:10 -0300

Hi all,

I have this piece of code:

my @modules = (
 'Module1', 'Module2', 'Module3',
 'ModuleX', 'ModuleY', 'Blah',
);
my $param1 = "whatever";
my $param2 = 0;
for my $module (@modules) {
  $param2++;
  eval("require $module");
  if ($@) {
    print "$module Not found\n";
  } else {
    eval("&${module}::process('$param1', '$param2')");
  }
}

And it's working, but I wonder if there's any cleaner/better way to do this.
The main idea is to have a XML file like this:

<xml>
  <file source="file1.txt" module="TextFile" parameters="1"/>
  <file source="file2.csv" module="TextFile" parameters="2"/>
  <file source="file3.xml" module="XMLFile" parameters="this and that"/>
</xml>

To tell me I have to do:

&TextFile::process('file1.txt', '1');
&TextFile::process('file2.csv', '2');
&XMLFile::process('file3.xml', 'this and that');

Got it? ;-)

Thanks!