how to get the Absolute Path for symlink file?

后端 未结 2 1012
自闭症患者
自闭症患者 2020-12-20 17:50

# test-> a.pl

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

how to get the Absolu

相关标签:
2条回答
  • 2020-12-20 18:29

    Cwd provides such functionality by abs_path.

    #!/usr/bin/perl -w
    
    use Cwd 'abs_path';
    
    my $file='/home/joe/test';
    if( -f $file && -l $file ) {
        print abs_path($file);
    }
    
    0 讨论(0)
  • 2020-12-20 18:39

    if you use File::Spec rel2abs along with readlink you'll get the abs path even if it's a symlink to another symlink

    use File::Spec;
    
    $path = File::Spec->rel2abs( readlink($file) ) ;
    
    0 讨论(0)
提交回复
热议问题