martin82 has asked for the wisdom of the Perl Monks concerning the following question:

When I read an HTML file from my server using standard input:
while (<'FILE'>) { $gline = <'FILE'>; print $gline; }
I often get apparently empty lines in $gline, although the character counts are +ve. I'm guessing that the incoming text runs through the Perl Parser and gets mangled.

Is there another way?

Edit Masem 2001-09-04 - CODE and slight formatting fix

Replies are listed 'Best First'.
Re: Reading HTML?
by tachyon (Chancellor) on Sep 05, 2001 at 02:08 UTC

    Your syntax is wrong. Each time you say <FILE> you get another line - you do this twice for each while loop but only assign and print one line. Thus you skip every second line. You don't need the ' quotes either. Here is a script that does it two different ways. $. is the current line in the file. $_ is a magical Perl var that contains the line in the first chunk of example code. First you see the code, then its output.

    C:\>type test.pl print "\nRead files off command line\n"; while(<>) { print "$. $_"; } print "\n\nRead file in usual way\n"; $file = 'c:/test.pl'; open F, "<$file" or die "Oops Perl says $!"; while (my $line = <F>) { print "$. $line"; } close F; print "\n\n\nPerl Rocks!\n"; C:\>perl test.pl test.pl Read files off command line 1 print "\nRead files off command line\n"; 2 while(<>) { 3 print "$. $_"; 4 } 5 6 print "\n\nRead file in usual way\n"; 7 $file = 'c:/test.pl'; 8 open F, "<$file" or die "Oops Perl says $!"; 9 while (my $line = <F>) { 10 print "$. $line"; 11 } 12 close F; 13 14 print "\n\n\nPerl Rocks!\n"; Read file in usual way 1 print "\nRead files off command line\n"; 2 while(<>) { 3 print "$. $_"; 4 } 5 6 print "\n\nRead file in usual way\n"; 7 $file = 'c:/test.pl'; 8 open F, "<$file" or die "Oops Perl says $!"; 9 while (my $line = <F>) { 10 print "$. $line"; 11 } 12 close F; 13 14 print "\n\n\nPerl Rocks!\n"; Perl Rocks! C:\>

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Reading HTML?
by monkfish (Pilgrim) on Sep 05, 2001 at 06:03 UTC
    Your code should look like this:

    while (<'FILE'>) { $gline = $_; print $gline; }

    This should fix your problem. The perl parser will not care what the content of the lines you are reading in, and shold never mangle them.

    -monkfish (The fishy monk)

Re: Reading HTML?
by martin82 (Initiate) on Sep 06, 2001 at 00:35 UTC
    Thanks guys. Both were problems. If you'd like to see the result, visit http://www.ribbonrail.com/nmra/Search.html and run a search using the code you helped me with.