perlquestion
bliako
<p>Dear Monks,</p>
<p>I have come across a situation where I have a module and a test script which <c>use</c>s it and all is fine when done using the perl interpreter from the command line. However, when I read the module file into a string and the test script into another string with the intend to <c>eval()</c> the strings one after the other, the operation collapses at the <c>eval()</c> of the test script string.</p>
<p>After some time, I realised why I was getting the error <c>Can't locate Module.pm in @INC</c> at the second eval which indeed has a <c>use Module;</c> statement. Obviously <c>Module.pm</c> is not in <c>@INC</c> and any <c>use</c> statement for it will fail even if said module has already been eval'ed successfully.
</p>
<p>So, I am asking whether there is an easier solution to this kind of problem than analysing each script with [mod://PPI] and removing <c>use/require</c> statements ONLY of modules already eval'ed. Because obviously there will be some <c>use/require</c> statements in there which must remain as they refer to modules which should be in <c>@INC</c>. I am doing the [mod://PPI] way right now but I hope there is a more natural way.</p>
<p>Here is a test which demonstrates the problem when the marked statements uncomment:</p>
<c>
#!/usr/bin/env perl
use strict;
use warnings;
sub load_modules {
my $mod1 = <<'EOM';
package Test::Module::Hello::Hello;
our $VERSION = 0.1;
use strict;
use warnings;
sub go {
return "Hi iam ".__PACKAGE__
}
1;
__END__
EOM
my $mod2 = <<'EOM';
package Test::Module::Hello::Goodbye;
our $VERSION = 0.1;
use strict;
use warnings;
# >>> This will kill the eval if uncommented
#use Test::Module::Hello::Hello;
sub go {
return "Hi iam ".__PACKAGE__
}
1;
__END__
EOM
eval($mod1) or die "$mod1\n\neval failed $@\n";
eval($mod2) or die "$mod2\n\neval failed $@\n";
}
load_modules();
my $testscript = '
# >>> Following use/require will kill the eval
#use Test::Module::Hello::Hello;
require Test::Module::Hello::Goodbye;
my $ret = Test::Module::Hello::Hello::go();
print "ret=$ret\n";
$ret = Test::Module::Hello::Goodbye::go();
print "ret=$ret\n";
';
eval($testscript) or die "eval failed, $@";
</c>
<p>thanks, bliako</p>