获取当前进程的 perl 可执行文件的绝对路径 [英] Getting absolute path to perl executable for the current process

查看:75
本文介绍了获取当前进程的 perl 可执行文件的绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获得当前进程的 Perl 可执行文件的绝对路径?

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

$^X 会给我 Perl 可执行文件名称,但文档指出它有时会是一个相对路径,例如在 OS X 上这似乎是正确的.

$^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 似乎有一些魔法来找到绝对路径,因为它在我的 OS X 上生成的 Makefile 包含

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.

感谢 Borodin 的 $Config{perlpath} 提示.在 ExtUtils 中寻找这个,我在 ExtUtils::MM_Unix::_fixin_replace_shebang 中发现了这个花絮,我想这是 MakeMaker 用来替换 #!perl代码>使用正确的shebang行.

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};
}

推荐答案

我认为您正在寻找的是 $Config{perlpath}.

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

如果你想让你的代码非常便携,你可能需要在那个值后面附加一个文件类型;这在 perlport 文档.否则你只需要这个:

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};

这篇关于获取当前进程的 perl 可执行文件的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆