in reply to nested subroutines
(Note the reordering there) sub with no name creates an anonymous sub and returns a reference, so just do that first and use the reference it returns rather than taking a reference to a named sub.my $sub_handle = sub { my($expat, $element, %attrs) = @_; if($element eq $targetElement){ print "Target = $targetElement\n"; if(%attrs){ while(my($key, $value) = each(%attrs)){ if($key eq $targetAttribute){push @results,$value;} } } } } my $parser = XML::Parser->new(Handlers => {Start => $sub_handle}); my @results; print "$location, $targetElement, $targetAttribute, $file\n"; $parser->parsefile($file); if($location eq 'first'){return $results[0]} if($location eq 'last'){return $results[-1]} else {return @results}
|
|---|