Perl 脚本的 shebang 行应该使用什么?

发布于 2024-09-01 06:47:11 字数 323 浏览 4 评论 0原文

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

#! perl

#! perl.exe

#! fullpath/perl(/perl.exe)

#! partialpath/perl(/perl.exe)

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


并且,如果使用 /path/path/perl,是否允许将 *... 用于文件夹?

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

#! 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?


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

冰葑 2024-09-08 06:47:11

如果必须硬编码#!,请使用#!/usr/bin/env perl。为什么?您想要的是 Perl 程序以用户首选的 Perl 运行。这将是他们的 PATH 中的第一个。 #!perl 并没有达到我的意思,它不会搜索用户的 PATH,#!/usr/bin/env perl 就是你实现这一目标的方法。 /usr/bin/env 在 Unix 系统上始终存在。

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

如果您要传送代码,请让安装程序来处理。 MakeMaker/Makefile.PLModule::Build/Build.PL 都会改变你的#!行来匹配用户用来安装的 perl。他们会为您解决这个问题。

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

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.

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.

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.

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.

合约呢 2024-09-08 06:47:11

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

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

左秋 2024-09-08 06:47:11

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

不过,Perl 本身仍可能会拾取某些命令行标志(根据此线程)。

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).

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

桜花祭 2024-09-08 06:47:11
  1. 正如 ChristopheD 所指出的,我可以通过实践(XP 上的 ActivePerl)确认 shebang 行在 Windows 上并不是真正必要的。

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

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

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

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

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

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

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

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

    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.

孤芳又自赏 2024-09-08 06:47:11

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

我最初将 shebang 行中的前 2 个字符颠倒了。刚刚修复/编辑了它。

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.

I originally had the first 2 characters in the shebang line reversed. Just fixed/edited that.

终难遇 2024-09-08 06:47:11

第一行代表 shebang。它基本上告诉程序 Perl 解释器位于哪里,因为 Perl 是解释性语言。在 Linux 上,您可以输入terminal:,

whereis perl

这将为您提供它的确切位置。通常它位于/usr/bin/perl内部。这意味着您想要对 /usr/bin/perl 进行 shebang

#! /usr/bin/perl

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

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

我希望你觉得这很有用,

谢谢。

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:

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

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.

方圜几里 2024-09-08 06:47:11

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

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

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 ?

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

醉生梦死 2024-09-08 06:47:11

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

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

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

This is one of the things which I dislike about 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.

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文