Ask a Question related to PERL Beginners, Design and Development.
-
Papo Napolitano #1
Loading and using modules using eval
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!
Papo Napolitano Guest
-
Loading Perl Modules from same directory as script
Well, after only 6 hours of hacking around trying to do something that should be obvious in perl - but isn't - i seem to have found two solutions :... -
Loading Modules - Yipee!!
Owen. If you were standing next to me I'd give you a big kiss (no I'm not really like that, just a figure of speech) It worked, how, why I'm still... -
Loading Modules - More
On Tue, 23 Dec 2003 09:53:09 +1300 Support <support@emlgroup.co.nz> wrote: See my previous message, you need to make a a directory "Date" in... -
Loading Modules
Hi All I have a perl module I want to use in a perl script and make available by the ' use mymodule.pm' call. Plus I would like to place it the... -
eval'
I am retrieving the following string from the database. " and upper(gsa_template_data_header) >= '".$start_selection."' and... -
Drieux #2
Re: Loading and using modules using eval
On Jan 23, 2004, at 1:23 PM, Papo Napolitano wrote:
[..][..]> <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');
Why not try something a bit more vanilla
where one does the
use SomeModuleHere;
for all the modules you want to use. Then you
can use the no strict refs option IF you
really want to do the strictly functional approach.
I do not think that
eval("&${module}::process('$param1', '$param2')");
will do what you want it to do.
IF the Text::process and XML::process functions are things
that you are building out you may want to think about the
idea of doing the perl oo-ish aproach, as
$foo->showMe($line);
will work without requiring the no strict refs.
ciao
drieux
---
Some code to play around with would be:
#!/usr/bin/perl -w
use strict;
my ($foo, $line) ;
while(<DATA>){
chomp;
/^([\w:]+)\s+(.*)/;
($foo, $line) = ($1,$2);
# this assumes that the Package is External
# require "$foo.pm" if (!exists($INC{"$foo.pm"}));
# the way that will work by indirection
#$foo->showMe($line);
my $code = "${foo}::showMe";
no strict 'refs';
$code->($line);
# does not invoke the code
#eval{ &${foo}::showMe($line) };
if ($@)
{
print "Error: had \$foo -> $foo\n\t\$line -> $line\n$@\n#-----\n";
}
}
print "and now a Procecural call:\n" ;
Foo::showMe("Procedural\n$line\n");
BEGIN {
package Foo::Bar;
#------------------------
#
sub showMe
{
#my $me = shift if ( $_[0] eq __PACKAGE__);
my ($line) = @_;
print $line;
print "\n" unless $line =~ /\n/gim;
} # end of showMe
1;
package Foo;
#------------------------
#
sub showMe
{
#my $me = shift if ( $_[0] eq __PACKAGE__ or
# ref($_[0]) eq __PACKAGE__ );
my ($line) = @_;
print "foo sees:\n\t $line\n";
} # end of showMe
1; # so that the 'use Foo::Bar'
# will know we are happy
} # end begin
__DATA__
Foo word up
Foo::Bar not that one.
Foo This is a Happy Line
Drieux Guest
-
Papo Napolitano #3
Re: Loading and using modules using eval (SOLVED)
----- Original Message -----
From: "drieux" <drieux@wetware.com>
To: "Perl Beginners Mailing List" <beginners@perl.org>
Sent: Friday, January 23, 2004 19:53
Subject: Re: Loading and using modules using eval
Your method is much better, easier on the eyes and working flawlessly ;-)> IF the Text::process and XML::process functions are things
> that you are building out you may want to think about the
> idea of doing the perl oo-ish aproach, as
>
> $foo->showMe($line);
>
> will work without requiring the no strict refs.
>
> ciao
> drieux
Thanks!
Papo Napolitano Guest



Reply With Quote

