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

发布于 2024-08-17 12:29:48 字数 629 浏览 7 评论 0 原文

我已经在我的 Windows 机器上设置了本地 Perl Web 环境。我正在开发的应用程序最初来自 Linux 服务器,因此源 .pl 文件的 shebang 如下所示:

#!/usr/bin/perl

这会导致我的 Windows 开发计算机上出现以下错误:

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

Is it possible to change我的 Apache 2 conf 以便在我的 Windows 计算机上忽略 shebang?当然,我可以将 shebang 设置为 #!c:\perl\bin\perl.exe,这是显而易见的;但问题在于部署更新的文件。显然,在每次部署时都将其改回是非常不方便的。我在 Windows 7 上使用 ActivePerl

更新:

我应该提到我需要保持 shebang这些脚本将在我们的共享托管 Linux 生产服务器上运行。如果我没有这个限制并且我不必使用 shebang,那么显而易见的答案就是不使用它。

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:

#!/usr/bin/perl

This causes the following error on my Windows dev machine:

(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 ActivePerl on Windows 7.

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.

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

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

发布评论

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

评论(8

虚拟世界 2024-08-24 12:29:48

我在脚本中使用 #!/usr/bin/perl 并将 Windows 上的 Apache 配置为忽略 shebang 行。添加

 ScriptInterpreterSource Registry-Strict

到您的 httpd.conf 并设置 Windows 注册表项,如 Apache 文档

以下是导出密钥时得到的结果:

Windows Registry Editor Version 5.00

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

我一直在我的 Windows 笔记本电脑上使用此设置以及 Apache 和 ActiveState Perl,以及在我的服务器上使用 ArchLinux 附带的 Apache 和 Perl 发行版。

Apache 文档(我在上面链接到)指出:

Apache 2.0 中新增的选项Registry-Strict 的功能与Registry 相同,但仅使用子项Shell\ExecCGI\CommandExecCGI 键并不常见。它必须在 Windows 注册表中手动配置,从而防止系统上的意外程序调用。 (强调我的)

I use #!/usr/bin/perl in my scripts and configure Apache on Windows to ignore the shebang line. Add

 ScriptInterpreterSource Registry-Strict

to your httpd.conf and set up the Windows Registry key as explained in the Apache docs.

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.

The Apache docs (to which I linked above) state:

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

紧拥背影 2024-08-24 12:29:48

没有便携式 Shebang 线。即使在相同的平台和体系结构上,某人也可能将 perl 安装在不同的位置。

诀窍是不要手动安装模块和脚本。当您将所有内容打包为发行版并使用模块工具链时,shebang 行会自动修改为指向您用于安装所有内容的 perl。您不必考虑这些细节。 :)

There is no portable shebang line. Even on the same platform and architecture, someone might have installed perl is a different location.

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

无尽的现实 2024-08-24 12:29:48

我使用 #!/usr/bin/env perl 作为所有 Perl 的 shebang,无论是在 *nix 还是 Windows 上。 Windows 只是忽略它,而 Unix 则遵循 env 到所选的 Perl 发行版。

I use #!/usr/bin/env perl as the shebang on all of my Perl, whether on *nix or Windows. Windows just ignores it, and Unix follows env to the chosen Perl disto.

柠檬心 2024-08-24 12:29:48

在 win7 及更高版本中,您还可以使用“dos”命令 mklink 来执行此操作。

以管理员身份启动 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

In win7 and up you can also do this with the "dos" command mklink.

Start a cmd shell as administrator and do something like the following:

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
〆凄凉。 2024-08-24 12:29:48
  1. 安装任何 Windows Bash 风格(例如 Cygwin、MSYS2 或 GnuWin32);
  2. 创建一个简单的重定向 shell 脚本:

    <前><代码>执行“@”

  3. 创建注册表项:

    Windows 注册表编辑器版本 5.00
    
    [HKEY_CLASSES_ROOT\.cgi\Shell\ExecCGI\Command]
    @=" <脚本路径>"
    
    [HKEY_CLASSES_ROOT\.pl\Shell\ExecCGI\Command]
    @=" <脚本路径>"
    
    [HKEY_CLASSES_ROOT\.py\Shell\ExecCGI\Command]
    @=" <脚本路径>"
    

    (...等等。)

  4. 在您的 httpd.conf 文件中添加:

    ScriptInterpreterSource 注册表
    

Apache 现在将根据您选择的 Bash 风格给出的解释解析 Unix shebangs。这比在注册表中硬编码解释器路径提供了更多的灵活性。

  1. Install any Windows Bash flavor (such as Cygwin, MSYS2 or GnuWin32);
  2. Create a trivial redirecting shell script:

    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:

    ScriptInterpreterSource Registry
    

Apache will now resolve Unix shebangs relative to the interpretation given by your choosen Bash flavor. This gives much more flexibility than hardcoding interpreter paths in the registry.

夏末的微笑 2024-08-24 12:29:48

我完成这项工作的方法是将 perl.exe 复制到 c:/usr/bin/ 并将其重命名为 perl (去掉 代码>.exe

The way I had this working was to copy perl.exe to c:/usr/bin/ and rename it to perl (strip the .exe)

原来是傀儡 2024-08-24 12:29:48

我手头没有 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};

所以,我猜#! perl 应该可以工作。

编辑:在linux上不起作用;显然在parrot中有效,尽管我不明白他们是如何做到这一点的。

I don't have Windows handy, but perlcritic says:

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.

Edit: doesn't work on linux; apparently works in parrot, although I don't see how they manage that.

夜夜流光相皎洁 2024-08-24 12:29:48

在 Windows 10 中的本地主机上使用 {site-name} 运行基于 Perl 的网站时,如何使用 Linux shebang (#!/usr/bin/perl)?

这对我有用:

  • localhost\{site-name} (C:\xampp\htdocs\{site-name}) 上的 XAMPP
  • 独立安装了 Strawberry Perl (C:\Perl\perl\bin),因为 XAMPP 包中包含的 Perl 不是令人满意

在默认配置中,您必须使用此shebang:

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

是否可以将目录(在-I开关之后)永久包含到@INC?

是的,您可以通过设置 PERLLIB 或 PERL5LIB 环境变量来做到这一点。这在从命令行运行 perl 脚本时有效,但令人惊讶的是它被 XAMPP 中的 apache 服务器忽略。然而,@INC 目录之一 (C:/Perl/perl/site/lib) 通常是空的,因此您可以创建指向网站目录的符号链接:

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

现在,您可以使用这个 shebang:

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

此外,您可以创建目录 c: \usr\bin

md c:\usr\bin

并从 C:\perl\perl\bin\ 复制 perl.exe

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

(由于某些原因,另一个 mklink 技巧在这里不起作用。)

最后,您可以在 Windows 上使用 Unix 风格的 shebang:

#!/usr/bin/perl

How to use Linux shebang (#!/usr/bin/perl) when running Perl based web-site with {site-name} on localhost in Windows 10?

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

In default configuration you must use this shebang:

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

Is it possible to include directory (after -I switch) permanently to @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:

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

Now, you can use this shebang:

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

Moreover, you can create directory c:\usr\bin

md c:\usr\bin

and copy perl.exe from C:\perl\perl\bin\

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

(Another mklink trick is not working here from some reasons.)

Finally, you can use Unix-styled shebang on Windows:

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