package HTML::TokeParser::Listerine; use strict; use warnings; use base 'HTML::TokeParser'; sub get_tag { my $self = shift; if (wantarray) { # build and return a list my @tags; while ( my $tag = $self->SUPER::get_tag(@_) ) { # delegate to superclass push @tags, $tag; } return @tags; } else { return $self->SUPER::get_tag(@_) } } sub get_token { my $self = shift; if (wantarray) { # build and return a list my @tokens; while ( my $token = $self->SUPER::get_token(@_) ) { # delegate to superclass push @tokens, $token; } return @tokens; } else { return $self->SUPER::get_token(@_) } } 1; __END__ =pod =head1 NAME HTML::TokeParser::Listerine - Context-sensitive HTML token parsing =head1 SYNOPSIS use HTML::TokeParser::Listerine; my $html = q { Bar
Foo
}; my $p = HTML::TokeParser::Listerine->new(\$html); # magically parse html with map rather than tedious while! # you could also use get_token to do this my @links = map { $_->[1]->{href} } $p->get_tag('a'); print "Links are: ", join("\n", @links), "\n"; =head1 DESCRIPTION HTML::TokeParser::Listerine overrides the C and C methods of HTML::TokeParser to make them DWIM in a list context, for example one provided by the C and C operators. This allows you to do terse complex filtering, rather than having to enter a big while loop everytime you want to parse HTML, which isn't easy on the eye. Obviously, this is a slower approach than doing it with a while loop, as internally it uses the same mechanism. It simply saves you typing, and that can be a lot more convenient than you think. =head1 METHODS The only difference to HTML::TokeParser is that if you use the methods C and C in list context they return a list of all the tags and tokens, respectively. Using it in scalar context should behave the same as vanilla TokeParser does. =head1 AUTHOR Amoe. =head1 REQUIREMENTS HTML::TokeParser and everything else that depends on. =head1 SEE ALSO HTML::TokeParser and HTML::PullParser manpages. =cut