How to get Absolute Path for symlink file?

# test-> a.pl

my $file = '/home/joe/test';
if ( -f $file && -l $file )  {
    print readlink( $file ) ;
}

how to get absolute path for symlink file?

+5
source share
3 answers

Cwd provides such abs_path functions .

#!/usr/bin/perl -w

use Cwd 'abs_path';

my $file='/home/joe/test';
if( -f $file && -l $file ) {
    print abs_path($file);
}
+10
source

if you use File :: Spec rel2abs along with readlink, you will get the paragraph path even if it is a symbolic link to another symbolic link

use File::Spec;

$path = File::Spec->rel2abs( readlink($file) ) ;
+3
source

, . .

my $good = \`readlink -f $0\`;
-1

All Articles