Hello all!
I am new to Perl and have a question. I have to see about converting my Python code to Perl for the project I am working on and I need to know how to go about doing a very specific part of my code.
I am Python beginner so if I miss something in the code below be sure to ask.....
The flow of the code is as follows:
Using an hash (perl term, dict in Python terms), select a string from the hash to get the name of a module to load dynamically.
Ok, are you good with loading your hash?
Load the module into the interpreter dynamically Using another string in the same hash, execute a function in the dynamically loaded interpreter using the value in the string as the name of the function, passing in a set of arguments.
In general this is a pretty easy thing to do in Perl....
The python code is basicly as follows: 'entry' is the hash (dict) that contains the module and function names.
try: mod = __import__(entry['module']) except ImportError, msg: log('Failed to load module %s: %s' % (entry['module'],msg)) sys.exit(0)
The try/catch idiom is built with 'eval' in Perl. See example below. The trickiest part about this is that 'use' must take a bareword, and accessing into a hash is not a bareword, which means you have to either use the string execution form of 'eval' or switch to a 'require'.
if entry['test'] not in mod.__dict__: log('Failed to run test %s in module %s: Does not exist' % (entry['test'],entry['module'])) sys.exit(0) while 1: try: rtn = mod.__dict__[entry['test']](entry) except:
Again we can use 'eval' to do the "running" and reporting of errors... I am not entirely sure I understand the '(entry)' in the above Python code, sure it isn't wrong just my being a newbie...
import traceback, StringIO
r = StringIO.StringIO()
r.write('<HR>There was a software failure in
"%s.%s".</HR>' % (entry['module'],entry['test']))
r.write('Please report this entire message to
XXXXX<BR>')
r.write('<PRE>')
traceback.print_exc(file=r)
r.write('</PRE>\n')
For example, you can mess with the values assigned in %hash below to cause failures to see the try/catch model work:
-- UNTESTED --
#!/usr/local/bin/perl use strict; use warnings;
# setup our values my %hash = ('module' => 'CGI', 'method' => 'new');
# attempt to load module, catch failures eval "use $hash{'module'};"; if ($@) { die "Failed to load module $hash{'module'}: [EMAIL PROTECTED]"; }
# check to see if we can call 'method' this step can be
# skipped in Perl, the failure will be caught in the below eval
unless (UNIVERSAL::can($hash{'module'}, $hash{'method'})) {
die "Failed to run test $hash{'method'} in module $hash{'module'}: Does not exist\n";
}
# execute the code, catch a failure
my $result = eval {
my $method = $hash{'method'};
$hash{'module'}->$method();
};
if ($@) {
die "There was a software failure in $hash{'method'}.$hash{'module'}: [EMAIL PROTECTED]";
}
print "Return result good: $result\n";
In the above the temporary setting of $method might be avoidable but I couldn't do it quickly. You should be aware of a few caveats mentioned in these docs,
perldoc -f use perldoc -f eval perldoc -f UNIVERSAL
Specifically about AUTOLOADED methods.
Not sure of your intentions, if you are writing a test suite to test Perl code there are better ways to go about this.
HTH,
http://danconia.org
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>