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

二次信任 提交于 2019-11-27 01:52:49

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)

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

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.

The way I had this working was to copy perl.exe to c:/usr/bin/ and rename it to perl (strip the .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

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.

  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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!