Perl - Perl Script within a DOS Batch

Perl script and batch in a single file.


It`s nice not to have to type "perl -s" into the command line and rather being able to just double click a Perl script in Explorer. The trick of renaming the Perl .pl to a batch .bat file and wrapping a batch script around the Perl script is well known under Perl monks. However the solution I have seen so far needed batch code before and after the Perl script where as the solution presented below only needs some lines of DOS at the top. The added DOS script is generic and works independent from the name of the file.

When running the batch the DOS command interpreter will read the first lines and execute the file itself in Perl context.

Script: Download: Batch4Perl.bat  
@rem = 'source
@CD/d"%~dp0"&perl -s "%~nx0" %*&Exit/b&:';

#perl script starts below here
print 'Hi there!  DOS rocks!\n'