source: trunk/essentials/dev-lang/perl/win32/bin/pl2bat.pl@ 3296

Last change on this file since 3296 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 12.5 KB
Line 
1 eval 'exec perl -x -S "$0" ${1+"$@"}'
2 if 0; # In case running under some shell
3
4require 5;
5use Getopt::Std;
6use Config;
7
8$0 =~ s|.*[/\\]||;
9
10my $usage = <<EOT;
11Usage: $0 [-h]
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
16 '-x -S %0 %*'.
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"
28 Not case-sensitive
29 Can be a regex if it begins with `/'
30 Defaults to "/\.plx?/"
31 -h show this help
32EOT
33
34my %OPT = ();
35warn($usage), exit(0) if !getopts('whun:o:a:s:',\%OPT) or $OPT{'h'};
36# NOTE: %0 is already enclosed in doublequotes by cmd.exe, as appropriate
37$OPT{'n'} = '-x -S %0 %*' unless exists $OPT{'n'};
38$OPT{'o'} = '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9' unless exists $OPT{'o'};
39$OPT{'s'} = '/\\.plx?/' unless exists $OPT{'s'};
40$OPT{'s'} = ($OPT{'s'} =~ m#^/([^/]*[^/\$]|)\$?/?$# ? $1 : "\Q$OPT{'s'}\E");
41
42my $head;
43if( defined( $OPT{'a'} ) ) {
44 $head = <<EOT;
45 \@rem = '--*-Perl-*--
46 \@echo off
47 perl $OPT{'a'}
48 goto endofperl
49 \@rem ';
50EOT
51} else {
52 $head = <<EOT;
53 \@rem = '--*-Perl-*--
54 \@echo off
55 if "%OS%" == "Windows_NT" goto WinNT
56 perl $OPT{'o'}
57 goto endofperl
58 :WinNT
59 perl $OPT{'n'}
60 if NOT "%COMSPEC%" == "%SystemRoot%\\system32\\cmd.exe" goto endofperl
61 if %errorlevel% == 9009 echo You do not have Perl in your PATH.
62 if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
63 goto endofperl
64 \@rem ';
65EOT
66}
67$head =~ s/^\t//gm;
68my $headlines = 2 + ($head =~ tr/\n/\n/);
69my $tail = "\n__END__\n:endofperl\n";
70
71@ARGV = ('-') unless @ARGV;
72
73foreach ( @ARGV ) {
74 process($_);
75}
76
77sub process {
78 my( $file )= @_;
79 my $myhead = $head;
80 my $linedone = 0;
81 my $taildone = 0;
82 my $linenum = 0;
83 my $skiplines = 0;
84 my $line;
85 my $start= $Config{startperl};
86 $start= "#!perl" unless $start =~ /^#!.*perl/;
87 open( FILE, $file ) or die "$0: Can't open $file: $!";
88 @file = <FILE>;
89 foreach $line ( @file ) {
90 $linenum++;
91 if ( $line =~ /^:endofperl\b/ ) {
92 if( ! exists $OPT{'u'} ) {
93 warn "$0: $file has already been converted to a batch file!\n";
94 return;
95 }
96 $taildone++;
97 }
98 if ( not $linedone and $line =~ /^#!.*perl/ ) {
99 if( exists $OPT{'u'} ) {
100 $skiplines = $linenum - 1;
101 $line .= "#line ".(1+$headlines)."\n";
102 } else {
103 $line .= "#line ".($linenum+$headlines)."\n";
104 }
105 $linedone++;
106 }
107 if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
108 $line = "";
109 }
110 }
111 close( FILE );
112 $file =~ s/$OPT{'s'}$//oi;
113 $file .= '.bat' unless $file =~ /\.bat$/i or $file =~ /^-$/;
114 open( FILE, ">$file" ) or die "Can't open $file: $!";
115 print FILE $myhead;
116 print FILE $start, ( $OPT{'w'} ? " -w" : "" ),
117 "\n#line ", ($headlines+1), "\n" unless $linedone;
118 print FILE @file[$skiplines..$#file];
119 print FILE $tail unless $taildone;
120 close( FILE );
121}
122__END__
123
124=head1 NAME
125
126pl2bat - wrap perl code into a batch file
127
128=head1 SYNOPSIS
129
130B<pl2bat> B<-h>
131
132B<pl2bat> [B<-w>] S<[B<-a> I<argstring>]> S<[B<-s> I<stripsuffix>]> [files]
133
134B<pl2bat> [B<-w>] S<[B<-n> I<ntargs>]> S<[B<-o> I<otherargs>]> S<[B<-s> I<stripsuffix>]> [files]
135
136=head1 DESCRIPTION
137
138This utility converts a perl script into a batch file that can be
139executed on DOS-like operating systems. This is intended to allow
140you to use a Perl script like regular programs and batch files where
141you just enter the name of the script [probably minus the extension]
142plus any command-line arguments and the script is found in your B<PATH>
143and run.
144
145=head2 ADVANTAGES
146
147There are several alternatives to this method of running a Perl script.
148They each have disadvantages that help you understand the motivation
149for using B<pl2bat>.
150
151=over
152
153=item 1
154
155 C:> perl x:/path/to/script.pl [args]
156
157=item 2
158
159 C:> perl -S script.pl [args]
160
161=item 3
162
163 C:> perl -S script [args]
164
165=item 4
166
167 C:> ftype Perl=perl.exe "%1" %*
168 C:> assoc .pl=Perl
169 then
170 C:> script.pl [args]
171
172=item 5
173
174 C:> ftype Perl=perl.exe "%1" %*
175 C:> assoc .pl=Perl
176 C:> set PathExt=%PathExt%;.PL
177 then
178 C:> script [args]
179
180=back
181
182B<1> and B<2> are the most basic invocation methods that should work on
183any system [DOS-like or not]. They require extra typing and require
184that the script user know that the script is written in Perl. This
185is a pain when you have lots of scripts, some written in Perl and some
186not. It can be quite difficult to keep track of which scripts need to
187be run through Perl and which do not. Even worse, scripts often get
188rewritten from simple batch files into more powerful Perl scripts in
189which case these methods would require all existing users of the scripts
190be updated.
191
192B<3> works on modern Win32 versions of Perl. It allows the user to
193omit the ".pl" or ".bat" file extension, which is a minor improvement.
194
195B<4> and B<5> work on some Win32 operating systems with some command
196shells. One major disadvantage with both is that you can't use them
197in pipelines nor with file redirection. For example, none of the
198following will work properly if you used method B<4> or B<5>:
199
200 C:> script.pl <infile
201 C:> script.pl >outfile
202 C:> echo y | script.pl
203 C:> script.pl | more
204
205This is due to a Win32 bug which Perl has no control over. This bug