Commit | Line | Data |
d444a431 |
1 | #!perl -w |
2 | require 5; |
3 | use Getopt::Std; |
4 | |
5 | $0 =~ s|.*[/\\]||; |
6 | |
7 | my $usage = <<EOT; |
8 | Usage: $0 [-h] [-a argstring] [-s stripsuffix] [files] |
9 | -a argstring arguments to invoke perl with in generated file |
10 | Defaults to "-x -S %0 %*" on WindowsNT, |
11 | "-x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9" otherwise |
12 | -s stripsuffix strip this suffix from file before appending ".bat" |
13 | Not case-sensitive |
14 | Can be a regex if it begins with `/' |
15 | Defaults to "/\.pl/" |
16 | -h show this help |
17 | EOT |
18 | |
19 | my %OPT = (); |
20 | warn($usage), exit(0) if !getopts('ha:s:',\%OPT) or $OPT{'h'}; |
21 | $OPT{'a'} = ($^O eq 'MSWin32' and &Win32::IsWinNT |
22 | ? '-x -S %0 %*' |
23 | : '-x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9') |
24 | unless exists $OPT{'a'}; |
25 | $OPT{'s'} = '.pl' unless exists $OPT{'s'}; |
26 | $OPT{'s'} = ($OPT{'s'} =~ m|^/([^/]*)| ? $1 : "\Q$OPT{'s'}\E"); |
27 | |
28 | (my $head = <<EOT) =~ s/^\t//gm; |
29 | \@rem = '--*-Perl-*-- |
30 | \@echo off |
31 | perl $OPT{'a'} |
32 | goto endofperl |
33 | \@rem '; |
34 | EOT |
35 | my $headlines = 2 + ($head =~ tr/\n/\n/); |
36 | my $tail = "__END__\n:endofperl\n"; |
37 | |
38 | @ARGV = ('-') unless @ARGV; |
39 | |
40 | process(@ARGV); |
41 | |
42 | sub process { |
43 | LOOP: |
44 | foreach ( @_ ) { |
45 | my $myhead = $head; |
46 | my $linedone = 0; |
47 | my $linenum = $headlines; |
48 | my $line; |
49 | open( FILE, $_ ) or die "$0: Can't open $_: $!"; |
50 | @file = <FILE>; |
51 | foreach $line ( @file ) { |
52 | $linenum++; |
53 | if ( $line =~ /^:endofperl/) { |
54 | warn "$0: $_ has already been converted to a batch file!\n"; |
55 | next LOOP; |
56 | } |
57 | if ( not $linedone and $line =~ /^#!.*perl/ ) { |
58 | $line .= "#line $linenum\n"; |
59 | $linedone++; |
60 | } |
61 | } |
62 | close( FILE ); |
63 | s/$OPT{'s'}$//oi; |
64 | $_ .= '.bat' unless /\.bat$/i or /^-$/; |
65 | open( FILE, ">$_" ) or die "Can't open $_: $!"; |
66 | print FILE $myhead; |
67 | print FILE "#!perl\n#line " . ($headlines+1) . "\n" unless $linedone; |
68 | print FILE @file, $tail; |
69 | close( FILE ); |
70 | } |
71 | } |
72 | __END__ |
73 | |
74 | =head1 NAME |
75 | |
76 | pl2bat - wrap perl code into a batch file |
77 | |
78 | =head1 SYNOPSIS |
79 | |
80 | B<pl2bat> [B<-h>] S<[B<-a> I<argstring>]> S<[B<-s> I<stripsuffix>]> [files] |
81 | |
82 | =head1 DESCRIPTION |
83 | |
84 | This utility converts a perl script into a batch file that can be |
85 | executed on DOS-like operating systems. |
86 | |
87 | Note that by default, the ".pl" suffix will be stripped before adding |
88 | a ".bat" suffix to the supplied file names. This can be controlled |
89 | with the C<-s> option. |
90 | |
91 | The default behavior on WindowsNT is to generate a batch file that |
92 | uses the C<%*> construct to refer to all the command line arguments |
93 | that were given to it, so you'll need to make sure that works on your |
94 | variant of the command shell. It is known to work in the cmd.exe shell |
95 | under WindowsNT. 4DOS/NT users will want to put a C<ParameterChar = *> |
96 | line in their initialization file, or execute C<setdos /p*> in |
97 | the shell startup file. On Windows95 and other platforms a nine |
98 | argument limit is imposed on command-line arguments given to the |
99 | generated batch file, since they may not support C<%*> in batch files. |
100 | This can be overridden using the C<-a> option. |
101 | |
102 | =head1 OPTIONS |
103 | |
104 | =over 8 |
105 | |
106 | =item B<-a> I<argstring> |
107 | |
108 | Arguments to invoke perl with in generated batch file. Defaults to |
109 | S<"-x -S %0 %*"> on WindowsNT, S<"-x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9"> |
110 | on other platforms. |
111 | |
112 | =item B<-s> I<stripsuffix> |
113 | |
114 | Strip a suffix string from file name before appending a ".bat" |
115 | suffix. The suffix is not case-sensitive. It can be a regex if it |
116 | begins with `/' (the trailing '/' being optional. Defaults to ".pl". |
117 | |
118 | =item B<-h> |
119 | |
120 | Show command line usage. |
121 | |
122 | =back |
123 | |
124 | =head1 EXAMPLES |
125 | |
126 | C:\> pl2bat foo.pl bar.PM |
127 | [..creates foo.bat, bar.PM.bat..] |
128 | |
129 | C:\> pl2bat -s "/\.pl|\.pm/" foo.pl bar.PM |
130 | [..creates foo.bat, bar.bat..] |
131 | |
132 | C:\> pl2bat < somefile > another.bat |
133 | |
134 | C:\> pl2bat > another.bat |
135 | print scalar reverse "rekcah lrep rehtona tsuj\n"; |
136 | ^Z |
137 | [..another.bat is now a certified japh application..] |
138 | |
139 | =head1 BUGS |
140 | |
141 | C<$0> will contain the full name, including the ".bat" suffix |
142 | when the generated batch file runs. If you don't like this, |
143 | see runperl.bat for an alternative way to invoke perl scripts. |
144 | |
145 | Default behavior is to invoke Perl with the -S flag, so Perl will |
146 | search the PATH to find the script. This may have undesirable |
147 | effects. |
148 | |
149 | =head1 SEE ALSO |
150 | |
151 | perl, perlwin32, runperl.bat |
152 | |
153 | =cut |
154 | |