Getting perl's executable absolute path with keeping symlinks -


how cleaned-up absolute path perl's executable keeping symlinks? (from $^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 /opt/local/bin/perl in script regardless how run perl interpreter.

the testing code (changesbang.pl):

use strict; use warnings; use cwd; use file::spec; use config;  $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 above as

$ ../../../../../opt/local/bin/perl changesbang.pl 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 1 returns me wanted /opt/local/bin/perl

the question is: how wanted perl's executable path constrains:

  • should cleaned up absolute path
  • but if symbolic link, should keep (don't change it's target)

ps: here similar questions this or this no 1 gives me answer.

i suggest try

use file::spec::functions qw( rel2abs canonpath );  print canonpath(rel2abs($^x)); 

alternatively, there is

use cwd::ext 'abs_path_nd';  print abs_path_nd($^x); 

Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

How to call a javascript function after the page loads with a chrome extension? -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -