#!/usr/bin/perl -- use strict; use warnings; use HTML::TreeBuilder; my $html = <<'__HTML__';

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

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__