Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

HTML::TokeParser Line numbers

by jithoosin (Scribe)
on Nov 24, 2005 at 12:16 UTC ( #511418=perlquestion: print w/ replies, xml ) Need Help??
jithoosin has asked for the wisdom of the Perl Monks concerning the following question:

HI monks
i am parsing an html file using HTML::TokeParser. The code is
use HTML::TokeParser; my $p = HTML::TokeParser->new($ARGV[0]); while (my $token = $p->get_token() ) { my @tag = @{$token}; if( $tag[0] eq 'S' || $tag[0] eq 'E' ){ print "$tag[@tag-1]\n" ; } }

The question is i need to print the line number along with start or end tag. Is it possible?

Comment on HTML::TokeParser Line numbers
Download Code
Replies are listed 'Best First'.
Re: HTML::TokeParser Line numbers
by Tomte (Priest) on Nov 24, 2005 at 14:02 UTC

    Because HTML::TokeParser is a HTML::Parser, you can try to add handler-subs for start/end events with an argspec of 'line'.

    Something like

    use HTML::TokeParser; my $line; sub lineHandler { $line = $_[0]; } my $p = HTML::TokeParser->new($ARGV[0]); $p->handler(start => \&lineHandler, 'line'); $p->handler(end => \&lineHandler, 'line'); while (my $token = $p->get_token() ) { my @tag = @{$token}; if( $tag[0] eq 'S' || $tag[0] eq 'E' ){ print "$line: $tag[@tag-1]\n" ; } }
    This is completly untested and "just written" after a quick scan of the HTML-Parser documentation, but I hope it helps.

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

      Sorry.It did not work and i got error message Can't set handlers for HTML::PullParser at t1.pl line 10

        Hmm, unfortunate

        Maybe it's a viable alternative then, to look at HTML::Parser instead and use it directly to solve your problem ?

        regards,
        tomte


        An intellectual is someone whose mind watches itself.
        -- Albert Camus

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://511418]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2015-07-30 16:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls