Perl:调试存储在字符串中并由eval调用的子例程 [英] Perl: debugging subroutines stored in strings and called by eval
问题描述
我在文件 A
中做了很多 regex
。基于解析,我创建了许多子例程,并将它们存储在子例程 f
中的 $ code
哈希中。
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;}",
}
}
code
的哈希值是生成的子例程,而键是其他一些字符串。
The hash values of code
are the generated subroutines and the keys are some other strings.
然后,使用子例程 g
,对文件做很多<code> regex
B 并获取一些键,然后在 eval
中通过 code
调用相应的子例程 $ code {$ key}($ x)
。
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)
}
}
我做这个丑陋的工作(而不是调用 f
)以避免多个 regex
超过 A
。但是,这使调试 sub f
变得很乏味-因为我没有来自 B
的实际键在 A
中。除非我在 $ code
本身中放置一些 print
语句。我避免这样做,因为我宁愿保持 $ code
的功能不变(没有LOG语句)。
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).
可以解决我的问题吗?
推荐答案
您可以将名称分配给 local * __ANON __
(请参阅在PerlMonks命名的匿名子邮件),也可以使用 Sub :: Name (顺便说一句,链接线程中的建议之一)。
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).
这篇关于Perl:调试存储在字符串中并由eval调用的子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!