|Welcome to the Monastery
file globbing in a nested while loopby George_Sherston (Vicar)
|on Oct 05, 2001 at 16:29 UTC
George_Sherston has asked for the wisdom of the Perl Monks concerning the following question:
My brain, such as it is, hurts. What I want is something quite simple (and you'll probably tell me there's a module for it, which I will accept with humility): a file tree showing all the .pl files in a directory and its sub directories.
Now, I'd be very interested in suggestions how to do this differently, but what I'm really stuck on is the particular reason why what I've tried so far fails. Here's my first attempt:
What I expected was that it would go through all the directories, open each one, then loop back on itself and go through that directory, keep on doing that until it had run out of directories. Each time it ran out of directories it would exit the loop, print out the files themselves, and then drop down one level. Obviously it's a nested loop, and that way madness lies, but it's never going to be an infinite recursion, because my file tree is finite.
That was the plan. But actually what happens is, it loops round and round printing out the directories in my root directory over and over again forever. At first I thought the problem was that the working directory got reset each time I re-called &GetDirs; but I used Cwd (thanks to the nice people in the CB) to get an absolute directory reference which I passed, and I still had roughly the same problem:
So now what I THINK is happening is that the while loop is not restarting when I call &GetDirs from within &GetDirs. This seems odd - but maybe it does this for a good reason. Or maybe it's something else again.
I would be very grateful for any guidance - please don't hesitate to point out what may seem obvious, as it certainly isn't obvious to me!
§ George Sherston