简体   繁体   English

如何使用 Apache 2 在 Windows 上忽略 Perl shebang?

[英]How do I ignore the Perl shebang on Windows with Apache 2?

I have set up a local Perl web environment on my Windows machine.我已经在我的 Windows 机器上设置了本地 Perl Web 环境。 The application I'm working on is originally from a Linux server, and so the shebang for source .pl files look like so:我正在处理的应用程序最初来自 Linux 服务器,因此源.pl文件的 shebang 如下所示:

#!/usr/bin/perl

This causes the following error on my Windows dev machine:这会在我的 Windows 开发机器上导致以下错误:

(OS 2)The system cannot find the file specified.

Is it possible to change my Apache 2 conf so that the shebang is ignored on my Windows machine?是否可以更改我的 Apache 2 conf 以便在我的 Windows 机器上忽略shebang? Of course I could set the shebang to #!c:\\perl\\bin\\perl.exe , that much is obvious;当然,我可以将 shebang 设置为#!c:\\perl\\bin\\perl.exe ,这很明显; but the problem comes to deploying the updated files.但问题在于部署更新的文件。 Clearly it would be very inconvenient to change this back on each deploy.显然,在每次部署时将其改回是非常不方便的。 I am using ActivePerl on Windows 7.我在 Windows 7 上使用ActivePerl

Update:更新:

I should have mentioned that I need to keep the shebang so that the scripts will work on our shared hosting Linux production server.我应该提到我需要保留shebang,以便脚本可以在我们的共享托管Linux生产服务器上运行。 If I did not have this constraint and I didn't have to use the shebang, the obvious answer would be to just not use it.如果我没有这个约束并且我不必使用shebang,那么显而易见的答案就是不使用它。

I use #!/usr/bin/perl in my scripts and configure Apache on Windows to ignore the shebang line.我在脚本中使用#!/usr/bin/perl并在 Windows 上配置 Apache 以忽略 shebang 行。 Add添加

 ScriptInterpreterSource Registry-Strict

to your httpd.conf and set up the Windows Registry key as explained in the Apache docs .到您的httpd.conf并按照Apache 文档中的说明设置 Windows 注册表项。

Here is what I get when I export the key:这是我导出密钥时得到的:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.pl\Shell\ExecCGI\Command]
@="c:\\opt\\perl\\bin\\perl.exe"

I have been using this setup with Apache and ActiveState Perl on my Windows laptop and the Apache and Perl distributions that come with ArchLinux on my server.我一直在我的 Windows 笔记本电脑上的 Apache 和 ActiveState Perl 以及我的服务器上的 ArchLinux 附带的 Apache 和 Perl 发行版中使用此设置。

The Apache docs (to which I linked above) state: Apache 文档(我在上面链接到的)状态:

The option Registry-Strict which is new in Apache 2.0 does the same thing as Registry but uses only the subkey Shell\\ExecCGI\\Command . Apache 2.0 中新增的选项 Registry-Strict 与 Registry 执行相同的操作,但仅使用子项Shell\\ExecCGI\\Command The ExecCGI key is not a common one. ExecCGI密钥并不常见。 It must be configured manually in the windows registry and hence prevents accidental program calls on your system .它必须在 Windows 注册表中手动配置,从而防止系统上的意外程序调用 (emphasis mine) (强调我的)

There is no portable shebang line.没有便携式shebang线。 Even on the same platform and architecture, someone might have installed perl is a different location.即使在相同的平台和体系结构上,也可能有人在不同的位置安装了 perl。

The trick is to not install modules and scripts by hand.诀窍是不要手动安装模块和脚本。 When you package everything as distributions and use the module toolchain, the shebang lines are modified automatically to point to the perl you used to install everything.当您将所有内容打包为发行版并使用模块工具链时,shebang 行会自动修改为指向您用于安装所有内容的 perl。 You shouldn't have to think about these details.你不应该考虑这些细节。 :) :)

I use # ! /usr/bin/env perl我用 # ! /usr/bin/env perl ! /usr/bin/env perl as the shebang on all of my perl, whether on *nix or Windows. ! /usr/bin/env perl作为我所有! /usr/bin/env perl上的 shebang,无论是在 *nix 还是 Windows 上。 Windows just ignores it, and the Unixen follow env to the chosen perl disto. Windows 只是忽略它,Unixen 跟随 env 到所选的 perl disto。

我进行这项工作的方式是将 perl.exe 复制到 c:/usr/bin/ 并将其重命名为 perl(剥离 .exe)

In win7 and up you can also do this with the "dos" command mklink.在 win7 及更高版本中,您也可以使用“dos”命令 mklink 执行此操作。

Start a cmd shell as administrator and do something like the following:以管理员身份启动 cmd shell 并执行以下操作:

mklink /d c:\usr c:\Perl       # Activestate perl in c:\Perl\bin\perl.exe
mklink /d c:\usr c:\xampp\perl # Xampp perl in c:\xampp\perl\bin\perl.exe
  1. Install any Windows Bash flavor (such as Cygwin, MSYS2 or GnuWin32);安装任何Windows Bash 风格(例如 Cygwin、MSYS2 或 GnuWin32);
  2. Create a trivial redirecting shell script:创建一个简单的重定向 shell 脚本:

     exec "@"
  3. Create a registry entry:创建一个注册表项:

     Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\\.cgi\\Shell\\ExecCGI\\Command] @="<path-to-sh> <path-to-script>" [HKEY_CLASSES_ROOT\\.pl\\Shell\\ExecCGI\\Command] @="<path-to-sh> <path-to-script>" [HKEY_CLASSES_ROOT\\.py\\Shell\\ExecCGI\\Command] @="<path-to-sh> <path-to-script>"

    (...and so on.) (...等等。)

  4. Jot in your httpd.conf file:在您的httpd.conf文件中记录:

     ScriptInterpreterSource Registry

Apache will now resolve Unix shebangs relative to the interpretation given by your choosen Bash flavor. Apache 现在将根据您选择的 Bash 风格给出的解释来解析 Unix shebang。 This gives much more flexibility than hardcoding interpreter paths in the registry.这比在注册表中硬编码解释器路径提供更大的灵活性。

I don't have Windows handy, but perlcritic says:我手头没有 Windows,但 perlcritic 说:

my $desc = q{Found platform-specific perl shebang line};
my $expl = q{Perl source in parrot should use the platform-independent shebang line: #! perl};

So, I guess #! perl所以,我猜#! perl #! perl should work. #! perl应该可以工作。

Edit: doesn't work on linux;编辑:在 linux 上不起作用; apparently works in parrot , although I don't see how they manage that.显然适用于parrot ,尽管我不知道他们是如何管理的。

How to use Linux shebang (#!/usr/bin/perl) when running Perl based web-site with {site-name} on localhost in Windows 10?在 Windows 10 中的本地主机上使用 {site-name} 运行基于 Perl 的网站时,如何使用 Linux shebang (#!/usr/bin/perl)?

This works for me:这对我有用:

  • XAMPP on localhost\\{site-name} (C:\\xampp\\htdocs\\{site-name}) XAMPP 在 localhost\\{site-name} (C:\\xampp\\htdocs\\{site-name})
  • independently installed Strawberry Perl (C:\\Perl\\perl\\bin) because Perl included in XAMPP package is not satisfactory独立安装 Strawberry Perl (C:\\Perl\\perl\\bin) 因为 XAMPP 包中包含的 Perl 不令人满意

In default configuration you must use this shebang:在默认配置中,您必须使用此shebang:

#!C:\perl\perl\bin\perl.exe -IC:\xampp\htdocs\{site-name}

Is it possible to include directory (after -I switch) permanently to @INC?是否可以将目录(在 -I 切换后)永久包含到@INC?

Yes, you can do it by setting the PERLLIB or PERL5LIB environment variables.是的,您可以通过设置 PERLLIB 或 PERL5LIB 环境变量来实现。 This works when running perl script from command line, but surprisingly it is ignored by apache server in XAMPP.这在从命令行运行 perl 脚本时有效,但令人惊讶的是它被 XAMPP 中的 apache 服务器忽略。 However one of @INC directories (C:/Perl/perl/site/lib) is usually empty so you can make symbolic link to your web-site directory:但是,@INC 目录之一 (C:/Perl/perl/site/lib) 通常是空的,因此您可以创建指向网站目录的符号链接:

mklink /D c:\perl\perl\site\lib C:\xampp\htdocs\{site-name}

Now, you can use this shebang:现在,你可以使用这个shebang:

#!C:\perl\perl\bin\perl.exe

Moreover, you can create directory c:\\usr\\bin此外,您可以创建目录 c:\\usr\\bin

md c:\usr\bin

and copy perl.exe from C:\\perl\\perl\\bin\\并从 C:\\perl\\perl\\bin\\ 复制 perl.exe

copy C:\perl\perl\bin\perl.exe c:\usr\bin\perl.exe

(Another mklink trick is not working here from some reasons.) (由于某些原因,另一个 mklink 技巧在这里不起作用。)

Finally, you can use Unix-styled shebang on Windows:最后,您可以在 Windows 上使用 Unix 风格的 shebang:

#!/usr/bin/perl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM