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

Howdy,

I am creating a build script to install cgi files upon installation on my application. The problem I am having is that Apache insists that the shebang be included whether I am using Windows or Unix.

I can put the shebang at the top of my cgi files during installation but I need someway of finding the path to the perl interpreter on a Windows or a *NIX system.

Suggestions?

Replies are listed 'Best First'.
Re: Where is the interpreter
by ikegami (Patriarch) on Jan 08, 2007 at 03:08 UTC

    The answer is in $^X.

    >perl -le "print $^X" c:\Progs\perl588\bin\perl.exe
      That was going to be my original answer, until I continued reading that section in perlvar and saw this text:
      Because many operating systems permit anyone with read access to the Perl program file to make a copy of it, patch the copy, and then execute the copy, the security-conscious Perl programmer should take care to invoke the installed copy of perl, not the copy referenced by $^X.

      ---
      It's all fine and dandy until someone has to look at the code.

        The warning makes no sense in that situation. It tells you to use a copy of Perl that's known to be safe instead of relying on $^X, while the OP's problem is that he doesn't know where his Perl is located.

        The warning applies to the method used in your solution too. In fact, it applies to all answers to the OP's question other than those that match the pattern "I talked to your service provider and they told me to use the Perl located in XXX."

        Furthermore, if the OP made a copy, he probably wants to use that copy (especially if he patched it).

        The usual case for using $^X is to spawn a subprocess. The warning applies to launching child processes from setuid scripts (or code that might find its way into one).

Re: Where is the interpreter
by shigetsu (Hermit) on Jan 08, 2007 at 00:36 UTC

    This functionality has been portably implemented by Per Einar Ellefsen.

    See File::Which for the details.

    Example code for Unix:
    #!/usr/bin/perl use strict; use warnings; use File::Which qw(which where); my $path = which('perl'); my @paths = where('perl');
Re: Where is the interpreter
by Joost (Canon) on Jan 08, 2007 at 00:17 UTC
      Well, the Apache installation for Windows is a bit different. It is not looking at file associations. If you screw up the shebang even if it is Windows, it will not run correctly.
Re: Where is the interpreter
by kwaping (Priest) on Jan 08, 2007 at 01:22 UTC
    How about this (adapted from perlvar):
    use Config; my $perl_with_path = $Config{perlpath};

    ---
    It's all fine and dandy until someone has to look at the code.
Re: Where is the interpreter
by NetWallah (Canon) on Jan 08, 2007 at 00:28 UTC
    Assuming your install script is in perl, the following should work on both systems (Tested OK on Win32):
    my $p; for (split /,|;/, $ENV{PATH}){ m/\Wperl\W/i and $p=$_ }; print qq[===$p==\n]; # $p contains the path to perl
    Admittedly, if you have perl-like paths in the PATH, this code would be confused - but then you could get fancy, and try to verify existance of perl, and it's execute-ability.

    Update:Ignore this drivel. Use ikegami's (++) correct answer below.

         "A closed mouth gathers no feet." --Unknown

Re: Where is the interpreter
by randyk (Parson) on Jan 08, 2007 at 04:06 UTC
    As an alternative on Windows, you can also use the Apache ScriptInterpreterSource directive - this searches the Registry by script file extension to discover which command to use.
Re: Where is the interpreter
by bart (Canon) on Jan 08, 2007 at 11:09 UTC
    On Windows, you can just use
    #! perl

    As long as perl is found in the path, it'll work. I'm not convinced it even needs to be in the path. An incorrect path will not work, Apache won't like it.

Re: Where is the interpreter
by WiseGuru (Initiate) on Jan 08, 2007 at 00:19 UTC
    not sure of windows, but do a search of files for the perl folder, and it usualy looks like #!/(full path to folder/perl the folder name might vary, but in general there is a way to find it, you gotta do a google search or someone else here give you the windows command to find it! there will be several folders, like perl, perl5.8.. perl4.... here is where i believ the she-bang points in general, point it to the main folder, as pointing it to your latest install will be useless when you upgrade, and you have to change all your scripts to accomodate!

    www.arobcorp.com

      I am writing an automatic installation, not a manual installation.

      I would like a programmatic solution because I will be giving the installation to people who may or may not have the expertise to figure out where the interpreter is located and the application maybe in Windows or UNIX so it has to be flexible.

        in that case your writing a ".exe" right?, then you gotta get the ".exe" to search the file system for the appropriate file, then include it in the installed file, simular to when some programs instal, they go off and look for the nessary system files they need (i.e. fonts,sound,drives etc) then add them to the pre-install file, then once all found and no errors, instal the pre-install file! so, you gotta find the general folder, then the ".exe" has to include it in the instal!
Re: Where is the interpreter
by ikegami (Patriarch) on Jan 08, 2007 at 17:05 UTC
    If you're still having the problem, why don't you ask your web hosting service (if the box is remote) or whoever installed Perl for you (if the box is local).
Re: Where is the interpreter
by MaxKlokan (Monk) on Jan 08, 2007 at 09:45 UTC
    For UNIX you can use the following shebang (from perlintro), and it should work regardless of where the interpreter is, provided that it is in the path:
    #!/usr/bin/env perl
    As far as I know env should be always in the same place.

      As far as I know env should be always in the same place.

      I heard that it's not in the same place on all systems. It definitely does not exist there in Windows (which is the important part of the question, since perl is probably in /usr/bin on the unix box).