As in this
post I use HTML::Parser to extract the content of the a href tag.
$p->handler( start => \&a_start_handler, "tagname,self,attr" );
$p->unbroken_text( 1 );
$p->parse( $content ) || die $!;
foreach my $link ( @linklist ){
print $link->[0]; #link
print $link->[1]; #text
}
sub a_start_handler {
my( $tag, $self, $attr ) = @_;
# we only act on <a tags
return if $tag ne "a";
if( defined( $href = $attr->{href} ) ){
$self->handler(text => sub { $text = shift; $text =~ s/\n//g; },"d
+text");
$self->handler( end => \&a_end_handler, "tagname,self" );
}
foreach my $key ( keys %$attr ){
# print ">$key=$attr->{$key}\n";
}
}
sub a_end_handler {
return if shift ne "a";
my $self = shift;
push @linklist, [ $href, $text ] if defined $text && $text !~ /^\s*$
+/;
$self->handler(end => undef );
$self->handler(text => undef );
}
And as it should, it properly strips all html from it. But now, I need to get also all markup contained in the link. But as it seems, changing this line
$self->handler(text => sub { $text = shift; $text =~ s/\n//g; },"d
+text");
to
$self->handler( text => sub { $text = shift; $text =~ s/\n//g; },"text
+");
does not give the expected result (switch from getting
dtext to getting
text).
Any hint. Am I on the wrong track here (admitting that the parser interface is a bit hard to understand)?
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.