Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Recursion problems...

by Ranna (Acolyte)
on Mar 03, 2002 at 23:23 UTC ( #149033=perlquestion: print w/replies, xml ) Need Help??

Ranna has asked for the wisdom of the Perl Monks concerning the following question:

I'm having a bit of trouble with one of my scripts that lists a directory recursively for my webpage. What I want to do is be able to limit the recursion depth, but so far, I've only gotten it to limit directories that it sees fit to. It's a very picky script apparently.

Recursion depth of 0 works as it should, recursing infinitely (or, at least as far as it can). Depth of 1 works, too, working like `ls` does. However, after that..well.. it decides what depth it likes. Here's the code, and here's an example. Sorry for not using File::Find, I know I got criticized for that before. Any help welcomed :o)

Replies are listed 'Best First'.
Re: Recursion problems...
by steves (Curate) on Mar 04, 2002 at 02:31 UTC

    You need to re-think your depth computation. You're using $c for that, but the way I see it that can't be right. You're incrementing $c for each subdirectory within a directory -- not for each level. And you're also setting it back to 1 when you find a file within the directory that's not a subdirectory.

    With recursion, the typical model is to increment a depth variable on entry to the function -- not before a recursive call. That way the stack pops it back to its proper value as each recursive call returns.

    I'm also not 100% comfortable with a global PARSEDIR. The way you're using it -- getting the entire file list sorted -- you should be okay. But typically, on recursion if you open the same global it will have that global setting on return; i.e., you may return from a recursive call to find PARSEDIR referring to the directory you just processed. I always local these to avoid that (since you can't my a typeglob).

Re: Recursion problems...
by simon.proctor (Vicar) on Mar 03, 2002 at 23:54 UTC
    Just some quick ideas::
    1. You don't have warnings enabled
    2. Implement this in File::Find with a recursion counter


    From what I can tell, you are probably running out of filehandles as you are recursing on a dir but not closing the handle above. You might want to consider closing the handle before the recursion call and then reopening it.

    An untested idea:
    foreach my $file (sort(readdir(PARSEDIR))) { ..... becomes: my @list = sort(readdir(PARSEDIR)); closedir(PARSEDIR); foreach (@list) { .....
    That way the handle is closed immediately.

    Unfortunately I can't run your code on my puter as I don't have any of your support modules. So I can't be of more help.

    Simon
Re: Recursion problems...
by ignatz (Vicar) on Mar 03, 2002 at 23:51 UTC
    > Sorry for not using File::Find, I know I got criticized
    > for that before.

    Why be sorry? Why not take the advice? It's good advice, you know. (UPDATE: Suddenly noticed that irony of the Paco link in my sig.)

    ()-()                                                      ()-()
     \"/    DON'T   BLAME   ME,   I  VOTED   FOR    PACO!       \"/
      `                                                          ` 
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://149033]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2021-04-18 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?