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

In unix I created a symbolic link /data/rdmst11/abc.v -> /home/user/pqr/file_name123 Now to my perl script (test.pl) in directory /data/rdmst11/abc/mydir/ I provided ../../abc.v as the input. When i use cwd::abs_path("../../abc.v") it gives me /home/user/pqr/file_name123 as the return value. That means it has resolved the symbolic link. I want a way by which I get /data/rdmst11/abc.v as the output in which the symbolic link has not been resolved. I know i can easily write a small code snippet , but whether there exists some inbuilt perl API for this. Regards

Replies are listed 'Best First'.
Re: Symbolic Link
by targetsmart (Curate) on May 20, 2009 at 06:01 UTC
    Already you know from where you are running the perl script and what relative path you give as argument, you can easily make out the original path from that.
    IMO the resolved filename is the best that you can expect. because the original filename you can form very easily.

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
      IMO the resolved filename is the best that you can expect. because the original filename you can form very easily.
      If it's all very easy, there isn't much point of having the abs_path function in the first place, is it?

      I'd argue that it's much easier to follow a symbolic link, than to resolve a relative path into an absolute one.

Re: Symbolic Link
by doug (Pilgrim) on May 20, 2009 at 16:08 UTC

    I don't know how to get perl to do that, but I can with bash. How about a kludge like

    perl -e 'system "cd -P ./cs_envs && echo \$PWD";'

    which resolves the symbolic link, and this

    perl -e 'system "cd -L ./cs_envs && echo \$PWD";

    which doesn't.

    BTW: ./cs_envs is a symlink in my home directory. In the first one is shows the real (resolved) path, and the second one it shows the logical (unresolved) path.

    - doug
      sub utilGetAbsoluteFileName{ my $fileName = shift; $fileName =~ s/\/+/\//g; if(! -e $fileName){ return $fileName; } my $dir = getcwd; if ($fileName !~ /^\//){ $fileName = $dir."\/".$fileName; } my @targetArr = (); my @fileArray = split "\/", $fileName; for (my $index=0; $index<@fileArray; $index++){ next if($fileArray$index eq "\."); if($fileArray$index eq "\.\."){ pop @targetArr; }else{ push @targetArr, $fileArray$index; } } my $absFileName = join "/", @targetArr; $absFileName =~ s/\/+/\//g; return $absFileName; } I have written this API which calculates the absolute path for any filename in unix. It takes care of "../../" and "./". Please tell if i have missed any case. Thanks