Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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:
open (INPUTFILE, "<:encoding(UTF-8)", "in.txt") or die "Can't open fil +e: $!"; for (my $i = 1; $i < 5; $i++) { print "\n-----------------------LOOP $i-----------------------\n"; while ($line = <INPUTFILE>) { print "line $.: $line\n"; } } close INPUTFILE;

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:
for (my $i = 1; $i < 5; $i++) { open (INPUTFILE, "<:encoding(UTF-8)", "in.txt") or die "Can't open + file: $!"; print "\n-----------------------LOOP $i-----------------------\n"; while ($line = <INPUTFILE>) { print "line $.: $line\n"; } close INPUTFILE; }

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:
print "First loop:\n\n"; while (<DATA>) { print; } print "\n\nSecond loop:\n\n"; while (<DATA>) { print; } __DATA__ first line second line third line


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.

In reply to Trying to do multiple while loops on the same input file by elef

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 13:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found