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.


In reply to Passing a list as a subroutine's return value ( list of HTML::Element objects ) by mldvx4

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.