in reply to How to get a true canonical path

Try Cwd::abs_path.

Replies are listed 'Best First'.
Re^2: How to get a true canonical path
by hsinclai (Deacon) on Jun 09, 2004 at 14:56 UTC
    Yeh - that figures out links pretty well..

    use strict; use Cwd 'abs_path'; my $fileinquestion = "/var/mail/"; my $realpathfor = abs_path($fileinquestion); print "$realpathfor\n";


    If you have to be out inside a system operation, the value of
    pwd -P
    is pretty real

    Out of curiosity I tried a .. operation under win2k with AS.. it seems to work..
    #!perl use strict; use Cwd; use Cwd 'abs_path'; my $q = 'C:\\TEMP\\'; chdir $q; my $dirnow = getcwd(); print "Changed dir to C:\\TEMP\n"; print "Cwd thinks I am in $dirnow\n"; chdir "..\\"; print "Changed dir up one level ..\n"; my $newdirnow = getcwd(); print "Cwd now thinks I am in $newdirnow\n";

    so that might do the right thing if a user provides input including \path\..\another\path, so long as backslashes get treated correctly
Re^2: How to get a true canonical path
by rinceWind (Monsignor) on Jun 09, 2004 at 16:30 UTC
    Thanks - Look like it will do the trick. I need to check VMS though.

    One thing I have noticed is that the directory must exist - and must actually be a directory. Feeding a file to Cwd::abs_path causes it to carp with "Not a directory".

    --
    I'm Not Just Another Perl Hacker

      Not on my system (perl5.8.0):
      $ perl -MCwd=abs_path -e 'warn abs_path("$ENV{HOME}/../slavenr/.cshrc" +)' /home/slavenr/.cshrc at -e line 1.