Sea*_*her 1 xml perl anonymous-function subroutine
我想知道是否可以在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;
}
Run Code Online (Sandbox Code Playgroud)
我也在strict我的剧本中使用.
所以我想以@elements这样的方式声明它是本地的,getRecipeIDs但是对于匿名子例程是可见的.
感谢您的时间,非常感谢任何帮助.
| 归档时间: |
|
| 查看次数: |
765 次 |
| 最近记录: |