An example using HTML::Parser:
#! /usr/bin/perl
use warnings;
use strict;
use HTML::Parser;
my $p = HTML::Parser->new( api_version => 3,
start_h => [\&start, "tagname, attr, text, skipped_text"],
end_h => [\&end, "tagname, text, skipped_text"],
);
$p->parse_file(\*DATA);
my @switch_span_end;
sub start {
my($tag,$attr,$text,$skipped) = @_;
print $skipped;
unless ($tag eq 'span' && ($attr->{class}||"") eq "a") {
print $text;
return;
}
push @switch_span_end => 1;
print "<b>";
}
sub end {
my($tag,$text,$skipped) = @_;
print $skipped;
if (@switch_span_end && $tag eq "span") {
print "</b>";
pop @switch_span_end;
}
else {
print $text;
}
}
__DATA__
<span class="a">text</span><span class="a">text</span><span id="b">text</span>
Output:
<b>text</b><b>text</b><span id="b">text</span>