Need input for  HTML::Element->new_from_lol but all you got is html?

No problem :)

#!/usr/bin/perl -- use strict; use warnings; use HTML::TreeBuilder; my $html = <<'__HTML__'; <p> Need input for <code> HTML::Element->new_from_lol </code> but all you got is html? <p> No problem :) __HTML__ my $t = HTML::TreeBuilder->new(); $t->parse($html); $t->eof; $t->dump; $t->dump_as_lol; #~ print $t->as_lol_str,"\n"; #~ use DDS; Dump( $t->as_lol) ; #~ ROUND TRIP print $t->as_HTML, "\n"; print HTML::Element->new_from_lol( $t->as_lol )->as_HTML, "\n"; exit 0; sub HTML::Element::as_lol { package HTML::Element; use integer; use strict 'refs'; my ( $self, $depth, $ref ) = @_; $depth = 0 unless defined $depth; my $root = [ $self->tag ]; foreach $_ ( @{ $$self{'_content'}; } ) { if ( ref $_ ) { push @$root, $_->as_lol( $depth + 1 ); } else { push @$root, $_; } } return $root; } sub HTML::Element::as_lol_str { my( $self ) = @_; open my($fh), '>', \my($str ); $self->dump_as_lol ( $fh ); close $fh ; return $str; } sub HTML::Element::dump_as_lol { package HTML::Element; require Data::Dump; use integer; use strict 'refs'; my($self, $fh, $depth, $ref ) = @_; $fh = *STDOUT{'IO'} unless defined $fh; $depth = 0 unless defined $depth; #~ print $fh ' ' x $depth, $self->starttag, ' @', $self->address, +$$self{'_implicit'} ? " (IMPLICIT)\n" : "\n"; print $fh ' ' x $depth , "[\n"; print $fh ' ' x ($depth + 1), Data::Dump::dump($self->tag ), ",\n" +; foreach $_ (@{$$self{'_content'};}) { if (ref $_) { #~ print $fh ' ' x ($depth + 1), "[\n"; $_->dump_as_lol ($fh, $depth + 1); #~ print $fh ' ' x ($depth + 1), "],\n"; } else { #~ print $fh ; print $fh ' ' x ($depth + 1), Data::Dump::dump( $_ ), ",\n " +; } } print $fh ' ' x $depth , "],\n"; return; }; __END__

In reply to HTML::Element::as_lol , HTML::Element::as_lol_str , HTML::Element::dump_as_lol by Anonymous Monk

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.