Recommend to use a different HTML::Parser method. get_tag is attractive, but insufficient for your task. The following rips through grabbing all URLs. If it encounters an ignore comment tag as shown, it will rip through until it gets to an anchor end tag.
#!/usr/bin/perl -w
use strict;
use HTML::TokeParser;
my $sample_HTML =
"<a href=\"http://www.foobar1.com/\">link 1</a> " .
"<!--ignore--><a href=\"http://www.foobar2.com/\">link 2</a> " .
"<a href=\"http://www.foobar3.com/\">link 3</a> ";
my $p = HTML::TokeParser->new( \$sample_HTML );
my $token;
my $link_count = 1;
while( $token = $p->get_token() ) {
if( $token->[0] eq 'S' &&
$token->[1] eq 'a' )
{
my $text = $token->[2]->{'href'};
print "Found link $link_count: $text\n";
$link_count++;
}
if( $token->[0] eq 'C' &&
$token->[1] eq '<!--ignore-->' )
{
while ( $token->[0] ne 'E' &&
$token->[1] ne 'a' )
{
$token = $p->get_token();
}
}
}
__output___
%perl ignore_some.pl
Found link 1: http://www.foobar1.com/
Found link 2: http://www.foobar3.com/
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.