问题
I do a lot of regex
on file A
. Based on my parsing, I create many subroutines and store them in a hash , $code
--in subroutine f
.
sub f
{
#REGEX on file A
$code = { "a0" => "sub { my $x = shift; .... return 0*x;}",
"a1" => "sub { my $x = shift; .... return 1*x;}",
....
"a9" => "sub { my $x = shift; .... return 9*x;}",
}
}
The hash values of code
are the generated subroutines and the keys are some other strings.
Then, using subroutine g
, I do lots of regex
on file B
and get some keys and then call the corresponding subroutine from code
by eval
ing $code{$key}($x)
.
sub g
{
#REGEX on file B to get the keys and x`s
foreach $keys{
print $code{keys}($x)
}
}
I do this ugly job (instead of calling f
) to avoid multiple regex
ing over A
. However, this makes debugging sub f
a tedious job --since I don't have the actual keys coming from B
in A
. Unless, I put some, print
ing statement in $code
itself. I avoid doing this, since I'd rather to keep the functionality of $code
as it is (with no LOG statement).
Any remedy for my problem?
回答1:
You can assign the name to local *__ANON__
(see Named anonymous subs at PerlMonks), or you can use Sub::Name (BTW, one of the suggestions in the linked thread).
来源:https://stackoverflow.com/questions/25319236/perl-debugging-subroutines-stored-in-strings-and-called-by-eval