S
shanx
I am not a perl newbie, but I am really stumped by this eval problem.
I cannot change a lot of the existing code since it is legacy stuff, so
any solution to redo this in a totally different manner won't work for
me.
The Problem:
A perl program called bug_find includes a test-suite library called
foo.pl using 'do'. bug_find has 'use strict' declaration and it passes
a perl -c, (we're using perl 5.6), but foo.pl doesn't have a 'use
strict' declaration. The relevant parts of the code from both files are
shown below:
####bug_find####
...
sub run_test {
my ($ref) = @_;
my $command = $ref->{cmd};
local $results = {};
open(FH, "$command 2>&1 |") or die $!;
my @output = <FH>;
$results->{output} = @output;
if (exists($ref->{post}) and defined($ref->{post}) {
eval $ref->{post};
}
return $results;
}
...
# What follows is main part of bug_find.
{
do $suite; # let us say $suite is foo.pl
foreach $i (@tests) {
$result = run_test($i);
}
}
####end of bug_find####
####start of foo.pl (or whatever $suite is)####
@specific_tests = (
{
'cmd' => "cleartool $blah $blah", # This is some shell
command
'post' => "$gotit = $1 if ($results->{output} =~ m/success
(\w+)/); print \"Value of gotit is $gotit\n\"",
},
{
'cmd' => 'cd /var/tmp/$gotit; ./do_something.sh',
}
);
@tests = (@specific_tests);
###end of foo.pl####
Note that the variable @tests in foo.pl is being used by bug_find after
'do'.
My problem is that when I run bug_find, I see the correct value of
$gotit in the print statement but when I try to read the value of
$gotit (just before do_something.sh), the variable seems to be unset
and it is empty.
What is going on?
-Shanker
I cannot change a lot of the existing code since it is legacy stuff, so
any solution to redo this in a totally different manner won't work for
me.
The Problem:
A perl program called bug_find includes a test-suite library called
foo.pl using 'do'. bug_find has 'use strict' declaration and it passes
a perl -c, (we're using perl 5.6), but foo.pl doesn't have a 'use
strict' declaration. The relevant parts of the code from both files are
shown below:
####bug_find####
...
sub run_test {
my ($ref) = @_;
my $command = $ref->{cmd};
local $results = {};
open(FH, "$command 2>&1 |") or die $!;
my @output = <FH>;
$results->{output} = @output;
if (exists($ref->{post}) and defined($ref->{post}) {
eval $ref->{post};
}
return $results;
}
...
# What follows is main part of bug_find.
{
do $suite; # let us say $suite is foo.pl
foreach $i (@tests) {
$result = run_test($i);
}
}
####end of bug_find####
####start of foo.pl (or whatever $suite is)####
@specific_tests = (
{
'cmd' => "cleartool $blah $blah", # This is some shell
command
'post' => "$gotit = $1 if ($results->{output} =~ m/success
(\w+)/); print \"Value of gotit is $gotit\n\"",
},
{
'cmd' => 'cd /var/tmp/$gotit; ./do_something.sh',
}
);
@tests = (@specific_tests);
###end of foo.pl####
Note that the variable @tests in foo.pl is being used by bug_find after
'do'.
My problem is that when I run bug_find, I see the correct value of
$gotit in the print statement but when I try to read the value of
$gotit (just before do_something.sh), the variable seems to be unset
and it is empty.
What is going on?
-Shanker