Any pointers on the following would be appreciated. The code below correctly displays the links for a given HTML file. However, (between the #####s) I would like to add callbacks for (eg) the <title> tag, and display it's text as well. The $attr->{href}; line will obviously not display the text for a <title> tag, but what will? The print Dumper($attr); line shows no argument coming through for the title tag... Thanks henka
#!/usr/bin/perl -w # This program will print out all <a href=".."> links in a # document together with the text that goes with it. use HTML::Parser; use Data::Dumper; my $p = HTML::Parser->new(api_version => 3, start_h => [\&a_start_handler, "self,tagname,attr"], ##### report_tags => [qw(a img mailto title)], ##### ); $p->parse_file(shift || die) || die $!; sub a_start_handler { my($self, $tag, $attr) = @_; return unless $tag eq "a" || $tag eq "img" || $tag eq "mailto" || ##### $tag eq "title"; ##### print Dumper($attr); # display args return unless exists $attr->{href}; print "A $attr->{href}\n"; $self->handler(text => [], '@{dtext}' ); $self->handler(start => \&img_handler); $self->handler(start => \&mailto_handler); ##### $self->handler(start => \&title_handler); ##### $self->handler(end => \&a_end_handler, "self,tagname"); } ##### sub title_handler { my($self, $tag, $attr) = @_; return unless $tag eq "title"; push(@{$self->handler("text")}, $attr->{alt} || "[TITLE]"); } ##### sub img_handler { my($self, $tag, $attr) = @_; return unless $tag eq "img"; push(@{$self->handler("text")}, $attr->{alt} || "[IMG]"); } sub mailto_handler { my($self, $tag, $attr) = @_; return unless $tag eq "mailto"; push(@{$self->handler("text")}, $attr->{alt} || "[MAILTO]"); } sub a_end_handler { my($self, $tag) = @_; my $text = join("", @{$self->handler("text")}); $text =~ s/^\s+//; $text =~ s/\s+$//; $text =~ s/\s+/ /g; print "T $text\n"; $self->handler("text", undef); $self->handler("start", \&a_start_handler); $self->handler("end", undef); }

In reply to HTML::Parser help by henka

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.