11

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.

Community
  • 1
  • 1
Nick Bolton
  • 34,516
  • 66
  • 162
  • 230
  • 1
    A lot of your questions suffer from the XY problem (http://www.perlmonks.org/index.pl?node_id=542341). You ask about what you think the solution is instead of asking about the problem. In this case, I don't think you actually need a cross-platform shebang line, You just need one that works for ActivePerl on Windows 7 using Apache 2. – brian d foy Jan 10 '10 at 11:49
  • 1
    @brian Awesome! Thanks for the tip. I've always known that I do this and herein lies the problem. I will try and ask about the problem in future answers instead of already making my mind up on the solution. – Nick Bolton Jan 10 '10 at 13:53
  • Well, now that you've changed the question, it's much easier: you don't put a shebang line in the script. There's no requirement for it to be there unless you want the system to figure out which interpreter to call. That doesn't matter in your case. – brian d foy Jan 10 '10 at 23:19
  • @brian Question updated. – Nick Bolton Jan 10 '10 at 23:41

8 Answers8

12

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)

Sinan Ünür
  • 113,391
  • 15
  • 187
  • 326
  • As advised by the docs, I've tried this in all contexts: `server config, virtual host, directory, .htaccess` - but this does not seem to solve the error, what could I be doing wrong? – Nick Bolton Jan 10 '10 at 09:28
  • Yes I have restarted. I'm using ActivePerl so I don't know, what registry key do I need to look for? I am using the directive in `httpd.conf` just below `DocumentRoot` (default config line 177). – Nick Bolton Jan 10 '10 at 09:36
  • Thanks, I added the key `HKEY_CLASSES_ROOT\.pl\Shell\ExecCGI\Command` with REG_SZ value `C:\Perl\bin\perl.exe -wT`, and it works! – Nick Bolton Jan 10 '10 at 09:50
4

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

brian d foy
  • 121,466
  • 31
  • 192
  • 551
  • I am unable to use this solution because `.pl` files do not execute on our shared Linux hosting server without the shebang. Unfortunately I'm not in a position to change the server config; I would have added an Apache handler for Perl if this was the case. Also, there are many existing `.pl` scripts on the site that use the shebang - if I were more confident with Perl I'd probably change this, but I don't want to step on anyone's toes so to speak. – Nick Bolton Jan 11 '10 at 17:24
  • Well, you don't have to have the same shebang line on both installations. A proper installation will fix that up for you. – brian d foy Jan 11 '10 at 19:27
3

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.

gWaldo
  • 432
  • 1
  • 7
  • 23
1

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

Htbaa
  • 2,319
  • 18
  • 26
1

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
John Vinopal
  • 541
  • 4
  • 11
0

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.

Pedro Silva
  • 4,522
  • 1
  • 16
  • 30
0
  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.

alecov
  • 4,313
  • 2
  • 25
  • 51
0

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
Dharman
  • 21,838
  • 18
  • 57
  • 107
natT
  • 1
  • 1