1#!perl -w 2$0 =~ s|\.bat||i; 3unless (-f $0) { 4 $0 =~ s|.*[/\\]||; 5 for (".", split ';', $ENV{PATH}) { 6 $_ = "." if $_ eq ""; 7 $0 = "$_/$0" , goto doit if -f "$_/$0"; 8 } 9 die "`$0' not found.\n"; 10} 11doit: exec "perl", "-x", $0, @ARGV; 12die "Failed to exec `$0': $!"; 13__END__ 14 15=head1 NAME 16 17runperl.bat - "universal" batch file to run perl scripts 18 19=head1 SYNOPSIS 20 21 C:\> copy runperl.bat foo.bat 22 C:\> foo 23 [..runs the perl script `foo'..] 24 25 C:\> foo.bat 26 [..runs the perl script `foo'..] 27 28 29=head1 DESCRIPTION 30 31This file can be copied to any file name ending in the ".bat" suffix. 32When executed on a DOS-like operating system, it will invoke the perl 33script of the same name, but without the ".bat" suffix. It will 34look for the script in the same directory as itself, and then in 35the current directory, and then search the directories in your PATH. 36 37It relies on the C<exec()> operator, so you will need to make sure 38that works in your perl. 39 40This method of invoking perl scripts has some advantages over 41batch-file wrappers like C<pl2bat.bat>: it avoids duplication 42of all the code; it ensures C<$0> contains the same name as the 43executing file, without any egregious ".bat" suffix; it allows 44you to separate your perl scripts from the wrapper used to 45run them; since the wrapper is generic, you can use symbolic 46links to simply link to C<runperl.bat>, if you are serving your 47files on a filesystem that supports that. 48 49On the other hand, if the batch file is invoked with the ".bat" 50suffix, it does an extra C<exec()>. This may be a performance 51issue. You can avoid this by running it without specifying 52the ".bat" suffix. 53 54Perl is invoked with the -x flag, so the script must contain 55a C<#!perl> line. Any flags found on that line will be honored. 56 57=head1 BUGS 58 59Perl is invoked with the -S flag, so it will search the PATH to find 60the script. This may have undesirable effects. 61 62=head1 SEE ALSO 63 64perl, perlwin32, pl2bat.bat 65 66=cut 67 68