How to get the cleaned-up absolute path for the perl's executable but with keeping symlinks? (From the $^X
variable or anyhow)
example perl executable:
$ ls -l /opt/local/bin/perl
lrwxr-xr-x 1 root admin 8 18 nov 02:46 /opt/local/bin/perl -> perl5.12
I want get /opt/local/bin/perl
in my script regardless how I run the perl interpreter.
The testing code (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};
Now when run the above as
$ ../../../../../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
No one returns me the wanted /opt/local/bin/perl
The question is: how to get the wanted perl's executable path with constrains:
- should be cleaned up absolute path
- but if it is the symbolic link, should keep it (don't change it to it's target)
Ps: Here are similar questions like this or this but no one gives me answer.