use HTML::Parser; # Remove the s///g from this one to leave tags alone # Alternately, specify additional methods to alter only # specific token types sub tagpush {$_ = shift; s/foo/bar/g; $tags{length($html)} .= $_ ;} sub txtpush { $html .= "@_"; } my $p = HTML::Parser->new(unbroken_text => 1, text_h => [ \&txtpush, "text" ], default_h => [ \&tagpush, "text" ], ); my $file = shift || usage(); $p->parse_file($file) || die "Can't open file $file: $!\n"; $html =~ s/foo/bar/g; foreach my $point (sort {$b<=>$a} keys (%tags)) { substr($html, $point, 0 ) = $tags{$point}; }