- Turning Perl Scripts into Commands
- The UNIX/Linux Magic String
- Using the env Program
- The Shell Script Method
- Microsoft Windows .bat File
- Resources
Microsoft Windows .bat File
To turn a script into a program under Microsoft Windows, use the PL2BAT command. For example:
C:\SCRIPTS> pl2bat script.pl
The result is a script.bat file containing your Perl program. The PL2BAT command adds a few commands to the beginning of your script that run Perl and tell it to use the body of the batch file as the Perl script.
Listing B.1 contains the result of running PL2BAT on a simple Perl program.
Listing B.1hello.bat
@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 :WinNT 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 '; #!perl #line 15 use strict; use warnings; print "Hello world\n"; __END__ :endofperl
The first line of the program is a comment (rem in Microsoft Windows terms.) The next line turns off echo.
Microsoft Windows NT and the other Microsoft operating systems have slightly different batch languages. For most operating systems, the system will execute the command:
perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
This command runs the perl program with the argument x which tell Perl to look through the file until it sees the line:
#!perl
Next comes the S switch which tell Perl to search along the PATH variable for the script. This is followed by the name of the script ("%0") and up to 9 additional arguments (%1 through %9).
This is followed by a goto that causes the Windows command process to skip to the end of the text.
goto endofperl
On Windows-NT, the command executed is:
perl -x -S %0 %*
The x and S flag are exactly the same as before. The other arguments pass in the name of the script (%0) and the arguments (%*).
This is followed by some error checking:
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
Next comes goto that gets you out of the program:
goto endofperl