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:
use strict; use CGI qw/:standard/; my $ext = 'pl'; my $Indent = 15; my $q = new CGI; print $q->header; print $q->start_html; &GetDirs; print $q->end_html; sub GetDirs { my ($Dir, $Posn) = @_; chdir $Dir if $Dir; while (<*/>) { print "<div style=\"margin-left:$Posn\">$_</div>"; &GetDirs($_, $Posn + $Indent); } while (<*.$ext>) { print "<div style=\"margin-left:$Posn\"><a href=\"editor.pl?Ac +tion=GetScript&File=$_\" target=\"$_\">$_</a><BR>"; } chdir "../" if $Dir; }
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:
sub GetDirs { my ($Dir, $Posn) = @_; my $OldDir = getcwd; chdir $Dir; while (<*/>) { print "<div style=\"margin-left:$Posn\">$_</div>"; &GetDirs(getcwd, $Posn + $Indent); } while (<*.$ext>) { print "<div style=\"margin-left:$Posn\"><a href=\"editor.pl?Ac +tion=GetScript&File=$_\" target=\"$_\">$_</a><BR>"; } chdir $OldDir; }
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

In reply to file globbing in a nested while loop by George_Sherston

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.