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

I am trying to understand the functioning of batch files such as ptked.bat under perl for windows. The first part is usually like:
@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 -w #line 15 ... perl code here ...
I did read runperl document, but still need more details as to how the above code works. Would appreciate any pointers in this regard. Thanks very much in advance.

Replies are listed 'Best First'.
Re: Batch files in Win/Perl
by PodMaster (Abbot) on Nov 21, 2004 at 06:25 UTC
    Why? nevermind

    When you read runperl, it says at the end SEE ALSO perl, perlwin32, pl2bat.bat, and pl2bat does indeed have more details.

    update: struck out the Why?, made an extra link

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Batch files in Win/Perl
by theorbtwo (Prior) on Nov 21, 2004 at 17:13 UTC

    This question doesn't actually have that much to do with perl. From perl's point of view, the code consists of an assignment to the array @rem with a single element, being a long string from line 1 through 12. After that comes the body of the original perl script.

    From DOS's point of view, though, it's a perfectly valid batch file -- though my batch is a bit (OK, a lot) rusty.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: Batch files in Win/Perl
by ikegami (Patriarch) on Nov 21, 2004 at 19:27 UTC

    Notice how STDIN redirection doesn't work below.

    >copy con script.pl while (<STDIN>) { s/[aeiou]/!/ig; print; } ^Z >script.pl < script.pl >perl script.pl < script.pl wh!l! (<STD!N>) { s/[!!!!!]/!/!g; pr!nt; } >ver Microsoft Windows XP [Version 5.1.2600]

    It works fine with batch files, however. That's where pl2bat comes in. It makes it so you don't have to explicitly mention "perl" to execute the script, without suffering from the above bug.