Rob Dixon wrote: > > Rob Dixon wrote: > > > > If you have a fixed piece of HTML that you want to add then > > you would still be better off coding it up using 'new_from_lol', > > but if the content varies then you could package the lines > > above as a subroutine: > > > > > > sub html_element { > > my $html = shift; > > my $element = HTML::TreeBuilder->new_from_content($html); > > $element->look_down('_implicit', undef); > > } > > > > > > and then call it like this > > > > > > my $r = $ele->postinsert(html_element($tag)); > > Another point: this will work, but will result in a memory > leak as HTML::Treebuilder objects aren't destroyed > automatically when they go out of scope. This way is better: > > sub html_element { > my $tree = HTML::TreeBuilder->new_from_content(shift); > my $element = $tree->look_down('_implicit', undef); > $element->detach; > $tree->delete; > $element; > }
I'll shut up after this post! I've just been re-reading the POD for HTML::TreeBuilder, and note there is a method 'disembowel' which does exactly what I've coded above, which makes things a lot neater. It looks like this (but don't forget that you still need to call $element->delete when you're done with using the code fragment). HTH, Rob #!perl use strict; use warnings; use HTML::TreeBuilder; my $insert= <<TEST; <tr> <td> some more tags here </td> </tr> TEST my $element = HTML::TreeBuilder->new_from_content($insert)->disembowel; print $element->as_HTML; **OUTPUT** <tr><td> some more tags here </td></tr>