Linux 如何指定在 CentOS 上使用哪个版本的 perl
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4764025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to specify which version of perl to use on CentOS
提问by DanielBryan
I am running CentOS 5.4 which only has version 5.8 of perl available by default, and I have a program which requires perl 5.10, so I compiled perl 5.10 on CentOS. How do I specify which perl I want to run the program with because the perl command uses 5.8 by default.
我正在运行 CentOS 5.4,它默认只有 5.8 版的 perl,而且我有一个需要 perl 5.10 的程序,所以我在 CentOS 上编译了 perl 5.10。如何指定我想用哪个 perl 运行程序,因为 perl 命令默认使用 5.8。
回答by gergi
The first line of the program file should reference the perl binary you wish to use: e.g.
程序文件的第一行应该引用您希望使用的 perl 二进制文件:例如
#!/usr/bin/perl
You may also want to change your PATH variable so that the directory your perl 5.10 binary is in is listed prior to the 5.8 binary directory. e.g.
您可能还想更改 PATH 变量,以便 perl 5.10 二进制文件所在的目录列在 5.8 二进制目录之前。例如
export PATH=/path/to/perl/5.10:$PATH
回答by Navi
set your PATH environment variable to point to your new perl executable. For instance
将 PATH 环境变量设置为指向新的 perl 可执行文件。例如
export PATH=/newpath/perl:$PATH
回答by mob
I like to make symbolic links to my different perl executables in /usr/local/bin
:
我喜欢在以下位置创建指向不同 perl 可执行文件的符号链接/usr/local/bin
:
$ [sudo] ln -s /path/to/perl5.10.1.exe /usr/local/bin/perl510
$ [sudo] ln -s /path/to/perl5.13.8.exe /usr/local/bin/perl513
$ ... etc. ...
$ # and just for completeness
$ ln -s /usr/bin/perl /usr/local/bin/perl58
and then just invoke:
然后调用:
$ perl510 script_to_use_with_v5.10.pl
回答by Lasse
There is a tool called alternatives that was designed to deal effectively with exactly this kind of problem. It basically gives you an easy way of switching between different version of applications by manipulating symbolic links in e.g. your bin directories.
有一种称为替代品的工具,旨在有效地处理此类问题。它基本上为您提供了一种通过操作例如 bin 目录中的符号链接在不同版本的应用程序之间切换的简单方法。
Say "man alternatives" in a terminal (or yum install alternatives, if you don't have it installed).
在终端中说“人替代品”(或者 yum install替代品,如果你没有安装它)。
回答by Ashley
I add my voice to recommending against messing with the system perl at all.
我加入了我的声音,建议完全不要弄乱系统 perl。
No one mentioned App::perlbrewyet. It allows you to have several versions of Perl and switch between them easily. This can be done manually of course but it's much easier to have this tool to do it for you; from the Pod–
还没有人提到App::perlbrew。它允许您拥有多个版本的 Perl 并在它们之间轻松切换。这当然可以手动完成,但使用此工具为您完成要容易得多;来自 Pod——
# Install some Perls
perlbrew install perl-5.12.2
perlbrew install perl-5.8.1
perlbrew install perl-5.13.6
# See what were installed
perlbrew list
# Switch perl in the $PATH
perlbrew switch perl-5.12.2
perl -v
# Switch to another version
perlbrew switch perl-5.8.1
perl -v
# Switch to a certain perl executable not managed by perlbrew.
perlbrew switch /usr/bin/perl
# Or turn it off completely. Useful when you messed up too deep.
perlbrew off
# Use 'switch' command to turn it back on.
perlbrew switch perl-5.12.2
回答by slm
BTW, The perlbrew package is available for installation from the EPELrepository for CentOS 5.x. I tried to install just this rpm initially but it has a number of dependencies so I opted to add the EPEL repository to my list of yum repos on my box.
顺便说一句, perlbrew 包可以从CentOS 5.x的EPEL存储库安装。我最初尝试只安装这个 rpm,但它有许多依赖项,所以我选择将 EPEL 存储库添加到我的盒子上的 yum 存储库列表中。
回答by Scott Stensland
solution has two parts ... first edit myscript.pl which wants specific version
解决方案有两部分......首先编辑需要特定版本的 myscript.pl
old
#!/usr/bin/perl
new
#!/usr/bin/env perl
above has no impact on normal executions of the script ... when you want above myscript.pl to use a specific perl version create a wrapper script which contains
以上对脚本的正常执行没有影响......当你想在 myscript.pl 上面使用特定的 perl 版本时,创建一个包含
export PATH=/cool/new/version/perl:$PATH
# now execute script on following line
/path/to/myscript.pl
this way other invocations of the script remain unchanged and they just use default perl whereas launcher wrapper script executes same myscript.pl script with chosen perl version
这样,脚本的其他调用保持不变,它们只使用默认的 perl,而启动器包装脚本使用选定的 perl 版本执行相同的 myscript.pl 脚本