我应该将什么用于Perl脚本的shebang线?

时间:2021-01-31 07:10:26

Which of these is better or faster to use as the shebang line for a Perl script?

使用哪个更好或更快作为Perl脚本的shebang行?

#! perl

#! perl.exe

#! fullpath/perl(/perl.exe)

#! partialpath/perl(/perl.exe)

And, when using #!perl, when it works on a particular system, how do I find out in the script which perl interpreter I'm using so I can put that one into the shebang line?

并且,当使用#!perl时,当它在特定系统上运行时,如何在脚本中找到我正在使用的perl解释器,以便将其放入shebang行?


And, if using a /path/path/perl, are * or ... allowed to be used for the folders?

而且,如果使用/ path / path / perl,是*还是......允许用于文件夹?

8 个解决方案

#1


60  

If you have to hard code #!, use #!/usr/bin/env perl. Why? What you want is for the Perl program to run with the user's preferred Perl. That's going to be the first on in their PATH. #!perl doesn't do what I mean, it doesn't search the user's PATH, #!/usr/bin/env perl is how you pull that off. /usr/bin/env will always be there on Unix systems.

如果你需要硬编码#!,请使用#!/ usr / bin / env perl。为什么?你想要的是Perl程序与用户首选的Perl一起运行。这将是他们的PATH中的第一个。 #!perl没有做我的意思,它没有搜索用户的路径,#!/ usr / bin / env perl就是你如何解决这个问题。 / usr / bin / env将永远存在于Unix系统上。

If the user is using Windows, as others have pointed out, it doesn't matter. Windows doesn't use #! it uses file extension associations. Make sure your program is called foo.pl or something and it'll work. But include the #! line anyway as some utilities and editors make use of it.

如果用户正在使用Windows,正如其他人所指出的那样,这并不重要。 Windows不使用#!它使用文件扩展名关联。确保你的程序被称为foo.pl或其他东西,它会工作。但包括#!无论如何,一些实用程序和编辑器都使用它。

If you're shipping code, let the installer take care of it. Both MakeMaker/Makefile.PL and Module::Build/Build.PL will change your #! line to match the perl the user used to install with. They will take care of this problem for you.

如果您要发送代码,请让安装人员负责处理。 MakeMaker / Makefile.PL和Module :: Build / Build.PL都会改变你的#!用于匹配用户用于安装的perl的行。他们会为你解决这个问题。

If you are installing code for your own production use, you should use the full path to a particular copy of perl. Which copy of perl? One specific to your project. Does this mean you need to compile perl for every project? No, you can make a symlink. Project foo might have /usr/local/bin/fooperl point at /usr/bin/perl5.18. Use #!/usr/local/bin/fooperl. Now if you decide to upgrade perl you can do it per project by changing the symlink.

如果要为自己的生产用途安装代码,则应使用perl特定副本的完整路径。 perl的哪个副本?一个特定于您的项目。这是否意味着您需要为每个项目编译perl?不,你可以制作一个符号链接。 Project foo可能/ usr / local / bin / fooperl指向/usr/bin/perl5.18。使用#!/ usr / local / bin / fooperl。现在,如果您决定升级perl,则可以通过更改符号链接为每个项目执行此操作。

#2


8  

A Windows she-bang (deduced from the perl.exe bit) seems irrelevant since your (ahem) "shell" probably does not even parse it (correct me if I am wrong, could have been changed lately).

Windows she-bang(从perl.exe位推断出来)似乎无关紧要,因为你的(咳)“shell”可能甚至不解析它(如果我错了,请纠正我,最近可能会改变)。

Some command line flags may still be picked up by Perl itself though (according to this thread).

Perl本身仍然可以获取一些命令行标志(根据此线程)。

#3


6  

  1. As ChristopheD noted, I can confirm from practice (ActivePerl on XP) that the shebang line is not really necessary on Windows.

    正如ChristopheD指出的那样,我可以从实践中确认(XP上的ActivePerl),在Windows上不需要shebang线。

    A shebang line tells a Unix shell which interpreter to pass the script to.

    一个shebang行告诉Unix shell哪个解释器将脚本传递给。

    On Windows, the program to pass the script to will be determined by associations based on the extension.

    在Windows上,传递脚本的程序将由基于扩展名的关联确定。

  2. On Unix, the third option (full path to perl executable) is best.

    在Unix上,第三个选项(perl可执行文件的完整路径)是最好的。

    And yes, you can use ".." in theory (shell doesn't care) but you should not really use relative path - you never know what your current working directory when executing a script will be.

    是的,你可以在理论上使用“...”(shell并不关心),但你不应该真正使用相对路径 - 你永远不知道执行脚本时你当前的工作目录是什么。

#4


5  

If you are running CGI via Apache on Windows, the SHEBANG IS USED. You will need the fullpath to perl.

如果您在Windows上通过Apache运行CGI,则使用SHEBANG。你需要perl的完整路径。

#5


4  

If you're developing in Unix using Perl and you use "perlbrew" to easily switch between different versions of Perl, then the "!#/usr/bin/env perl" shebang line works well.

如果您使用Perl在Unix上进行开发,并使用“perlbrew”轻松地在不同版本的Perl之间切换,那么“!#/ usr / bin / env perl”shebang系列运行良好。

#6


2  

The first line stands for shebang. It basically tells the program where Perl interpreter is located since Perl is interpreted language. On Linux you can type in terminal:

第一行代表shebang。它基本上告诉程序Perl解释器所在的位置,因为Perl是解释语言。在Linux上,您可以输入终端:

whereis perl

which will give you exact location of it. Usually it's inside /usr/bin/perl. This means that you want to make shebang regarding to /usr/bin/perl

这将给你准确的位置。通常它在/ usr / bin / perl中。这意味着您希望将shebang与/ usr / bin / perl相关联

#! /usr/bin/perl

use strict;
use warnings;
use v5.10.1;
.
.
.

This is just some good practice, hence it's obviously fastest solution.

这只是一些很好的实践,因此它显然是最快的解决方案。

I hope you find this useful,

希望这个对你有帮助,

Thanks.

谢谢。

#7


1  

And, when using "#! perl", when it works on a particular system, what is the print() for showing the full path to perl.exe, that could be included into the Shebang Line ?

并且,当使用“#!perl”时,当它在特定系统上工作时,用于显示perl.exe完整路径的print()是什么,可以包含在Shebang Line中?

Well, if you're using the print statement you are already executing perl code, so...

好吧,如果您正在使用print语句,那么您已经在执行perl代码,所以...

#8


1  

This is one of the things which I dislike about Perl.

这是我不喜欢Perl的事情之一。

On Windows, if you are using ActiveState Perl at least, if the file ends with .pl then the Windows registry will run the Perl interpreter for you, regardless of the shebang line. On Cygwin, I am not sure why but #! perl works too. On Unix you should put the full path to your Perl executable in the shebang line. Schwern's idea of using env is convenient, but has some danger, as I pointed out in a comment.

在Windows上,如果您至少使用ActiveState Perl,如果文件以.pl结尾,则Windows注册表将为您运行Perl解释器,而不管shebang行。在Cygwin,我不确定为什么,但#! perl也有效。在Unix上,您应该在shebang行中放置Perl可执行文件的完整路径。 Schwern使用env的想法很方便,但有一些危险,正如我在评论中指出的那样。

This is why I suggest to you that the best solution is to package your Perl scripts as CPAN modules. CPAN installers like Module::Build will then change the shebang line to the full path to your Perl interpreter. (I am not sure whether Schwern's installer, ExtUtils::MakeMaker, does this or uses env, since I don't use it.)

这就是为什么我建议您最好的解决方案是将Perl脚本打包为CPAN模块。像Module :: Build这样的CPAN安装程序会将shebang行更改为Perl解释器的完整路径。 (我不确定Schwern的安装程序ExtUtils :: MakeMaker是否执行此操作或使用env,因为我不使用它。)

#1


60  

If you have to hard code #!, use #!/usr/bin/env perl. Why? What you want is for the Perl program to run with the user's preferred Perl. That's going to be the first on in their PATH. #!perl doesn't do what I mean, it doesn't search the user's PATH, #!/usr/bin/env perl is how you pull that off. /usr/bin/env will always be there on Unix systems.

如果你需要硬编码#!,请使用#!/ usr / bin / env perl。为什么?你想要的是Perl程序与用户首选的Perl一起运行。这将是他们的PATH中的第一个。 #!perl没有做我的意思,它没有搜索用户的路径,#!/ usr / bin / env perl就是你如何解决这个问题。 / usr / bin / env将永远存在于Unix系统上。

If the user is using Windows, as others have pointed out, it doesn't matter. Windows doesn't use #! it uses file extension associations. Make sure your program is called foo.pl or something and it'll work. But include the #! line anyway as some utilities and editors make use of it.

如果用户正在使用Windows,正如其他人所指出的那样,这并不重要。 Windows不使用#!它使用文件扩展名关联。确保你的程序被称为foo.pl或其他东西,它会工作。但包括#!无论如何,一些实用程序和编辑器都使用它。

If you're shipping code, let the installer take care of it. Both MakeMaker/Makefile.PL and Module::Build/Build.PL will change your #! line to match the perl the user used to install with. They will take care of this problem for you.

如果您要发送代码,请让安装人员负责处理。 MakeMaker / Makefile.PL和Module :: Build / Build.PL都会改变你的#!用于匹配用户用于安装的perl的行。他们会为你解决这个问题。

If you are installing code for your own production use, you should use the full path to a particular copy of perl. Which copy of perl? One specific to your project. Does this mean you need to compile perl for every project? No, you can make a symlink. Project foo might have /usr/local/bin/fooperl point at /usr/bin/perl5.18. Use #!/usr/local/bin/fooperl. Now if you decide to upgrade perl you can do it per project by changing the symlink.

如果要为自己的生产用途安装代码,则应使用perl特定副本的完整路径。 perl的哪个副本?一个特定于您的项目。这是否意味着您需要为每个项目编译perl?不,你可以制作一个符号链接。 Project foo可能/ usr / local / bin / fooperl指向/usr/bin/perl5.18。使用#!/ usr / local / bin / fooperl。现在,如果您决定升级perl,则可以通过更改符号链接为每个项目执行此操作。

#2


8  

A Windows she-bang (deduced from the perl.exe bit) seems irrelevant since your (ahem) "shell" probably does not even parse it (correct me if I am wrong, could have been changed lately).

Windows she-bang(从perl.exe位推断出来)似乎无关紧要,因为你的(咳)“shell”可能甚至不解析它(如果我错了,请纠正我,最近可能会改变)。

Some command line flags may still be picked up by Perl itself though (according to this thread).

Perl本身仍然可以获取一些命令行标志(根据此线程)。

#3


6  

  1. As ChristopheD noted, I can confirm from practice (ActivePerl on XP) that the shebang line is not really necessary on Windows.

    正如ChristopheD指出的那样,我可以从实践中确认(XP上的ActivePerl),在Windows上不需要shebang线。

    A shebang line tells a Unix shell which interpreter to pass the script to.

    一个shebang行告诉Unix shell哪个解释器将脚本传递给。

    On Windows, the program to pass the script to will be determined by associations based on the extension.

    在Windows上,传递脚本的程序将由基于扩展名的关联确定。

  2. On Unix, the third option (full path to perl executable) is best.

    在Unix上,第三个选项(perl可执行文件的完整路径)是最好的。

    And yes, you can use ".." in theory (shell doesn't care) but you should not really use relative path - you never know what your current working directory when executing a script will be.

    是的,你可以在理论上使用“...”(shell并不关心),但你不应该真正使用相对路径 - 你永远不知道执行脚本时你当前的工作目录是什么。

#4


5  

If you are running CGI via Apache on Windows, the SHEBANG IS USED. You will need the fullpath to perl.

如果您在Windows上通过Apache运行CGI,则使用SHEBANG。你需要perl的完整路径。

#5


4  

If you're developing in Unix using Perl and you use "perlbrew" to easily switch between different versions of Perl, then the "!#/usr/bin/env perl" shebang line works well.

如果您使用Perl在Unix上进行开发,并使用“perlbrew”轻松地在不同版本的Perl之间切换,那么“!#/ usr / bin / env perl”shebang系列运行良好。

#6


2  

The first line stands for shebang. It basically tells the program where Perl interpreter is located since Perl is interpreted language. On Linux you can type in terminal:

第一行代表shebang。它基本上告诉程序Perl解释器所在的位置,因为Perl是解释语言。在Linux上,您可以输入终端:

whereis perl

which will give you exact location of it. Usually it's inside /usr/bin/perl. This means that you want to make shebang regarding to /usr/bin/perl

这将给你准确的位置。通常它在/ usr / bin / perl中。这意味着您希望将shebang与/ usr / bin / perl相关联

#! /usr/bin/perl

use strict;
use warnings;
use v5.10.1;
.
.
.

This is just some good practice, hence it's obviously fastest solution.

这只是一些很好的实践,因此它显然是最快的解决方案。

I hope you find this useful,

希望这个对你有帮助,

Thanks.

谢谢。

#7


1  

And, when using "#! perl", when it works on a particular system, what is the print() for showing the full path to perl.exe, that could be included into the Shebang Line ?

并且,当使用“#!perl”时,当它在特定系统上工作时,用于显示perl.exe完整路径的print()是什么,可以包含在Shebang Line中?

Well, if you're using the print statement you are already executing perl code, so...

好吧,如果您正在使用print语句,那么您已经在执行perl代码,所以...

#8


1  

This is one of the things which I dislike about Perl.

这是我不喜欢Perl的事情之一。

On Windows, if you are using ActiveState Perl at least, if the file ends with .pl then the Windows registry will run the Perl interpreter for you, regardless of the shebang line. On Cygwin, I am not sure why but #! perl works too. On Unix you should put the full path to your Perl executable in the shebang line. Schwern's idea of using env is convenient, but has some danger, as I pointed out in a comment.

在Windows上,如果您至少使用ActiveState Perl,如果文件以.pl结尾,则Windows注册表将为您运行Perl解释器,而不管shebang行。在Cygwin,我不确定为什么,但#! perl也有效。在Unix上,您应该在shebang行中放置Perl可执行文件的完整路径。 Schwern使用env的想法很方便,但有一些危险,正如我在评论中指出的那样。

This is why I suggest to you that the best solution is to package your Perl scripts as CPAN modules. CPAN installers like Module::Build will then change the shebang line to the full path to your Perl interpreter. (I am not sure whether Schwern's installer, ExtUtils::MakeMaker, does this or uses env, since I don't use it.)

这就是为什么我建议您最好的解决方案是将Perl脚本打包为CPAN模块。像Module :: Build这样的CPAN安装程序会将shebang行更改为Perl解释器的完整路径。 (我不确定Schwern的安装程序ExtUtils :: MakeMaker是否执行此操作或使用env,因为我不使用它。)