Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to know the input line number when using parse_file of HTML::Parser

Reply
Thread Tools

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

 
 
Himanshu Garg
Guest
Posts: n/a
 
      02-02-2004
Hello,

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

# Create parser object
my $p = HTML:arser->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.
 
Reply With Quote
 
 
 
 
ko
Guest
Posts: n/a
 
      02-02-2004
Himanshu Garg wrote:
> Hello,
>
> I am using HTML:arser 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:arser;

undef $/;
my $html = <DATA>;
my $p = HTML:arser->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
 
Reply With Quote
 
 
 
 
Himanshu Garg
Guest
Posts: n/a
 
      02-03-2004
ko <(E-Mail Removed)> wrote in message news:<bvlg0k$bsh$(E-Mail Removed)>...
> Himanshu Garg wrote:
> > Hello,
> >
> > I am using HTML:arser 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:arser;
>
> undef $/;
> my $html = <DATA>;
> my $p = HTML:arser->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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Line in connector input not working? disabled? input radio to computer. 8ball meme Computer Support 7 11-18-2010 10:39 PM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM
I know, I know, I don't know Andries Perl Misc 3 04-23-2004 02:17 AM
why doesn't HTML::HeadParser include a parse_file function? Dan Jacobson Perl Misc 0 07-22-2003 02:44 AM



Advertisments