Introduction
Batch basics:
·
Its extension should be ".bat" or ".cmd" (i.e: "showdate.bat").
·
A comment in a batch is allowed using the "rem" command.
·
To avoid printing out batch execution on
terminal, the "@" char should precede
the Batch command (i.e: "@rem ..." or "@perl ...").
·
To jump from one line to another, the command
"goto" needs a label as parameter (i.e: "goto end"). The label should be defined with ":" character before it (i.e: ":end").
Perl basics:
·
A Perl script line beginning with "@", means that the token afterwards is an array
(i.e: "@array = (1, 2, 3);").
·
Comments under Perl begin with a "#" character.
·
A string with simple quote "'" can contain new lines characters.
Facts:
·
Batch and Perl scripts share only the "@" character.
The ActiveState solution
In directory "<perl
path>\bin", you will find more than 50
batch files starting Perl Script.
Basically, the idea is to use the following
structure:
@rem = '
BATCH command to call Perl
perl -x -S %0 %*
goto endofperl
@rem ';
Perl script
__END__
:endofperl
ev. other BATCH commands
In this case, the Batch part is at the
beginning (between "@rem = '" and
"goto
endofperl", including these lines) and at
the end of the file.
Problems:
·
it can't contain the "'" character, otherwise Perl will not be able to understand the
command "@rem = '… ' … ';"
·
The error analysis should be done in the Batch
part itself, as the "goto endofperl"
erases the Perl script return-code…
Probably the best solution
Basically, the idea is to use the following
structure:
@goto ;#DOS
Perl script
__END__
:;#DOS
BATCH command to call Perl
perl -S %0 %*
The only common part used by Perl and Batch
is the line "@goto ;#DOS" which
means:
·
for Perl: "Do nothing with @goto" (which produces a warning if switch "-w" has been used) followed by the comment "DOS".
·
for Batch: "jump to label ;#DOS"
This way, both script are completely separated.
Beside it, as the Perl call is the last batch command used, any result
(return-code) returned by the Perl Script is automatically returned by the
batch itself.
Problems:
·
The Perl script generates two warnings
·
"Useless use of a variable in void context
at" and
·
"Name "main::goto" used only once"
·
The Perl call as described "perl -S %0 %*" is not supported under Windows 95/98/ME
Other solutions:
·
To avoid both warnings try following:
@goto =
"GotoBATCH" ;
@goto = ();
Perl script
__END__
:"GotoBATCH"
BATCH command to call Perl
perl -S %0 %*
·
To be compatible both for Windows NT (NT, 2000,
2003, 2008, XP, Vista) and Windows 95/98/ME, use:
perl -x -S
"%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
Tests solution
I was very sceptic with the Perl's "-S" switch. I thought we needed to specify the extension and it
will not work with full path script. Therefore I tried following script (stored
under "c:\apps\bat\testperl.bat", "c:\apps\bat" being set in the Path):
@goto ;#DOS
print("This is Perl speaking: $0 @ARGV\n");
__END__
:;#DOS
@echo This is DOS
speaking: %0 %*
@perl -S %0 %*
And tried following calls:
·
Calling "c:\apps\bat\testperl.bat param1 param2 param3" returns:
This is DOS speaking:
c:\apps\bat\testperl.bat param1 param2 param3
This is Perl
speaking: c:\apps\bat\testperl.bat param1 param2 param3
·
Calling "testperl.bat param1 param2 param3" returns:
This is DOS speaking:
testperl.bat param1 param2 param3
This is Perl
speaking: c:\apps\bat/testperl.bat param1 param2 param3
·
Calling "testperl param1 param2 param3" returns:
This is DOS speaking:
testperl param1 param2 param3
This is Perl
speaking: c:\apps\bat/testperl.bat param1 param2 param3
·
Renaming "testperl.bat"
by "test
perl.bat" and calling
" "test
perl" param1 param2 param3" returns:
This is DOS speaking:
"test perl" param1 param2 param3
This is Perl
speaking: c:\apps\bat/test perl.bat param1 param2 param3
·
Even "TESTPE~1.BAT param1 param2 param3" returns:
This is DOS speaking:
TESTPE~1.BAT param1 param2 param3
This is Perl
speaking: c:\apps\bat/TESTPE~1.BAT param1 param2 param3
Now let's check some error cases:
·
When Perl works fine,"echo %errorlevel%" returns then:
·
Perl is not in the Path or not installed:
·
change "@perl -S %0 %*" with "@XXXperl ‑S %0 %*"
·
This returns:
This is DOS speaking:
testperl param1 param2 param3
Der Befehl "XXXperl" ist
entweder falsch geschrieben oder
konnte nicht gefunden
werden.
·
"echo %errorlevel%" returns then:
·
When Perl script has troubles:
·
change back "@XXXperl ‑S %0 %*" with "@perl -S %0 %*"
·
insert a new line "print 1/0;" before line "__END__":
·
This returns:
This is DOS speaking:
testperl param1 param2 param3
This is Perl
speaking: c:\apps\bat/testperl.bat param1 param2 param3
Illegal division by
zero at c:\apps\bat/testperl.bat line 3.
·
"echo %errorlevel%" returns then:
·
When Perl script returns an exit code:
·
replace "print 1/0;"
with "exit 5;":
·
This returns:
This is DOS speaking:
testperl param1 param2 param3
This is Perl
speaking: c:\apps\bat/testperl.bat param1 param2 param3
·
"echo %errorlevel%" returns then: