Getting the absolute path to the perl executable for the current process

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

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

ExtUtils :: MakeMaker seems to have some kind of magic to find the absolute path, since the Makefile that it creates in my OS X contains

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

but I have no idea how this is done or magic is easily accessible to others.

EDIT: Thanks Borodin for the tip $Config{perlpath}. Grepping for this in ExtUtils, I found this tidbit in ExtUtils :: MM_Unix :: _ fixin_replace_shebang, which I suppose uses MakeMaker to replace #! Perl with the correct shebang string.

    if ($ Config {startperl} = ~ m, ^ \ #!. * / perl,) {
        $ interpreter = $ Config {startperl};
        $ interpreter = ~ s, ^ \ #! ,,;
    }
    else {
        $ interpreter = $ Config {perlpath};
    }
+5
source share
3 answers

I think what you are looking for $Config{perlpath}.

If you want your code to be very portable, you may need to add a file type to this value; this is described in the documentation perlport. Otherwise, you will need the following:

use Config;
my $perl = $Config{perlpath};
+8
source

You can get it through the Config module .

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

I think that should always contain an absolute path, but I can not guarantee this.

+1
source

Perl includes the main module File :: Spec , which can translate relative paths into absolute paths.

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

All Articles