如何使用 Apache 2 在 Windows 上忽略 Perl shebang?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2036577/
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 do I ignore the Perl shebang on Windows with Apache 2?
提问by Nick Bolton
I have set up a local Perl web environment on my Windows machine. The application I'm working on is originally from a Linux server, and so the shebang for source .pl
files look like so:
我已经在我的 Windows 机器上设置了本地 Perl Web 环境。我正在处理的应用程序最初来自 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? Of course I could set the shebang to #!c:\perl\bin\perl.exe
, that much is obvious; 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 ActivePerlon Windows 7.
是否可以更改我的 Apache 2 conf 以便在我的 Windows 机器上忽略shebang?当然,我可以将 shebang 设置为#!c:\perl\bin\perl.exe
,这很明显;但问题在于部署更新的文件。显然,在每次部署时将其改回是非常不方便的。我在 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. 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,以便脚本可以在我们的共享托管Linux 生产服务器上运行。如果我没有这个约束并且我不必使用shebang,那么显而易见的答案就是不使用它。
回答by Sinan ünür
I use #!/usr/bin/perl
in my scripts and configure Apache on Windows to ignore the shebang line. Add
我#!/usr/bin/perl
在我的脚本中使用并在 Windows 上配置 Apache 以忽略 shebang 行。添加
ScriptInterpreterSource Registry-Strict
to your httpd.conf
and set up the Windows Registry key as explained in the Apache docs.
你httpd.conf
并设立作为解释的Windows注册表键的Apache文档。
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
. TheExecCGI
key is not a common one. It must be configured manually in the windows registry and hence prevents accidental program calls on your system. (emphasis mine)
Apache 2.0 中新增的选项 Registry-Strict 与 Registry 执行相同的操作,但仅使用子项
Shell\ExecCGI\Command
。该ExecCGI
键不是常见的一种。它必须在 Windows 注册表中手动配置,从而防止系统上的意外程序调用。(强调我的)
回答by brian d foy
There is no portable shebang line. Even on the same platform and architecture, someone might have installed perl is a different location.
没有便携式shebang线。即使在相同的平台和体系结构上,也可能有人在不同的位置安装了 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. You shouldn't have to think about these details. :)
诀窍是不要手动安装模块和脚本。当您将所有内容打包为发行版并使用模块工具链时,shebang 行会自动修改为指向您用于安装所有内容的 perl。你不应该考虑这些细节。:)
回答by gWaldo
I use #! /usr/bin/env perl
as the shebang on all of my perl, whether on *nix or Windows. Windows just ignores it, and the Unixen follow env to the chosen perl disto.
我! /usr/bin/env perl
在所有 perl 上都使用 #作为shebang,无论是在*nix 还是Windows 上。Windows 只是忽略它,Unixen 跟随 env 到所选的 perl disto。
回答by John Vinopal
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
回答by Htbaa
The way I had this working was to copy perl.exe to c:/usr/bin/ and rename it to perl (strip the .exe)
我的工作方式是将 perl.exe 复制到 c:/usr/bin/ 并将其重命名为 perl(去除 .exe)
回答by Pedro Silva
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
should work.
所以,我想#! perl
应该工作。
Edit: doesn't work on linux; apparently works in parrot
, although I don't see how they manage that.
编辑:在 linux 上不起作用;显然在parrot
,虽然我不明白他们是如何管理的。
回答by alecov
- Install anyWindows Bash flavor (such as Cygwin, MSYS2 or GnuWin32);
Create a trivial redirecting shell script:
exec "@"
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.)
Jot in your
httpd.conf
file:ScriptInterpreterSource Registry
- 安装任何Windows Bash 风格(例如 Cygwin、MSYS2 或 GnuWin32);
创建一个简单的重定向 shell 脚本:
exec "@"
创建一个注册表项:
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>"
(...等等。)
在你的
httpd.conf
文件中记下:ScriptInterpreterSource Registry
Apache will now resolve Unix shebangs relative to the interpretation given by your choosen Bash flavor. This gives muchmore flexibility than hardcoding interpreter paths in the registry.
Apache 现在将根据您选择的 Bash 风格给出的解释来解析 Unix shebang。这比在注册表中硬编码解释器路径提供了更大的灵活性。
回答by natT
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})
- independently installed Strawberry Perl (C:\Perl\perl\bin) because Perl included in XAMPP package is not satisfactory
- XAMPP 在 localhost\{site-name} (C:\xampp\htdocs\{site-name})
- 独立安装 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. This works when running perl script from command line, but surprisingly it is ignored by apache server in XAMPP. However one of @INC directories (C:/Perl/perl/site/lib) is usually empty so you can make symbolic link to your web-site directory:
是的,您可以通过设置 PERLLIB 或 PERL5LIB 环境变量来实现。这在从命令行运行 perl 脚本时有效,但令人惊讶的是它被 XAMPP 中的 apache 服务器忽略。但是,@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