mldvx4 has asked for the wisdom of the Perl Monks concerning the following question:
Greetings
I would like to pass a list of HTML::Element objects as a return value from a subroutine. I thought since the list would be just a list, that the normal approach would apply. Clearly I am misunderstanding something, or perhaps there is a more appropriate approach:
#!/usr/bin/perl use HTML::TreeBuilder::XPath; use HTML::Element; use HTML::Entities qw(decode_entities); use Data::Dumper; use strict; use warnings; my $html = &layer(3); print $html->as_XML,"\n"; exit(0); sub layer { my ($layer) = (@_); my $ul = HTML::Element->new('ul'); my $li = HTML::Element->new('li'); $li->push_content("Layer $layer"); $ul->push_content($li); if($layer--) { my $h = &layer($layer); my @c = &unescape_entities($h); # offending line print "Wrong structure:\n",Dumper(@c),"\n ----\n"; exit(1); $ul->push_content($h); } else { my $foo = ' foo < bar'; my $literal = HTML::Element->new('~literal', text=>$foo); $li->push_content($literal); $ul->push_content($li); } return($ul); } sub unescape_entities { my ($html) = (@_); my $tmp = HTML::TreeBuilder::XPath->new; $tmp->parse(decode_entities($html->as_XML)); my @c = $tmp->findnodes('//body/*'); print "Right structure:\n", Dumper(@c),"\n ----\n"; $tmp->delete; return(@c); # this is getting transformed }
I would expect that the script (minus the exit) to produce a nested HTML unordered list. What happens is that the variable actually recovered from the subroutine is basically empty, as seen by comparing the output from the two Dump calls.
I've looked at the manual pages for the modules given above as well as the one for perlrref. Please nudge me in the right direction.
|
---|