home *** CD-ROM | disk | FTP | other *** search
/ Chip: Windows 2000 Professional Resource Kit / W2KPRK.iso / apps / perl / ActivePerl.exe / data.z / pl2bat.bat < prev    next >
Encoding:
DOS Batch File  |  1999-10-16  |  8.0 KB  |  261 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. if "%OS%" == "Windows_NT" goto WinNT
  4. perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. :WinNT
  7. perl -x -S "%0" %*
  8. if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
  9. if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  10. goto endofperl
  11. @rem ';
  12. #!perl
  13. #line 14
  14.     eval 'exec perl -x -S "$0" ${1+"$@"}'
  15.     if 0;    # In case running under some shell
  16.  
  17. require 5;
  18. use Getopt::Std;
  19. use Config;
  20.  
  21. $0 =~ s|.*[/\\]||;
  22.  
  23. my $usage = <<EOT;
  24. Usage:  $0 [-h]
  25.    or:  $0 [-w] [-u] [-a argstring] [-s stripsuffix] [files]
  26.    or:  $0 [-w] [-u] [-n ntargs] [-o otherargs] [-s stripsuffix] [files]
  27.         -n ntargs       arguments to invoke perl with in generated file
  28.                             when run from Windows NT.  Defaults to
  29.                             '-x -S "%0" %*'.
  30.         -o otherargs    arguments to invoke perl with in generated file
  31.                             other than when run from Windows NT.  Defaults
  32.                             to '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'.
  33.         -a argstring    arguments to invoke perl with in generated file
  34.                             ignoring operating system (for compatibility
  35.                             with previous pl2bat versions).
  36.         -u              update files that may have already been processed
  37.                             by (some version of) pl2bat.
  38.         -w              include "-w" on the /^#!.*perl/ line (unless
  39.                             a /^#!.*perl/ line was already present).
  40.         -s stripsuffix  strip this suffix from file before appending ".bat"
  41.                             Not case-sensitive
  42.                             Can be a regex if it begins with `/'
  43.                             Defaults to "/\.plx?/"
  44.         -h              show this help
  45. EOT
  46.  
  47. my %OPT = ();
  48. warn($usage), exit(0) if !getopts('whun:o:a:s:',\%OPT) or $OPT{'h'};
  49. $OPT{'n'} = '-x -S "%0" %*' unless exists $OPT{'n'};
  50. $OPT{'o'} = '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9' unless exists $OPT{'o'};
  51. $OPT{'s'} = '/\\.plx?/' unless exists $OPT{'s'};
  52. $OPT{'s'} = ($OPT{'s'} =~ m|^/([^/]*)| ? $1 : "\Q$OPT{'s'}\E");
  53.  
  54. my $head;
  55. if(  defined( $OPT{'a'} )  ) {
  56.     $head = <<EOT;
  57.     \@rem = '--*-Perl-*--
  58.     \@echo off
  59.     perl $OPT{'a'}
  60.     goto endofperl
  61.     \@rem ';
  62. EOT
  63. } else {
  64.     $head = <<EOT;
  65.     \@rem = '--*-Perl-*--
  66.     \@echo off
  67.     if "%OS%" == "Windows_NT" goto WinNT
  68.     perl $OPT{'o'}
  69.     goto endofperl
  70.     :WinNT
  71.     perl $OPT{'n'}
  72.     if NOT "%COMSPEC%" == "%SystemRoot%\\system32\\cmd.exe" goto endofperl
  73.     if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  74.     goto endofperl
  75.     \@rem ';
  76. EOT
  77. }
  78. $head =~ s/^\t//gm;
  79. my $headlines = 2 + ($head =~ tr/\n/\n/);
  80. my $tail = "__END__\n:endofperl\n";
  81.  
  82. @ARGV = ('-') unless @ARGV;
  83.  
  84. foreach ( @ARGV ) {
  85.     process($_);
  86. }
  87.  
  88. sub process {
  89.  my( $file )= @_;
  90.     my $myhead = $head;
  91.     my $linedone = 0;
  92.     my $taildone = 0;
  93.     my $linenum = 0;
  94.     my $skiplines = 0;
  95.     my $line;
  96.     my $start= $Config{startperl};
  97.     $start= "#!perl"   unless  $start =~ /^#!.*perl/;
  98.     open( FILE, $file ) or die "$0: Can't open $file: $!";
  99.     @file = <FILE>;
  100.     foreach $line ( @file ) {
  101.     $linenum++;
  102.     if ( $line =~ /^:endofperl\b/ ) {
  103.         if(  ! exists $OPT{'u'}  ) {
  104.         warn "$0: $file has already been converted to a batch file!\n";
  105.         return;
  106.         }
  107.         $taildone++;
  108.     }
  109.     if ( not $linedone and $line =~ /^#!.*perl/ ) {
  110.         if(  exists $OPT{'u'}  ) {
  111.         $skiplines = $linenum - 1;
  112.         $line .= "#line ".(1+$headlines)."\n";
  113.         } else {
  114.         $line .= "#line ".($linenum+$headlines)."\n";
  115.         }
  116.         $linedone++;
  117.     }
  118.     if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
  119.         $line = "";
  120.     }
  121.     }
  122.     close( FILE );
  123.     $file =~ s/$OPT{'s'}$//oi;
  124.     $file .= '.bat' unless $file =~ /\.bat$/i or $file =~ /^-$/;
  125.     open( FILE, ">$file" ) or die "Can't open $file: $!";
  126.     print FILE $myhead;
  127.     print FILE $start, ( $OPT{'w'} ? " -w" : "" ),
  128.            "\n#line ", ($headlines+1), "\n" unless $linedone;
  129.     print FILE @file[$skiplines..$#file];
  130.     print FILE $tail unless $taildone;
  131.     close( FILE );
  132. }
  133. __END__
  134.  
  135. =head1 NAME
  136.  
  137. pl2bat - wrap perl code into a batch file
  138.  
  139. =head1 SYNOPSIS
  140.  
  141. B<pl2bat> B<-h>
  142.  
  143. B<pl2bat> [B<-w>] S<[B<-a> I<argstring>]> S<[B<-s> I<stripsuffix>]> [files]
  144.  
  145. B<pl2bat> [B<-w>] S<[B<-n> I<ntargs>]> S<[B<-o> I<otherargs>]> S<[B<-s> I<stripsuffix>]> [files]
  146.  
  147. =head1 DESCRIPTION
  148.  
  149. This utility converts a perl script into a batch file that can be
  150. executed on DOS-like operating systems.
  151.  
  152. Note that by default, the ".pl" suffix will be stripped before adding
  153. a ".bat" suffix to the supplied file names.  This can be controlled
  154. with the C<-s> option.
  155.  
  156. The default behavior is to have the batch file compare the C<OS>
  157. environment variable against C<"Windows_NT">.  If they match, it
  158. uses the C<%*> construct to refer to all the command line arguments
  159. that were given to it, so you'll need to make sure that works on your
  160. variant of the command shell.  It is known to work in the cmd.exe shell
  161. under WindowsNT.  4DOS/NT users will want to put a C<ParameterChar = *>
  162. line in their initialization file, or execute C<setdos /p*> in
  163. the shell startup file.
  164.  
  165. On Windows95 and other platforms a nine-argument limit is imposed
  166. on command-line arguments given to the generated batch file, since
  167. they may not support C<%*> in batch files.
  168.  
  169. These can be overridden using the C<-n> and C<-o> options or the
  170. deprecated C<-a> option.
  171.  
  172. =head1 OPTIONS
  173.  
  174. =over 8
  175.  
  176. =item B<-n> I<ntargs>
  177.  
  178. Arguments to invoke perl with in generated batch file when run from
  179. Windows NT (or Windows 98, probably).  Defaults to S<'-x -S "%0" %*'>.
  180.  
  181. =item B<-o> I<otherargs>
  182.  
  183. Arguments to invoke perl with in generated batch file except when
  184. run from Windows NT (ie. when run from DOS, Windows 3.1, or Windows 95).
  185. Defaults to S<'-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'>.
  186.  
  187. =item B<-a> I<argstring>
  188.  
  189. Arguments to invoke perl with in generated batch file.  Specifying
  190. B<-a> prevents the batch file from checking the C<OS> environment
  191. variable to determine which operating system it is being run from.
  192.  
  193. =item B<-s> I<stripsuffix>
  194.  
  195. Strip a suffix string from file name before appending a ".bat"
  196. suffix.  The suffix is not case-sensitive.  It can be a regex if
  197. it begins with `/' (the trailing '/' is optional and a trailing
  198. C<$> is always assumed).  Defaults to C</.plx?/>.
  199.  
  200. =item B<-w>
  201.  
  202. If no line matching C</^#!.*perl/> is found in the script, then such
  203. a line is inserted just after the new preamble.  The exact line
  204. depends on C<$Config{startperl}> [see L<Config>].  With the B<-w>
  205. option, C<" -w"> is added after the value of C<$Config{startperl}>.
  206. If a line matching C</^#!.*perl/> already exists in the script,
  207. then it is not changed and the B<-w> option is ignored.
  208.  
  209. =item B<-u>
  210.  
  211. If the script appears to have already been processed by B<pl2bat>,
  212. then the script is skipped and not processed unless B<-u> was
  213. specified.  If B<-u> is specified, the existing preamble is replaced.
  214.  
  215. =item B<-h>
  216.  
  217. Show command line usage.
  218.  
  219. =back
  220.  
  221. =head1 EXAMPLES
  222.  
  223.     C:\> pl2bat foo.pl bar.PM 
  224.     [..creates foo.bat, bar.PM.bat..]
  225.     
  226.     C:\> pl2bat -s "/\.pl|\.pm/" foo.pl bar.PM
  227.     [..creates foo.bat, bar.bat..]
  228.     
  229.     C:\> pl2bat < somefile > another.bat
  230.     
  231.     C:\> pl2bat > another.bat
  232.     print scalar reverse "rekcah lrep rehtona tsuj\n";
  233.     ^Z
  234.     [..another.bat is now a certified japh application..]
  235.     
  236.     C:\> ren *.bat *.pl
  237.     C:\> pl2bat -u *.pl
  238.     [..updates the wrapping of some previously wrapped scripts..]
  239.     
  240.     C:\> pl2bat -u -s .bat *.bat
  241.     [..same as previous example except more dangerous..]
  242.  
  243. =head1 BUGS
  244.  
  245. C<$0> will contain the full name, including the ".bat" suffix
  246. when the generated batch file runs.  If you don't like this,
  247. see runperl.bat for an alternative way to invoke perl scripts.
  248.  
  249. Default behavior is to invoke Perl with the B<-S> flag, so Perl will
  250. search the PATH to find the script.   This may have undesirable
  251. effects.
  252.  
  253. =head1 SEE ALSO
  254.  
  255. perl, perlwin32, runperl.bat
  256.  
  257. =cut
  258.  
  259. __END__
  260. :endofperl
  261.