Question

Is there a way to get an absolute path to the Perl executable for the current process?

$^X will give me the Perl executable name, but the doc states that it will sometimes be a relative path, and this seems to be true on OS X for example.

ExtUtils::MakeMaker seems to have some magic to find the absolute path, since the Makefile it generates on my OS X contains

PERL = /usr/local/bin/perl
FULLPERL = /usr/local/bin/perl

but I have no idea how it does this or whether the magic is readily accessible to others.

EDIT: Thanks Borodin for the $Config{perlpath} tip. Grepping for this in ExtUtils, I found this tidbit in ExtUtils::MM_Unix::_fixin_replace_shebang, which I guess is what MakeMaker uses to replace #!perl with the correct shebang line.

if ( $Config{startperl} =~ m,^\#!.*/perl, ) {
    $interpreter = $Config{startperl};
    $interpreter =~ s,^\#!,,;
}
else {
    $interpreter = $Config{perlpath};
}
Was it helpful?

Solution

I think what you're looking for is $Config{perlpath}.

If you want your code to be very portable you may have to append a file type to that value; this is described in the perlport documentation. Otherwise all you need is this:

use Config;
my $perl = $Config{perlpath};

OTHER TIPS

You can get it via the core Config module.

use Config;
say $Config{perl5};   # current perl binary

I think that should always contain an absolute path, but I can't guarantee it.

Perl includes the core module File::Spec which can translate relative paths to absolute paths.

my $full_path_to_perl = File::Spec->rel2abs($^X);

For perl6 (raku): $*EXECUTABLE-NAME contains full path of the currently running Raku interpreter (like sys.executable for Python)

To get the full command line:

say ($*EXECUTABLE-NAME, $*PROGRAM-NAME, |@*ARGS).join(' ');

More in Variable Documentation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top