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

hi i write some code to get the directory of my computer - but that not working my code:
sub Again { $dirtoget= $_[0]; if(opendir(DH, $dirtoget)) { @thefiles = readdir(DH); closedir(DH); if(@thefiles) { foreach $f (@thefiles) { next if ($f eq "." or $f eq ".."); print "$f\n"; if(-d "$dirtoget/$f") { Again("$_[0]/$f"); } } } } else { return; } } $dirtoget = "c:/Exam"; Again($dirtoget);
thanks for help...

Replies are listed 'Best First'.
Re: recursing problem
by BrowserUk (Patriarch) on Apr 20, 2012 at 11:45 UTC

    The first thing that I noticed is that you are using bareword dirhandles opendir(DH, $dirtoget) in a recursive subroutine. That is never going to work.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      He's also not using lexical vars. It's never, ever gonna work!

      Dave.

        I wonder if that constitutes an ironic response to my sardonic reply; or a sardonic response to my ironic reply? :)


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

      The OP should read Higher Order Perl -- its first chapter is about recursion, with a specific example on directory walking and it explains why those silly lexical things are needed. And it's even available for free online.
Re: recursing problem
by JavaFan (Canon) on Apr 20, 2012 at 10:54 UTC
    Define "not working". Doesn't it compile? Does it generate a run-time error? Does it do something else than you expect? Does it sit on the couch, eating potato chips?

    You got to be far more specific. Gods do not exist, so there's noone here who knows what you mean.

Re: recursing problem
by Anonymous Monk on Apr 20, 2012 at 10:45 UTC
    -d "$dirtoget/$f" is never true. The rest of the program works.

      That's interesting, because on my machine, not only does the expression result to true when it should, but the code fails to recurse into anything further than a second level deep directory structure whether I change the -d line or not. (This before and after applying warnings and strict.

Re: recursing problem
by Marshall (Canon) on Apr 22, 2012 at 07:27 UTC
    I'm not sure what you want, but I would suggest looking at File::Find. There are variants depending upon what you want.

    The code below descends from the $start_dir and prints the full path names of all files.

    Recursive directory descent is a well known problem and I would use one of the well known solutions unless this is some kind of a homework problem.

    #!/usr/bin/perl -w use strict; use File::Find; #a core module my $start_dir = "C:/Exam"; find (\&wanted, ($start_dir)); sub wanted { print "$File::Find::name\n" if -f; }