I would use pQuery, but this will work
#!/usr/bin/perl -- use strict; use warnings; use HTML::TreeBuilder; my $html = <<'__HTML__'; <div id="target">old <B>i</B><I>n</I>ner</div> __HTML__ { my $t = HTML::TreeBuilder->new_from_content($html); print $t->as_HTML('<>&',' ',{}), "\n"; my $target = $t->look_down( id => 'target' ); $target->delete_content; $target->push_content( HTML::TreeBuilder->new_from_content( "<B>NEW</B>" )->look_down(qw!_tag body!)->detach_content ); print $t->as_HTML('<>&',' ',{}), "\n"; } __END__ <html> <head> </head> <body> <div id="target">old <b>i</b><i>n</i>ner</div> </body> </html> <html> <head> </head> <body> <div id="target"><b>NEW</b></div> </body> </html>
Yes, I am RTFM
source share