Perl command for same behaviour as readlink?

719*_*016 1 perl gnu-coreutils

What is the equivalent Perl command to the GNU coreutils command readlink -f?

If any component of the file name except the last one is missing or unavailable, readlink produces no output and exits with a nonzero exit code. A trailing slash is ignored.

dev*_*ull 8

你可以使用Cwd:

use Cwd 'abs_path';
my $path = "/some/arbitrary/path";
print abs_path($path);
Run Code Online (Sandbox Code Playgroud)

测试:

for q in exists imaginary imarginary/imaginary ; do
   echo "$q"
   echo -n "readlink -f: " ; readlink -f "$q"
   echo -n "abs_path:    " ; perl -MCwd=abs_path -E'say abs_path $ARGV[0]' "$q"
   echo
done
Run Code Online (Sandbox Code Playgroud)

输出:

exists
readlink -f: /home/eric/exists
abs_path:    /home/eric/exists

imaginary
readlink -f: /home/eric/imaginary
abs_path:    /home/eric/imaginary

imaginary/imaginary
readlink -f: abs_path:
Run Code Online (Sandbox Code Playgroud)