Win95-proofing pl2bat
[p5sagit/p5-mst-13.2.git] / win32 / bin / pl2bat.bat
1 @rem = '--*-Perl-*--
2 @echo off
3 perl -x -S %0 %*
4 goto endofperl
5 @rem ';
6 #!perl -w
7 #line 8
8 (my $head = <<'--end--') =~ s/^\t//gm;
9         @rem = '--*-Perl-*--
10         @echo off
11         perl -x -S %0 %*
12         goto endofperl
13         @rem ';
14 --end--
15 my $headlines = 2 + ($head =~ tr/\n/\n/);
16 my $tail = "__END__\n:endofperl\n";
17
18 @ARGV = ('-') unless @ARGV;
19
20 process(@ARGV);
21
22 sub process {
23    LOOP:
24     foreach ( @_ ) {
25         my $myhead = $head;
26         my $linedone = 0;
27         my $linenum = $headlines;
28         my $line;
29         open( FILE, $_ ) or die "Can't open $_: $!";
30         @file = <FILE>;
31         foreach $line ( @file ) {
32             $linenum++;
33             if ( $line =~ /^:endofperl/) {
34                 warn "$_ has already been converted to a batch file!\n";
35                 next LOOP;
36             }
37             if ( not $linedone and $line =~ /^#!.*perl/ ) {
38                 $line .= "#line $linenum\n";
39                 $linedone++;
40             }
41         }
42         close( FILE );
43         s/\.pl$//;
44         $_ .= '.bat' unless /\.bat$/ or /^-$/;
45         open( FILE, ">$_" ) or die "Can't open $_: $!";
46         print FILE $myhead;
47         print FILE "#!perl\n#line " . ($headlines+1) . "\n" unless $linedone;
48         print FILE @file, $tail;
49         close( FILE );
50     }
51 }
52 __END__
53
54 =head1 NAME
55
56 pl2bat.bat - a batch file to wrap perl code into a batch file
57
58 =head1 SYNOPSIS
59
60         C:\> pl2bat foo.pl bar 
61         [..creates foo.bat, bar.bat..]
62         
63         C:\> pl2bat < somefile > another.bat
64         
65         C:\> pl2bat > another.bat
66         print scalar reverse "rekcah lrep rehtona tsuj\n";
67         ^Z
68         [..another.bat is now a certified japh application..]
69
70 =head1 DESCRIPTION
71
72 This utility converts a perl script into a batch file that can be
73 executed on DOS-like operating systems.
74
75 Note that the ".pl" suffix will be stripped before adding a
76 ".bat" suffix to the supplied file names.
77
78 The batch file created makes use of the C<%*> construct to refer
79 to all the command line arguments that were given to the batch file,
80 so you'll need to make sure that works on your variant of the
81 command shell.  It is known to work in the cmd.exe shell under
82 WindowsNT.  4DOS/NT users will want to put a C<ParameterChar = *>
83 line in their initialization file, or execute C<setdos /p*> in
84 the shell startup file.
85
86 =head1 BUGS
87
88 C<$0> will contain the full name, including the ".bat" suffix.
89 If you don't like this, see runperl.bat for an alternative way to
90 invoke perl scripts.
91
92 Perl is invoked with the -S flag, so it will search the PATH to find
93 the script.  This may have undesirable effects.
94
95 =head1 SEE ALSO
96
97 perl, perlwin32, runperl.bat
98
99 =cut
100
101 __END__
102 :endofperl
103