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 = "__END__\n:endofperl\n";
69 @ARGV = ('-') unless @ARGV;
83 open( FILE, $file ) or die "$0: Can't open $file: $!";
85 foreach $line ( @file ) {
87 if ( $line =~ /^:endofperl\b/ ) {
88 if( ! exists $OPT{'u'} ) {
89 warn "$0: $file has already been converted to a batch file!\n";
94 if ( not $linedone and $line =~ /^#!.*perl/ ) {
95 if( exists $OPT{'u'} ) {
96 $skiplines = $linenum - 1;
97 $line .= "#line ".(1+$headlines)."\n";
99 $line .= "#line ".($linenum+$headlines)."\n";
103 if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
108 $file =~ s/$OPT{'s'}$//oi;
109 $file .= '.bat' unless $file =~ /\.bat$/i or $file =~ /^-$/;
110 open( FILE, ">$file" ) or die "Can't open $file: $!";
112 print FILE $Config{startperl}, ( $OPT{'w'} ? " -w" : "" ),
113 "\n#line ", ($headlines+1), "\n" unless $linedone;
114 print FILE @file[$skiplines..$#file];
115 print FILE $tail unless $taildone;
122 pl2bat - wrap perl code into a batch file
128 B<pl2bat> [B<-w>] S<[B<-a> I<argstring>]> S<[B<-s> I<stripsuffix>]> [files]
130 B<pl2bat> [B<-w>] S<[B<-n> I<ntargs>]> S<[B<-o> I<otherargs>]> S<[B<-s> I<stripsuffix>]> [files]
134 This utility converts a perl script into a batch file that can be
135 executed on DOS-like operating systems.
137 Note that by default, the ".pl" suffix will be stripped before adding
138 a ".bat" suffix to the supplied file names. This can be controlled
139 with the C<-s> option.
141 The default behavior is to have the batch file compare the C<OS>
142 environment variable against C<"Windows_NT">. If they match, it
143 uses the C<%*> construct to refer to all the command line arguments
144 that were given to it, so you'll need to make sure that works on your
145 variant of the command shell. It is known to work in the cmd.exe shell
146 under WindowsNT. 4DOS/NT users will want to put a C<ParameterChar = *>
147 line in their initialization file, or execute C<setdos /p*> in
148 the shell startup file.
150 On Windows95 and other platforms a nine-argument limit is imposed
151 on command-line arguments given to the generated batch file, since
152 they may not support C<%*> in batch files.
154 These can be overridden using the C<-n> and C<-o> options or the
155 deprecated C<-a> option.
161 =item B<-n> I<ntargs>
163 Arguments to invoke perl with in generated batch file when run from
164 Windows NT (or Windows 98, probably). Defaults to S<'-x -S "%0" %*'>.
166 =item B<-o> I<otherargs>
168 Arguments to invoke perl with in generated batch file except when
169 run from Windows NT (ie. when run from DOS, Windows 3.1, or Windows 95).
170 Defaults to S<'-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'>.
172 =item B<-a> I<argstring>
174 Arguments to invoke perl with in generated batch file. Specifying
175 B<-a> prevents the batch file from checking the C<OS> environment
176 variable to determine which operating system it is being run from.
178 =item B<-s> I<stripsuffix>
180 Strip a suffix string from file name before appending a ".bat"
181 suffix. The suffix is not case-sensitive. It can be a regex if
182 it begins with `/' (the trailing '/' is optional and a trailing
183 C<$> is always assumed). Defaults to C</.plx?/>.
187 If no line matching C</^#!.*perl/> is found in the script, then such
188 a line is inserted just after the new preamble. The exact line
189 depends on C<$Config{startperl}> [see L<Config>]. With the B<-w>
190 option, C<" -w"> is added after the value of C<$Config{startperl}>.
191 If a line matching C</^#!.*perl/> already exists in the script,
192 then it is not changed and the B<-w> option is ignored.
196 If the script appears to have already been processed by B<pl2bat>,
197 then the script is skipped and not processed unless B<-u> was
198 specified. If B<-u> is specified, the existing preamble is replaced.
202 Show command line usage.
208 C:\> pl2bat foo.pl bar.PM
209 [..creates foo.bat, bar.PM.bat..]
211 C:\> pl2bat -s "/\.pl|\.pm/" foo.pl bar.PM
212 [..creates foo.bat, bar.bat..]
214 C:\> pl2bat < somefile > another.bat
216 C:\> pl2bat > another.bat
217 print scalar reverse "rekcah lrep rehtona tsuj\n";
219 [..another.bat is now a certified japh application..]
223 [..updates the wrapping of some previously wrapped scripts..]
225 C:\> pl2bat -u -s .bat *.bat
226 [..same as previous example except more dangerous..]
230 C<$0> will contain the full name, including the ".bat" suffix
231 when the generated batch file runs. If you don't like this,
232 see runperl.bat for an alternative way to invoke perl scripts.
234 Default behavior is to invoke Perl with the B<-S> flag, so Perl will
235 search the PATH to find the script. This may have undesirable
240 perl, perlwin32, runperl.bat