Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Re: Recursive opendir without

by bobione (Pilgrim)
on Apr 11, 2001 at 18:29 UTC ( #71678=note: print w/replies, xml ) Need Help??

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

That is exactly what I wanted. I need to declare a my $dh.
But I steel don't understand why my opendir ($dir, $dir) doesn't work :(
Thank you very much.

Replies are listed 'Best First'.
(tye)Re: Recursive opendir without
by tye (Sage) on Apr 11, 2001 at 19:45 UTC

    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")
      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 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...

        $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.

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

Re:{3} Recursive opendir without
by jeroenes (Priest) on Apr 11, 2001 at 19:58 UTC
    Alternatively, have a look at Re: How can I create an array of filenames?. That's truly recursive, and simple.

    "We are not alone"(FZ)

    With a little change:

    our @list; findfile('*'); sub findfile{ my $glob = shift; for ( glob $glob ){ push( @list, $_) if -f; findfile( $_.'/*' ) if -d; } }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2019-11-15 02:38 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (80 votes). Check out past polls.