in reply to opendir in file find

Basically you want to ask the question "is this file a directory, and is this file not readable to me?" You can accomplish this with two file test operators...

"doh! can't read directory $file!\n" if (-d $file and not -r $file);
Update: Slight correction, I think... I believe archon and I were both only half right. You'll want to use both the -r and -x file test operators, at least, based upon what your desire to "search" the directory, this is probably what you want.

Replies are listed 'Best First'.
Re: Re: opendir in file find
by BrowserUk (Patriarch) on Jul 25, 2003 at 17:14 UTC

    You can save a little overhead by reusing the stat buffer using the magical filehandle '_',

    "doh! can't read directory $file!\n" if (-d $file and not -r _);

    In the case of File::Find, they guarentee that lstat will have been called before the wanted sub is called, so rather than

    sub wanted{ print $File::Find::name if -d $_ and $r $_; }

    you can use

    sub wanted{ print $File::Find::name if -d _ and -r _; }

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

      You can save a little overhead by reusing the stat buffer using the magical filehandle '_',

      Yes and no. You can do that if you first disable File::Find's overeager "optimization" via:

      $File::Find::dont_use_nlink= 1;
      If you don't do that first, then File::Find will skip doing lstat in some cases and your use of _ will give you stale data (data for the wrong file) in those cases.

      I'd much rather you have to request this optimization when you want it (which, on some file systems, can speed up Find operations where you don't care about anything but the file name), but p5p seems pretty violently against making this "optimization" opt-in instead of opt-out.

                      - tye

        Thanks for the heads-up on this. I don't make a lot of use of the module, but that is worth noting.

        Where does that sit with this statement in the 5.8 pod though?

        It is guaranteed that an lstat has been called before the user's wanted() function is called. This enables fast file checks involving _.

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

      Way cool. I have never before come across said magical file handle in my travels. I shall have to remember that bit of Perl arcana, though admittedly we might be willfully trading code comprensibility for performance. Of course, in this scenario it may result in our statting each file one third as many times as we may otherwise have, so if we're blasting through a directory structure of thousands of files it will be well worth it.