jacques has asked for the wisdom of the Perl Monks concerning the following question:

I am seeing an error with my use of File::Spec->rel2abs(). I am using File::Find and putting $File::Find::name into rel2abs(). It should print:
C:\Documents and Settings\Jacques\Desktop\Perl\Module.pm

Instead I am seeing the following:

$File::Find::name prints:
./Desktop/Perl/Module.pm

File::Spec->rel2abs($File::Find::name) prints:
C:\Documents and Settings\Jacques\Desktop\Perl\Desktop\Perl\Module.pm

Here's another example:

$File::Find::name prints:
./Perl/Module.pm

File::Spec->rel2abs($File::Find::name) prints:
C:\Documents and Settings\Jacques\Desktop\Perl\Perl\Module.pm

There is no code between the printing of $File::Find::name and the printing of File::Spec->rel2abs($File::Find::name). My platform is Windows (obviously) and I am using File::Spec version 0.87.

Replies are listed 'Best First'.
•Re: File::Spec error with rel2abs
by merlyn (Sage) on Jan 07, 2005 at 03:53 UTC
    If you're using $File::Find::name within the "wanted" routine, be aware that you also have a new working directory temporarily. Either use just $_ with rel2abs, or wait until you're done and use the saved results from $File::Find::name with your now restored original working directory. Your choice. Don't mix the two. {grin}

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

      You are so awesome. Thanks.
Re: File::Spec error with rel2abs
by brian_d_foy (Abbot) on Jan 07, 2005 at 06:14 UTC

    If you don't give rel2abs a third parameter, it uses the current working directory. It doesn't try to be smart or actually figure out if the answer makes sense. It just smushes two paths together.

    In your wanted() function, you can just use $_, which is only the filename portion, instead of $File::Find::name, or you can wait until you get the list of files and convert them later and specify the base directory.

    Alternately, if you give File::Find::find() an absolute path to start with, you get an absolute path in $File::Find::name, and you don't need to convert every path you find.

    --
    brian d foy <bdfoy@cpan.org>
Re: File::Spec error with rel2abs
by jplindstrom (Monsignor) on Jan 07, 2005 at 14:00 UTC
    What Merlyn said.

    Note that you also can tell File::Find to not chdir into the directories with the no_chdir option. This is more like you expected it to behave in the first place.

    /J