如何获得perl可执行文件的清理绝对路径但保留符号链接?(从$^X
变量或无论如何)
perl 可执行文件示例:
$ ls -l /opt/local/bin/perl
lrwxr-xr-x 1 root admin 8 18 nov 02:46 /opt/local/bin/perl -> perl5.12
/opt/local/bin/perl
无论我如何运行 perl 解释器,我都想进入我的脚本。
测试代码(changesbang.pl):
use strict;
use warnings;
use Cwd;
use File::Spec;
use Config;
my $fmt = "%-25s: %s\n";
printf $fmt, "The value of ^X", $^X;
printf $fmt, "Cwd::realpath", Cwd::realpath($^X);
printf $fmt, "Cwd::abs_path", Cwd::abs_path($^X);
printf $fmt, "File::Spec->cannonpath", File::Spec->canonpath($^X);
printf $fmt, "File::Spec->rel2abs", File::Spec->rel2abs($^X);
printf $fmt, "Config{perlpath}", $Config{perlpath};
现在当上面运行时
$ ../../../../../opt/local/bin/perl changesbang.pl
The value of ^X : ../../../../../opt/local/bin/perl
Cwd::realpath : /opt/local/bin/perl5.12
Cwd::abs_path : /opt/local/bin/perl5.12
File::Spec->cannonpath : ../../../../../opt/local/bin/perl
File::Spec->rel2abs : /Users/jm/Develop/perl/changesbang/../../../../../opt/local/bin/perl
Config{perlpath} : /opt/local/bin/perl5.12
没有人把想要的东西还给我/opt/local/bin/perl
问题是:如何通过约束获得想要的 perl 的可执行路径:
- 应该清理绝对路径
- 但如果它是符号链接,应该保留它(不要将它更改为它的目标)