1 eval 'exec perl -x -S "$0" ${1+"$@"}'
2 if 0; # In case running under some shell
12 or: $0 [-w] [-u] [-a argstring] [-s stripsuffix] [files]
13 or: $0 [-w] [-u] [-n ntargs] [-o otherargs] [-s stripsuffix] [files]
14 -n ntargs arguments to invoke perl with in generated file
15 when run from Windows NT. Defaults to
17 -o otherargs arguments to invoke perl with in generated file
18 other than when run from Windows NT. Defaults
19 to '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'.
20 -a argstring arguments to invoke perl with in generated file
21 ignoring operating system (for compatibility
22 with previous pl2bat versions).
23 -u update files that may have already been processed
24 by (some version of) pl2bat.
25 -w include "-w" on the /^#!.*perl/ line (unless
26 a /^#!.*perl/ line was already present).
27 -s stripsuffix strip this suffix from file before appending ".bat"
29 Can be a regex if it begins with `/'
30 Defaults to "/\.plx?/"
35 warn($usage), exit(0) if !getopts('whun:o:a:s:',\%OPT) or $OPT{'h'};
36 $OPT{'n'} = '-x -S "%0" %*' unless exists $OPT{'n'};
37 $OPT{'o'} = '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9' unless exists $OPT{'o'};
38 $OPT{'s'} = '/\\.plx?/' unless exists $OPT{'s'};
39 $OPT{'s'} = ($OPT{'s'} =~ m|^/([^/]*)| ? $1 : "\Q$OPT{'s'}\E");
42 if( defined( $OPT{'a'} ) ) {
54 if "%OS%" == "Windows_NT" goto WinNT
59 if NOT "%COMSPEC%" == "%SystemRoot%\\system32\\cmd.exe" goto endofperl
60 if %errorlevel% == 9009 echo You do not have Perl in your PATH.
66 my $headlines = 2 + ($head =~ tr/\n/\n/);
67 my $tail = "\n__END__\n:endofperl\n";
69 @ARGV = ('-') unless @ARGV;
83 my $start= $Config{startperl};
84 $start= "#!perl" unless $start =~ /^#!.*perl/;
85 open( FILE, $file ) or die "$0: Can't open $file: $!";
87 foreach $line ( @file ) {
89 if ( $line =~ /^:endofperl\b/ ) {
90 if( ! exists $OPT{'u'} ) {
91 warn "$0: $file has already been converted to a batch file!\n";
96 if ( not $linedone and $line =~ /^#!.*perl/ ) {
97 if( exists $OPT{'u'} ) {
98 $skiplines = $linenum - 1;
99 $line .= "#line ".(1+$headlines)."\n";
101 $line .= "#line ".($linenum+$headlines)."\n";
105 if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
110 $file =~ s/$OPT{'s'}$//oi;
111 $file .= '.bat' unless $file =~ /\.bat$/i or $file =~ /^-$/;
112 open( FILE, ">$file" ) or die "Can't open $file: $!";
114 print FILE $start, ( $OPT{'w'} ? " -w" : "" ),
115 "\n#line ", ($headlines+1), "\n" unless $linedone;
116 print FILE @file[$skiplines..$#file];
117 print FILE $tail unless $taildone;
124 pl2bat - wrap perl code into a batch file
130 B<pl2bat> [B<-w>] S<[B<-a> I<argstring>]> S<[B<-s> I<stripsuffix>]> [files]
132 B<pl2bat> [B<-w>] S<[B<-n> I<ntargs>]> S<[B<-o> I<otherargs>]> S<[B<-s> I<stripsuffix>]> [files]
136 This utility converts a perl script into a batch file that can be
137 executed on DOS-like operating systems.
139 Note that by default, the ".pl" suffix will be stripped before adding
140 a ".bat" suffix to the supplied file names. This can be controlled
141 with the C<-s> option.
143 The default behavior is to have the batch file compare the C<OS>
144 environment variable against C<"Windows_NT">. If they match, it
145 uses the C<%*> construct to refer to all the command line arguments
146 that were given to it, so you'll need to make sure that works on your
147 variant of the command shell. It is known to work in the cmd.exe shell
148 under WindowsNT. 4DOS/NT users will want to put a C<ParameterChar = *>
149 line in their initialization file, or execute C<setdos /p*> in
150 the shell startup file.
152 On Windows95 and other platforms a nine-argument limit is imposed
153 on command-line arguments given to the generated batch file, since
154 they may not support C<%*> in batch files.
156 These can be overridden using the C<-n> and C<-o> options or the
157 deprecated C<-a> option.
163 =item B<-n> I<ntargs>
165 Arguments to invoke perl with in generated batch file when run from
166 Windows NT (or Windows 98, probably). Defaults to S<'-x -S "%0" %*'>.
168 =item B<-o> I<otherargs>
170 Arguments to invoke perl with in generated batch file except when
171 run from Windows NT (ie. when run from DOS, Windows 3.1, or Windows 95).
172 Defaults to S<'-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'>.
174 =item B<-a> I<argstring>
176 Arguments to invoke perl with in generated batch file. Specifying
177 B<-a> prevents the batch file from checking the C<OS> environment
178 variable to determine which operating system it is being run from.
180 =item B<-s> I<stripsuffix>
182 Strip a suffix string from file name before appending a ".bat"
183 suffix. The suffix is not case-sensitive. It can be a regex if
184 it begins with `/' (the trailing '/' is optional and a trailing
185 C<$> is always assumed). Defaults to C</.plx?/>.
189 If no line matching C</^#!.*perl/> is found in the script, then such
190 a line is inserted just after the new preamble. The exact line
191 depends on C<$Config{startperl}> [see L<Config>]. With the B<-w>
192 option, C<" -w"> is added after the value of C<$Config{startperl}>.
193 If a line matching C</^#!.*perl/> already exists in the script,
194 then it is not changed and the B<-w> option is ignored.
198 If the script appears to have already been processed by B<pl2bat>,
199 then the script is skipped and not processed unless B<-u> was
200 specified. If B<-u> is specified, the existing preamble is replaced.
204 Show command line usage.
210 C:\> pl2bat foo.pl bar.PM
211 [..creates foo.bat, bar.PM.bat..]
213 C:\> pl2bat -s "/\.pl|\.pm/" foo.pl bar.PM
214 [..creates foo.bat, bar.bat..]
216 C:\> pl2bat < somefile > another.bat
218 C:\> pl2bat > another.bat
219 print scalar reverse "rekcah lrep rehtona tsuj\n";
221 [..another.bat is now a certified japh application..]
225 [..updates the wrapping of some previously wrapped scripts..]
227 C:\> pl2bat -u -s .bat *.bat
228 [..same as previous example except more dangerous..]
232 C<$0> will contain the full name, including the ".bat" suffix
233 when the generated batch file runs. If you don't like this,
234 see runperl.bat for an alternative way to invoke perl scripts.
236 Default behavior is to invoke Perl with the B<-S> flag, so Perl will
237 search the PATH to find the script. This may have undesirable
242 perl, perlwin32, runperl.bat