Home: Perl Programming Help: Advanced:
absoulte path for symbolic link



rahulgupta70
New User

May 19, 2009, 9:52 PM


Views: 6868
absoulte path for symbolic link

In unix in creatd 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

Regards


1arryb
User

May 20, 2009, 1:55 PM


Views: 6851
Re: [rahulgupta70] absoulte path for symbolic link

Hi rahu,

Get the abs_path of the directory component to avoid traversing the link at the file level (of course, this won't work if '../../' is also a link):

Code
#!/usr/bin/perl 
#
use strict;
use warnings;

use Cwd qw(abs_path);
use File::Basename;

my $link = $ARGV[0];
die "usage: $0 <link_name>" unless $link;
unless (-l $link) {
print "WARNING: $link isn't a symlink. This won't prove anything.\n";
}
print abs_path(dirname($link)) . '/' . basename($link) . "\n";


Cheers,

Larry


(This post was edited by 1arryb on May 20, 2009, 1:56 PM)


rahulgupta70
New User

May 20, 2009, 10:23 PM


Views: 6834
Re: [1arryb] absoulte path for symbolic link

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

Code

In Reply To


Code



rahulgupta70
New User

May 20, 2009, 10:24 PM


Views: 6833
Re: [1arryb] absoulte path for symbolic link

Thanks Larry !!!


Rahul