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

dumb question but why does
open(F,"/etc/passwd"); while(<F>) { print } while(<F>) { print }
only print out the contents of <F> once?

Replies are listed 'Best First'.
Re: multiple loop problem
by eduardo (Curate) on Apr 28, 2001 at 20:21 UTC
    Because once the first while loop has run it's course, F is "empty." Think of it like a book, if I say to you, "flip through all the pages in the book," and you finish it, so it's on its back cover, and I say: "Flip through all the pages in the book!" You can't without fliping it over and starting fresh. A:
    close(F); open(F, "/etc/passwd");
    between the whiles, is the equivalent of flipping the book over so you can flip through the pages again.
    seek(F, 0, 0);
    does something similar, only it doesn't completely close and reopen the book, think of this as just flipping the pages back to the beginning. I hope it makes sense :)