You are freeing the tree including all of its nodes before you're done with the nodes.
But it turns out you don't need to return the nodes. You just want the stringification of the elements, so stringify them in the sub instead of in the caller.
sub unescape_entities { my $html = shift; my $tree = HTML::TreeBuilder::XPath->new; $tree->parse( decode_entities( $html->as_XML ) ); my @c = map "$_", $tree->findnodes( '//body/*' ); $tree->delete; return @c; }
Better yet (since it properly handles exceptions):
use Sub::ScopeFinalizer qw( scope_finalizer ); sub unescape_entities { my $html = shift; my $tree = HTML::TreeBuilder::XPath->new; my $anchor = scope_finalizer { $tree->delete; }; $tree->parse( decode_entities( $html->as_XML ) ); return map "$_", $tmp->findnodes( '//body/*' ); }
Note: Does passing XML to HTML::TreeBuilder::XPath make sense???
In reply to Re: Passing a list as a subroutine's return value ( list of HTML::Element objects )
by ikegami
in thread Passing a list as a subroutine's return value ( list of HTML::Element objects )
by mldvx4
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |