Path: utzoo!utgpu!news-server.csri.toronto.edu!me!eastick Newsgroups: comp.lang.perl From: eastick@me.utoronto.ca (Doug Eastick) Subject: Recursively going into subdirectories problem Message-ID: <1990Mar29.185756.11553@me.toronto.edu> Copyright: none Organisation: U of Toronto, Dept. of Mechanical Engineering Distribution: comp Date: 29 Mar 90 23:57:56 GMT I'm writing a program that goes down in a directory tree, reading files while going down. Pseudo-perl below with commentary after. --------- #!/usr/bin/perl @directories = ( "barf", "foo" ); @dirstack = (); $topdir = "/u3/eastick/blork/"; for $dir (@directories) { print "doing $dir\n"; do dodir($dir); # do stuff with the results } # # Sum and count the hierarchy, calling recursively if neccessary # sub dodir { local($dir) = @_; local($file, $xyz, $burp, $snuff, $gorp); local($curdir) = ""; local($d); push(@dirstack, $dir); for $d (@dirstack) { $curdir .= "/" . $d; } print "dodir: $curdir\n"; chdir($topdir.$curdir) || warn "Couldn't cd to $curdir"; for $file (<*>) { print "dodir: for'ing $file\n"; if (-d $file) { # THIS FAILS THE 2ND TIME do dodir($file); # recursive print "dodir: back from recursive dodir of $file\n"; next; } if (-z $file) { next; } # # do stuff here with the readable file # } pop(@dirstack); } -------------- Say, inside $topdir is 3 directories: a, b and c. All 3 contain text files. Problem: stuff in `a' is processed properly, but once it returns from the recursive dodir(), `a' is processed again (as $file) and even fails the -d test. Why? Also, why is the second-last line (pop(@dirstack)) executed before the body of the for loop is done? ...Stupid perl questions -- Doug Eastick -- eastick@me.utoronto.ca