bart has asked for the wisdom of the Perl Monks concerning the following question:

In general, Windows or Perl don't care about what path you use in the shebang (= "#!") line in Perl scripts on Windows, it'll work regardless. One notable exception is in CGI scripts under Apache: Apache does check if the executable exists. That is a pain as I want to run CGI scripts unaltered both on the live Linux server, and for testing on my Windows XP PC.

Apache typically has two modes of operation:

I do not want to have to change 20 scripts to incorporate this one change, especially if it's the only thing that needs changing to make it run on both. It'd just mess up my version checking system, and I'd prefer to just have to use FTP between the two. File extension ".cgi" is associated with my text editor, and I'd like to keep it that way. So, what can I do?

Somebody mentioned on the Chatterbox that I might be using an extra SetHandler in Apache's configuration file, bypassing Windows' file association, but that's all he said about it. Anybody know if/how that would work? Or of any other alternatives?

  • Comment on shebang line for Perl scripts under Apache/Windows

Replies are listed 'Best First'.
Re: shebang line for Perl scripts under Apache/Windows
by mpeters (Chaplain) on Jan 12, 2005 at 14:32 UTC
    "Somebody mentioned on the Chatterbox that I might be using an extra SetHandler in Apache's configuration file, bypassing Windows' file association, but that's all he said about it. Anybody know if/how that would work? Or of any other alternatives?"

    Not SetHandler (unless you want to do it for specific location/directory) but AddHandler, should do the trick. Try something like this in your apache config file:
    AddHandler cgi-script .cgi
    This directive is only available with mod_mime (which is installed by default) and can work globally, or inside of a Location/Directory. You can read this for more information.
      Unless I'm badly mistaken, that won't solve the stated problem, as the handler cgi-script uses checkes the file's shebang to determine which script to execute. Which is the whole problem =]
Re: shebang line for Perl scripts under Apache/Windows
by sasikumar (Monk) on Jan 12, 2005 at 11:33 UTC

    you can create same directory structure in both windows and linux and install perl there. In linux create the directory with the drive name so it would find out.
    Update: In case of windows just create a directory with /usr/bin/ so it would resolve the problem.

    Thanks
    SasiKumar
      Yes erix pointed to one of his replies in an earlier tread which describes such a solution, in the Chatterbox. I guess it works... though it's an ugly hack, and I'm not sure what drive the directory has to be on, in order for it to work reliably. Apache's? The website's?
        It works fine and makes switching machines really simple, at least for me. The drive perl has to be on is which ever drive apache's CWD is on. So if apache's cwd is "C:/foo/bar/", "/usr/bin" will refer to "C:/usr/bin". But if apache starts under D: or some such, it needs to be there. Also note, last I checked, you don't need the entire perl install in this new directory, you can simply add a shortcut named perl, and that should work.
Re: shebang line for Perl scripts under Apache/Windows
by doink123 (Sexton) on Jan 12, 2005 at 11:21 UTC
    If the perl executable is in the PATH environment variable on both operating systems (most probably it is if you are using ActivePerl on windows) then you can only use
    #!perl
    it should run both on windows and linux

      Are you sure? The execve function that is used to execute the script under Linux won't search the $PATH for an interpreter. On my system with an unqualified '#!perl' I get:

      perl: bad interpreter: No such file or directory
      Of course your experience may be different.

      /J\

      It checks the shebang line, ignoring the file association of the file extension. If you use a full path, it must exist — though something as generic as just "perl" works too.

      he said that.

      "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

Re: shebang line for Perl scripts under Apache/Windows
by dragonchild (Archbishop) on Jan 12, 2005 at 15:02 UTC
    Instead of using Apache for Windows, why not use Cygwin and compile Apache under it? Granted, it is 30%-50% slower than on Linux, but for personal testing, that shouldn't be an issue ...

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: shebang line for Perl scripts under Apache/Windows
by strat (Canon) on Jan 13, 2005 at 08:04 UTC

    if apache and the website(s) (are on the same drive, and if you really need to keep the assoctiation of .cgi to your text editor, you could install perl to DRIVE:\usr (but only install perl into that dir, don't add other software...), so that perl can be reached by DRIVE:\usr\bin\perl.exe, and then the shebang /usr/bin/perl should work... (haven't tested it)

    But I prefer the way associating .cgi with perl.exe and ScriptInterpreterSource Registry in httpd.conf

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: shebang line for Perl scripts under Apache/Windows
by jZed (Prior) on Jan 12, 2005 at 19:30 UTC
    I think what you need is this Apache directive:

    ScriptInterpreterSource registry

    That will cause apache to ignore the location of the perl executable mentioned in the shebang line and use the location of the perl executable known to the registry via file associations for .cgi, .pl, etc.
      Like I said: that doesn't work, because *.cgi scripts are associated with my text editor.
Re: shebang line for Perl scripts under Apache/Windows
by cosmicperl (Chaplain) on Nov 24, 2014 at 10:19 UTC
    Hi,
      What you are looking for is possible by updating your registry to have an ExecCGI key: Apache 2 ScriptInterpreterSource documentation
    That way you can still have your text editor association when you double click in Windows explorer and alike, but have apache read from the registry and ignore the shebang line.
      On windows install Perl in c:/usr/bin/ or whatever path your remote machine has