The logic would be like this:
- Set up a default handler (default_h) which has access to the tag name and unparsed HTML.
- Set up a callback to look for table cells.
- If you haven't found a table cell, spit out the unparsed HTML.
- If you have found the start tag of a table cell, set a flag. You know any concurrent calls to your callback will be content within the table cell, until you hit a closing tag.
use HTML::Parser;
my $found = 0;
sub callback
{
my($tagname, $text) = @_;
if($tagname and $tagname eq 'td') {
if(not $found) {
# start tag.
$found = 1;
}
else {
# end tag.
$found = 0;
}
return;
}
print $text and return unless $found;
# If you're here, then the markup is within the td tags.
}
my $p = HTML::Parser->new(api_version => 3,
default_h => [\&callback,'tagname, text']);
Hope this helps..