i'm trying to write a recursive function to step through a series of directories and subdirectories, but i'm having trouble with it. now, i know everyone's going to say i should use File::Find, but i'm doing this for a commissioned job, and the employer doesnt want to use ANY modules (something about minimalizing the program ::shrug::).

anyway, the function, called traversePath, is passed a root pathname, and it's supposed to call another instance of itself for each subdirectory and pass it $npath, but when it calls the new instance, it seems to use the same $path as the instance that called it, which basically puts it into an infinite loop, rechecking the root dir over and over again...

heres the sub:

sub traversePath($path) { ##DEBUG## print "\nDEBUG:traversing path '$path'\n"; ##DEBUG## # will contain combined filesize of all files in pwd $totalSize = 0; # convert any backslashes to fwdslashes $path =~ s/\\/\//g; # open up the current dir opendir(PATH,$path); # check for trailing slash and remove if found if(substr($path,-1,1) eq "/") {chop($path)} #read files & subdirs into @subdirs @subdirs = readdir(PATH); closedir(PATH); # loop through them all foreach $subdir (@subdirs) { # construct full pathname to current subdir or file $npath = $path."/".$subdir; # not a dir, just a file, perform a function # which returns the byte size of said file if(! -d $npath) { $totalSize += writeFile($npath); } # is a subdir else { # dont mess with cwd or parent dir if($npath ne "." && $npath ne "..") { ##DEBUG## print "\nDEBUG:npath='$npath'\n"; ##DEBUG## # call new instance of self and pass as param # to other function which records total bytesize # of directory writeDir($npath, traversePath($npath) ); } } } # return bytesize of any files found in current dir return $totalSize; }
and heres the output when initially passed the path "f:/" (it falls into an infinite loop and repeats this endlessly):
DEBUG:traversing path 'f:' DEBUG:npath='f:/winnt'
does anyone see anything i'm not seeing? any help i would greatly appreciate.

In reply to recursive path function falls into infinite loop by Anonymous Monk

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.