in reply to Re: Re: Recursive opendir without
in thread Recursive opendir without

I assume that you mean "why my opendir($dh,$dir) doesn't work". That is because when you opendir($dh,"/") and then readdir($dh), you'll get a file name like "usr" and then try to do opendir($dh,"usr") which isn't the same as opendir($dh,"/usr"). If your current working directory happens to be "/", then that call will work (by luck) and you'll read a file name like "home" and try opendir($dh,"home") when you should be doing opendir($dh,"/usr/home").

So you either need to chdir into each directory before you open the next one (and then chdir back out when you are done) or you need to keep track of the full path to each directory as you go so you can prepend it in several places.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: Recursive opendir without
by bobione (Pilgrim) on Apr 11, 2001 at 21:52 UTC
    No, it's not a problem of "/" (even if my first exemple could be wrong).
    I mean : if I take ton's code and replace $dh by $dir :
    use strict; # ---> IMPORTANT my $dir = "toto"; my $file; sub openNewDir { my $dir = shift; ### my $dh; opendir ($dir, $dir); # ---> MODIFICATION while ($file = readdir ($dir)) { # ---> MODIFICATION here too next if (($file eq '.') || ($file eq '..')); if (-d "$dir/$file") { openNewDir("$dir/$file"); } else { # Do something with the file print "($file)\n"; } } close ($dir); # ---> MODIFICATION here too } openNewDir ($dir);
    (I comment important lines).
    use strict warm me : Can't use string ("toto") as a symbol ref while "strict refs" in use at test.pl line 13. (that mean during the readdir()).
    And when I was trying to explain you, I think I understood why ;) (but not sure)
    It could be that readdir() don't know if $dir is a "Directory Handler" or the "Directory" itself...

    BoBiOne
      $dir is a string (aka "toto") and you are trying to use it as a directory handle. How do you expect that to work?

      Let me give you a hint.

      Create a file called "tst" and run the following snippet without strict on.

      my $file = "tst"; open ($file, $file) or die "Cannot read $file: $!"; print <tst>;
      How did a filehandle get opened on the filehandle tst?

      Now read what strict 'refs' is supposed to disallow.

      UPDATE
      Not my link. tye pointed his tutorial out to me and I made the change...

        Great thanks for your link strict. !
        BoBiOne