I was wondering if it’s possible to declare a global variable within a subroutine in Perl so that I can use that variable in a hooked void function, but limiting the damaging effects of the global by having it declared in a subroutine.
So the subroutine uses XML::Parser to collect the IDs of a bunch of elements, in a manner similar to:
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;
}
I’m also using strict in my script.
So I want to declare @elements in such a way that it is local to getRecipeIDs but is visible to the anonymous subroutine.
Thanks for your time, any help is greatly appreciated.
It should already work the way in which you’ve written your example. What you’re doing with “
my $func = sub { ... }” is you create a closure which has access to the enclosing scope’s variables — in this case@elements.