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

I need a function which get as an input relative path and return its absolute path name, lets say ../../etc is the input, the function will return /local/disk1/etc. Any idea ?
  • Comment on convert a given dir name to its absolute path

Replies are listed 'Best First'.
Re: convert a given dir name to its absolute path
by tirwhan (Abbot) on Dec 22, 2005 at 11:42 UTC

    Take a look at File::Spec, the rel2abs method:

    $abs_path = File::Spec->rel2abs( "../../etc" ) ;

    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
      the $abs_path is empty, can you check - $abs_path = File::Spec->rel2abs( "../../"); print $abs_path,"\n"; I got this empty (??) Could you please advice ? Thanks azaria

        There are bugs in File::Spec that have to do with relative and absolute paths. Some are recently fixed, some fixes are still in developer releases. This may be related. You might want to see if upgrading File::Spec helps. See the change log for details.

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        Executed in /usr/local/bin:

        $abs_path = File::Spec->rel2abs( "../../"); print "$abs_path\n __OUTPUT__ /usr/local/bin/../..

        So it works correctly on my platform (Linux with perl 5.8.4).


        Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: convert a given dir name to its absolute path
by rinceWind (Monsignor) on Dec 22, 2005 at 12:12 UTC
    use Cwd qw/abs_path/;

    Note that whilst File::Spec->rel2abs may give you the right answer sometimes, abs_path will give you the canonical path. rel2abs works by platform specific string manipulation, but abs_path asks the operating system for the path, on some platforms by doing a chdir followed by a pwd.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

      To clarify slightly, as this is subtle:

      • File::Spec->rel2abs tells you what the absolute path should be, whether the file/dir exists or not

      • abs_path tells you what the filesystem thinks the absolute path is, and the file/dir must exist for it to do so

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      The one (obvious) caveat for that is that Cwd::abs_path will fail if you don't have permissions to chdir into the directory in question. Not normally an issue, since there isn't very much you can do with such a directory anyway.


      Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
      I checked the following code on Linux but the output was unfortunatly empty ?!! -------------------------------- use Cwd qw/abs_path/; $abs_path = File::Spec->rel2abs( "../../"); print $abs_path,"\n";
        opps I mixed two method. Ignore my mail. I will check this Cwd