|laziness, impatience, and hubris|
Trying to do multiple while loops on the same input fileby elef (Friar)
|on Jun 01, 2011 at 13:31 UTC||Need Help??|
elef has asked for the
wisdom of the Perl Monks concerning the following question:
Perl can't seem to do two while loops on the same read-only filehandle, as I painfully found out after spending upwards of three hours troubleshooting a long and fairly complex script.
The problem can be condensed to this:
This prints the contents of the file in loop one as expected, but then it prints an empty loop 2, 3 and 4. I have to close and reopen the filehandle in each loop to make it work:
As a point of interest, if I just reopen the fh in each loop without closing it (i.e. move the close out of the loop) then the program still works but the line numbers are not reset after each loop.
Is this behaviour intentional? Should I have known this to begin with? Does this behaviour serve any purpose or have any benefit? It certainly seems broken to me.
Even this simple code doesn't work as I would expect it to:
I'm on Win7 with perl 5.10.0, by the way. (I'm using an old version because ppm is broken in new versions.) I also tested this on 5.10.1 on Ubuntu and got the same result.