George_Sherston has asked for the wisdom of the Perl Monks concerning the following question:
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.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; }
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.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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: file globbing in a nested while loop
by pjf (Curate) on Oct 05, 2001 at 16:44 UTC | |
by George_Sherston (Vicar) on Oct 05, 2001 at 17:02 UTC | |
by busunsl (Vicar) on Oct 05, 2001 at 17:08 UTC | |
by tommyw (Hermit) on Oct 05, 2001 at 17:53 UTC | |
|
Re: file globbing in a nested while loop
by clintp (Curate) on Oct 05, 2001 at 17:15 UTC | |
|
Re: file globbing in a nested while loop
by busunsl (Vicar) on Oct 05, 2001 at 16:32 UTC | |
|
Re: file globbing in a nested while loop
by trantor (Chaplain) on Oct 05, 2001 at 17:10 UTC | |
|
Re: file globbing in a nested while loop
by Aristotle (Chancellor) on Oct 05, 2001 at 18:18 UTC |