I'm trying to write a utility to parse some PHP (w/HTML) files using HTML::TokeParser::Simple (HTS). I need to keep track of any problems I find by line number so I've opted loop through the file and parse each line with HTS using the string constructor. (This is the only way to know where you are within the file.) One of the first things I tried to detect is when the file goes into PHP mode by looking for processing instructions (is_pi). When I do this, it only detects 2 of the 5 occcurances. But if I rewrite it to parse the file via HTS's file constructor it finds all 5! I then wrote a quick test using HTML::TokeParser which indicates that the 2 missing PI's are being detected as comments. How is it that HTS using a file constructor works properly but yet the HTS string constructor and TokeParser don't work? At least if it was consistent that would make sense, but it's not. Here's some code that illustrates this situation:
#!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; use HTML::TokeParser::Simple; $|++; #--- Variables my $file = 'test2.php'; my $line_number; print "#--- TokeParser::Simple String\n"; $line_number = 0; open (FH, "<$file") or die "Unable to open $file $!\n"; while (<FH>) { chomp; $line_number++; my $p = HTML::TokeParser::Simple->new(string => $_); while (my $token = $p->get_token) { if ($token->is_pi) { print $line_number . 'P: ' . $token->get_token0 . "\n"; } if ($token->is_comment) { print $line_number . 'C: ' . $token->as_is . "\n"; } } } close (FH); ############################## print "\n#--- TokeParser::Filehandle\n"; my $fh; open ($fh, "<$file") or die "Unable to open $file $!\n"; $line_number++; my $p = HTML::TokeParser::Simple->new(handle => $fh); while (my $token = $p->get_token) { if ($token->is_pi) { print $line_number . 'P: ' . $token->get_token0() . "\n"; } if ($token->is_comment) { print $line_number . 'Pc: ' . $token->as_is . "\n"; } } close ($fh); ############################## print "\n#--- TokeParser\n"; $line_number = 0; open (FH, "<$file") or die "Unable to open $file $!\n"; while (<FH>) { chomp; $line_number++; print "LINE: $line_number ********\n"; my $p = HTML::TokeParser->new(\$_); while (my $token = $p->get_token) { print Dumper($token) . "\n"; } } close (FH); __END__

Here is the code I'm running it against (test2.php):
<? /**** $Id$ ****/ ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ +/www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <BODY> <P>Some text<BR /> </P> <p><? echo U($date);?></p> <? echo 'This is from PHP.'; ?> <h1><? echo $h1; ?></h1> <?php ?> </body> </html>
And the output I get:
#--- TokeParser::Simple String 1C: <? /**** $Id$ ****/ 9P: echo U($date);? 10C: <? 13P: echo $h1; ? 14C: <?php #--- TokeParser::Filehandle 18P: /**** $Id$ ****/ ? 18P: echo U($date);? 18P: echo 'This is from PHP.'; ? 18P: echo $h1; ? 18P: php ? #--- TokeParser LINE: 1 ******** $VAR1 = [ 'C', '<? /**** $Id$ ****/' ]; --- SNIP! --- LINE: 10 ******** $VAR1 = [ 'C', '<?' ]; --- SNIP! --- LINE: 14 ******** $VAR1 = [ 'C', '<?php' ]; --- SNIP! --- This shows HTML::TokeParser sees those three lines as comments not as +PI's.

2005-02-24 Janitored by Arunbear - added readmore tags, as per Monastery guidelines


In reply to File parsing with HTML::TokeParser::Simple by THRAK

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.