How to know the input line number when using parse_file of HTML::Parser

Discussion in 'Perl Misc' started by Himanshu Garg, Feb 2, 2004.

  1. Hello,

    I am using HTML::parser and want to know the line number where a
    particular event occurs. Given below is an illustrative example :-
    -----------------------------------------------------------
    use strict;
    use HTML::parser;

    # Create parser object
    my $p = HTML::parser->new( api_version => 3,
    start_h => [\&start, "tagname"],
    );
    $p->report_tags("s"); # report only s event
    $p->parse_file(*STDIN);



    sub start
    {
    warn "<s> found in $."; # HOW TO PRINT THE LINE NUMBER
    }

    ----------------------------------------------------------------

    Although warn normally prints the input file line number on its own,
    here it doesn't do that when I print it explicitly (using $.). I get
    the following error message :-

    Use of uninitialized value in concatenation (.) or string at
    ../parsehtml.pl line

    Could you suggest the right ways of getting the line number, please.
    The input I am parsing is erroneous and I want to know the location of
    errors. Hence the above code.

    Thank You
    Himanshu.
    Himanshu Garg, Feb 2, 2004
    #1
    1. Advertising

  2. Himanshu Garg

    ko Guest

    Himanshu Garg wrote:
    > Hello,
    >
    > I am using HTML::parser and want to know the line number where a
    > particular event occurs.


    [snip code/warning message]

    > Could you suggest the right ways of getting the line number, please.
    > The input I am parsing is erroneous and I want to know the location of
    > errors. Hence the above code.
    >
    > Thank You
    > Himanshu.


    Start off by re-reading the documentation. The part you're looking for
    is in the 'Argspec' section, specifically the 'line' argspec
    identifier. Use something like this:

    #!/usr/bin/perl -w
    use strict;
    use HTML::parser;

    undef $/;
    my $html = <DATA>;
    my $p = HTML::parser->new( api_version => 3,
    start_h => [sub { print shift, "\n"; }, 'line'],
    );
    $p->report_tags('s');
    $p->parse($html);
    $p->eof;

    __DATA__
    <html>
    <body>
    <s>paragraph</s>
    <pre>

    </pre>
    <s>paragraph</s>
    <body>
    </html>

    There are a lot of good examples here:

    http://search.cpan.org/src/GAAS/HTML-Parser-3.34/eg/

    HTH - keith
    ko, Feb 2, 2004
    #2
    1. Advertising

  3. ko <> wrote in message news:<bvlg0k$bsh$>...
    > Himanshu Garg wrote:
    > > Hello,
    > >
    > > I am using HTML::parser and want to know the line number where a
    > > particular event occurs.

    >
    > [snip code/warning message]
    >
    > > Could you suggest the right ways of getting the line number, please.
    > > The input I am parsing is erroneous and I want to know the location of
    > > errors. Hence the above code.
    > >
    > > Thank You
    > > Himanshu.

    >
    > Start off by re-reading the documentation. The part you're looking for
    > is in the 'Argspec' section, specifically the 'line' argspec
    > identifier. Use something like this:
    >
    > #!/usr/bin/perl -w
    > use strict;
    > use HTML::parser;
    >
    > undef $/;
    > my $html = <DATA>;
    > my $p = HTML::parser->new( api_version => 3,
    > start_h => [sub { print shift, "\n"; }, 'line'],
    > );
    > $p->report_tags('s');
    > $p->parse($html);
    > $p->eof;
    >
    > __DATA__
    > <html>
    > <body>
    > <s>paragraph</s>
    > <pre>
    >
    > </pre>
    > <s>paragraph</s>
    > <body>
    > </html>
    >
    > There are a lot of good examples here:
    >
    > http://search.cpan.org/src/GAAS/HTML-Parser-3.34/eg/
    >
    > HTH - keith


    Thanks a lot.

    Thank You
    ++imanshu.
    Himanshu Garg, Feb 3, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ZOCOR

    XML Parser VS HTML Parser

    ZOCOR, Oct 3, 2004, in forum: Java
    Replies:
    11
    Views:
    813
    Paul King
    Oct 5, 2004
  2. Hugo
    Replies:
    10
    Views:
    1,307
    Matt Humphrey
    Oct 18, 2004
  3. WUV999U

    can I know how to write a html parser in C

    WUV999U, Feb 23, 2005, in forum: C Programming
    Replies:
    14
    Views:
    1,202
    Walter Roberson
    Feb 28, 2005
  4. Dan Jacobson
    Replies:
    0
    Views:
    84
    Dan Jacobson
    Jul 22, 2003
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    231
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page