in reply to using file::find::name and file tests

$File::Find::name is a full path relative to the directory where you started the scan. During the scan, that'll be the wrong name. You want to make your tests against $_ instead, which is just the local name relative to the current place of the scan.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

  • Comment on Re: using file::find::name and file tests

Replies are listed 'Best First'.
Re^2: using file::find::name and file tests
by davidrw (Prior) on May 06, 2006 at 17:35 UTC
    the File::Find docs (and a quick warn tossed into OP's first code) say that $File::Find::name is the 'Complete pathname to the file', and not relative and all the examples given in the The wanted function section indicate the same thing..

    to OP-- you can add some debugging to print out cwd, $_, and $File::Find::name to examine exactly what's going on..
      the File::Find docs (and a quick warn tossed into OP's first code) say that $File::Find::name is the 'Complete pathname to the file', and not relative and all the examples given in the The wanted function section indicate the same thing..
      Let's recap... yes it is the complete path, but constructed from the original parameter. If you start with ".", your path will look something like "./path/to/file" which, as merlyn said, won't work as the cwd is changed to the directory the current file is in, every time.

      So, if you want an absolute path, start out with absolute paths. rel2abs from File::Spec::Functions can do wonders. You don't have to use in it the callback, just apply it to every parameter you pass to find.

      use File::Spec::Functions 'rel2abs'; find sub { blah blah } map rel2abs($_), @dirs;
      Now you'll get absolute paths.
        I get it now.. I tried it before running OP's code with '/tmp' as the param .. running w/'.' shows the issue..

        Is it just me or is that not clear in the docs? Maybe i was just getting thrown by the absolute paths used in the example chart...


        Anyways, i second the note from my other reply about File::Find::Rule :) (or merlyn's File::Finder; both obsolete OP's issue)
      what is an OP?

        "Original poster", in this case you.

        --
        David Serrano

        or, "Original Post".