in reply to Re: Trying to read a filehandle inside a loop
in thread Trying to read a filehandle inside a loop

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.
  • Comment on Re: Re: Trying to read a filehandle inside a loop

Replies are listed 'Best First'.
Re: Re: Re: Trying to read a filehandle inside a loop
by broquaint (Abbot) on Oct 23, 2002 at 09:42 UTC
    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