我想知道是否可以在 Perl 的子例程中声明一个全局变量,以便我可以在挂钩的 void 函数中使用该变量,但通过在子例程中声明它来限制全局变量的破坏性影响。
因此子例程用于XML::Parser
收集一堆元素的 ID,其方式类似于:
sub getRecipeIDs {
my $recipe = shift;
my @elements = ();
my $parser = new XML::Parser(Style => 'Tree',
Handlers => {
Start => sub {
my ($expat, $element, %attrs) = @_;
if ($element eq 'recipe') {
push @elements, $attrs{id};
}
}});
$parser->parse($recipe);
return @elements;
}
我也在strict
我的脚本中使用。
所以我想以@elements
这样一种方式声明它是本地的,getRecipeIDs
但对匿名子例程是可见的。
感谢您的宝贵时间,非常感谢您的帮助。