Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

As usual

by pwhysall (Acolyte)
on Mar 01, 2001 at 19:15 UTC ( [id://61561]=note: print w/replies, xml ) Need Help??


in reply to Re: Recursive directory scanning
in thread Recursive directory scanning

The monks come up with the goods. Deepest gratitude to you all.

Actually, I never though of this as a file-finding problem. I've obviously got my Win32 head on today, or else find(1) would have leapt immediately to mind.

Out of sheer curiosity, though, I'd be grateful if someone could tell me what's wrong with my recursive function.

Replies are listed 'Best First'.
Re: As usual
by merlyn (Sage) on Mar 01, 2001 at 19:26 UTC
    I don't see any subdirectory building anywhere. When you ask for the directories under "/home", you're getting back "merlyn" and "vroom", not "/home/merlyn" and "/home/vroom". So you then recurse on just "merlyn", and the opendir fails.

    To do that portably, you need to look at File::Spec, since Unix wants forward slash, Windows can handle forward slash but most people expect backslash, Macs want colon, and VMS wants some mixed up mess.

    But then you also have to figure out how to skip over symlinks, since that can send you into an infinite loop by turning a DAG into a general node map, bringing you back to where you started too fast.

    You also didn't localize your directory handle. In the way you used it, it wouldn't have hurt you, but it it could have potentially stomped on any other use of the same name.

    So this is why "recursive directory handling" is nearly always responded with a groan, a sigh, and "please use File::Find". It's an easy task to consider, but a difficult task to actually do right.

    -- Randal L. Schwartz, Perl hacker

      Actually, VMS just wants . separated directory names inside square brackets, with a prepended . if you want to explicitly state that it's relative to your current location, and appended ellipsis ... if you mean "this directory and everything below it". [.home.peter.stuff...]*.*;*, for example, would be roughly analogous to ./home/peter/stuff/*.*, but there's no comparable operator in UNIX for the ... bit.

      Simple, see?

      (Yes, I am a VMS admin:-)

      Thanks for the other stuff, but I really don't know what a DAG or a node map is - however, I avoid that in this case by running on Win32, which doesn't have those pesky symlinks.

      But I will definitely be using File::Find.

Re: As usual
by chipmunk (Parson) on Mar 01, 2001 at 19:43 UTC
    I suspect the reason for the deep recursion is that you don't filter out '.' and '..'. When you recurse on the contents of a directory, you recurse on the contents of its "subdirectory" '.' (and its "subdirectory" '.' ...).

    As merlyn also pointed out, since you're not using chdir, you need to specify the full path to each file in the file test and the opendir().

    You already have the solution, which is File::Find. :)

Re: As usual
by Tyke (Pilgrim) on Mar 01, 2001 at 19:49 UTC
    You're looping on the first item in the list which will be a '.'

    Works better if you add next if $item eq '.' or $item eq '..'; after the foreach

    readdir gives you the names in the directory... not path names, so when you call the printdir recursively you'll need to prefix the current directory to each element of the list. (Naturally you'll need to change your '.' test accordingly.

    Anyway this seems to work on my machine.

    #!perl -w
    use strict;
    use Carp;
    
    printdir(@ARGV);
    
    sub printdir {
        my $item;
        foreach $item(@_) {
          next if $item =~ /\.{1,2}$/;
          if (-d $item) {
            print "$item\n";
            opendir(SUBDIR, $item) or croak "Can't open directory :$!";
            my @subdir_items = readdir(SUBDIR);      #+
            closedir(SUBDIR);                        #+  
            printdir(map {"$item/$_"}@subdir_items); #+
          }
        }
      }
    

    Oh, by the way the equivalent in my previous post should have been

    perl -Mstrict -MFile::Find -wle 'find sub{print $File::Find::dir if -d},@ARGV' .
    
    but you saw that already, didn't you :)

    Update: Just saw merlyn's post. The above ran on Win32 because (as he says) the Windows port recognizes forward slashes as valid directory separators. I don't know whether this is an issue on other non unix ports. Still, I agree with him, it's a lot safer here to stay with the standard.

    Update 2: The '.' test is broken: it will match any file ending with a '.'. You'd need to split off the file name from the path, so you might as well use File::Spec... Oh good grief, why did I ever post this code? This'll teach me to shut up :(

      This regex, I think, will match . or .. and nothing else:

      /^\.{1,2}$/

        Good thing you're just "thinking". It also matches ".\n" and "..\n", thanks to $'s little-understood feature of matching before the optional newline at the end of the string.

        Perhaps you want

        /\A\.{1,2}\z/

        -- Randal L. Schwartz, Perl hacker

        This is true, and that would break the script :(

        Remember that $item has been prefixed by the directory, so we want to filter out elements like foo/bar/. and fi/fo/fum/..

        Better would be to extract the file name and compare it to current/parent directory in a portable fashion. this will work on *nix or windows. I have no idea what it might do on a MAC or on VMS.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://61561]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-20 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found