[tpm] Is there a way to choose from several interpreters, using the hashBang?

Indy Singh indy at indigostar.com
Fri Jun 24 12:25:00 PDT 2011

Windows version of Perl come with a script called pl2bat, which converts a perl script to a batch file.  This is done by wrapping the perl script with some line of batch code.  An example for a single line ‘Hello world’ script is later in the email.  The batch file initially runs as a batch file, then calls the Perl interpreter using the same batch file as the input perl script.  The command to do that is: “perl –x –S %0”.  
This looks for the Perl interpreter using the PATH environment variable.  

You could change the wrapper batch file to look for Perl in multiple places.

Windows does not use the #! line to locate perl, except in the special case of running the script under Apache as a CGI script.

Instead of pulling in the modules with ‘use’ statements, you can wrap the use statements with an eval expression.  Something like this (untested) code:
eval (“use Foo:: Bar;”);
if ($@) {
    print error message and exit.

@rem = '--*-Perl-*--
@echo off
if "%OS%" == "Windows_NT" goto WinNT
perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofperl
perl -x -S %0 %*
if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
if %errorlevel% == 9009 echo You do not have Perl in your PATH.
if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
goto endofperl
@rem ';
#line 15
print "hello world\n";


Indy Singh
IndigoSTAR Software -- www.indigostar.com

From: J Z Tam 
Sent: Friday, June 24, 2011 2:32 PM
To: Toronto Perl Mongers ; Mike Stok 
Subject: [tpm] Is there a way to choose from several interpreters,using the hashBang?

      CAVEAT:  Windoze XP and Server2003/5  here. Ugh.  I'm trying to figure out an elegant way to have multiple install trees able to be sourced, either just before a perl program is executed, or at the time the hashBang line is encountered. 

      I thought there was a nifty cool way to select the particular   perl interpreter just before the #! line, or actually AT the  #!  line.
      1. Q: what if the caller's %PATH% environment variable does NOT contain a valid pathToInterpreter
      2. Q:  What if the the install tree that is eventually found, is missing a use'd or require'd  package, or the wrong version of the package?


toronto-pm mailing list
toronto-pm at pm.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20110624/b538584d/attachment.html>

More information about the toronto-pm mailing list