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

Hi, I want to know how I can use conditional statement in the beginning of a script to use specific Perl. For example, usually, my perl scripts start with the line: - #!/opt/OV/bin/perl However, I want to give a Window's path as well: - It has to use either /opt/OV/bin/perl or C:\usr\ov\bin\perl Thanks, Karthik

Replies are listed 'Best First'.
Re: path to perl install directory
by Marshall (Canon) on Dec 14, 2010 at 07:16 UTC
    Under Windows, the path to Perl that we typically find as the first line is irrelevant. Windows will use the program that you have associated with say the .pl extension. So this is a lot messier than with Unix!

    To set this up in Windows there are 2 steps:

    1)Setup the association between the .pl extension and an actual program. Search for "associate extension" in Windows Help for the details. Now you will be able to "click on" a Perl program icon to run it.

    To view associations from the command line, use the assoc Windows command (or of course use the GUI method above):

    C:\Projects>assoc | grep -i perl .perlnet=PerlNET.Project .pl=Perl .pls=PerlScriptFile
    2)Next, you need to tell Windows shell that files ending in .pl are executable files. This is needed so that you can type in the name of a Perl file (something.pl) from the command line. This is done via the PATHEXT environment variable. Search for "environment" in Windows Help and look at "Add or change the values of environment variables".
    When you get done PATHEXT will look like this:
    C:\Projects>env | grep EXT PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PSC1;.pl
    To use a different version of Perl to run, at the command line you must type the name of the Perl.exe that you mean, perhaps like: >perlv58 xxx.pl or >perlv510 xxx.pl. I suppose that if you are going to do this a lot, you could set up a .pl58 and a .pl510 file extension for different versions. But, no under Windows, the .pl file itself will not know which Perl interpreter to run.

    Oh, I guess you could also make a .bat file that pre-pends the PATH with the path name of the version of Perl that you want to use, that way it will be found first when it sees a .pl file. Also, on Windows you can make different shortcuts to the command line shell with different environment variables. So if you want to work with say Perl 5.10 instead of Perl5.8, you just start a different command line environment.

    This is one other bit that you should know...Although the path to Perl is not used under Windows, the flags after it are used. For example: #!/usr/bin/perl -w The -w flag turns on warnings.

    So if you are not using multiple versions of Perl under Windows, just associate the .pl extension as shown above. The Windows Perl will ignore the path after #! that you are using under Unix, but will use the switches (like -w or whatever).

    This was wordy, but I hope clear enough to understand.

      Hi, this is actual documentation, I just bolded a few items

        Good info! From reading the OP's question a couple of times, I suspect the OP has the misunderstanding that a different #!path_to_perl is required under Windows. It is not.

        If this is the case, then I recommend using the GUI as outlined in my post to "set the Windows stuff once and forget it".

        As an additional note: "hey, grep isn't a Windows command!"...just install a gnu version. I use this one: GNU Grep 2.5.4 for Windows. There is a single step installer that works fine.

        awesome
      That was really informative and useful! Thanks a lot!
Re: path to perl install directory
by samarzone (Pilgrim) on Dec 14, 2010 at 06:42 UTC

    One of your path is a linux file-system path while another is windows one. Where are you running this script?

    (Assuming both are windows path) In general this is not possible (as per my understanding) but if you are concerned with a specific script you can write a batch file to choose and execute the correct Perl. Windows equivalent of following can do what I mean

    if [ -e "c:\path\to\perl1" ] then c:\path\to\perl1\bin\perl myscript.pl else c:\path\to\perl2\bin\perl myscript.pl fi
      Thanks samarzone!
Re: path to perl install directory
by Anonymous Monk on Dec 14, 2010 at 06:16 UTC
    You don't need to do that, windows does not have a hashbang