in reply to Trying to read a filehandle inside a loop

The diagnostics error is due to the fact that the reading of a file could return 0 which would evaluate to false in a boolean context, whereas you want to be testing for definedness. The reason you can't read from <DATA> within the loop is that you've already slurped it in the foreach. So in this case a while loop would make a lot more sense e.g
use strict; use diagnostics; my $line = (<DATA>); print "line ouside loop = $line"; while(my $line = <DATA>) { print "line from foreach = $line"; $line = (<DATA>) or print "error reading filehandle\n"; $line ||= "null\n"; print "line inside loop = $line"; } __DATA__ 1 2 3 4 5

HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Trying to read a filehandle inside a loop
by roik (Scribe) on Oct 23, 2002 at 09:31 UTC
    Thanks for your explanation. I can see now that the foreach has 'slurped' the data from the filehandle, so there is no data to read inside the loop. Why doesn't the 'while' loop slurp the data too? I'll have a look in perlsyn I think! Cheers.
      Why doesn't the 'while' loop slurp the data too?
      Because the filehandle is only evaulated in a scalar context, so you'll only get one line per iteration. Whereas a foreach loop evaluates the filehandle in a list context which produces the list which it iterates over. Another thing to be aware of is that if you're iterating over a filehandle in a while loop perl will auto-magically stick a defined test around your condition e.g
      while(my $line = <DATA>) # becomes while (defined(my $line = <DATA>))
      So now if a single 0 is read the loop will continue.
      HTH

      _________
      broquaint