home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _38cb90837667fca56be5c6a6ef635597 < prev    next >
Text File  |  2000-03-24  |  14KB  |  430 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. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
  11. goto endofperl
  12. @rem ';
  13. #!perl
  14. #line 15
  15.     eval 'exec perl -x -S "$0" ${1+"$@"}'
  16.     if 0;    # In case running under some shell
  17.  
  18. require 5;
  19. use Getopt::Std;
  20. use Config;
  21.  
  22. $0 =~ s|.*[/\\]||;
  23.  
  24. my $usage = <<EOT;
  25. Usage:  $0 [-h]
  26.    or:  $0 [-w] [-u] [-a argstring] [-s stripsuffix] [files]
  27.    or:  $0 [-w] [-u] [-n ntargs] [-o otherargs] [-s stripsuffix] [files]
  28.         -n ntargs       arguments to invoke perl with in generated file
  29.                             when run from Windows NT.  Defaults to
  30.                             '-x -S "%0" %*'.
  31.         -o otherargs    arguments to invoke perl with in generated file
  32.                             other than when run from Windows NT.  Defaults
  33.                             to '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'.
  34.         -a argstring    arguments to invoke perl with in generated file
  35.                             ignoring operating system (for compatibility
  36.                             with previous pl2bat versions).
  37.         -u              update files that may have already been processed
  38.                             by (some version of) pl2bat.
  39.         -w              include "-w" on the /^#!.*perl/ line (unless
  40.                             a /^#!.*perl/ line was already present).
  41.         -s stripsuffix  strip this suffix from file before appending ".bat"
  42.                             Not case-sensitive
  43.                             Can be a regex if it begins with `/'
  44.                             Defaults to "/\.plx?/"
  45.         -h              show this help
  46. EOT
  47.  
  48. my %OPT = ();
  49. warn($usage), exit(0) if !getopts('whun:o:a:s:',\%OPT) or $OPT{'h'};
  50. $OPT{'n'} = '-x -S "%0" %*' unless exists $OPT{'n'};
  51. $OPT{'o'} = '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9' unless exists $OPT{'o'};
  52. $OPT{'s'} = '/\\.plx?/' unless exists $OPT{'s'};
  53. $OPT{'s'} = ($OPT{'s'} =~ m#^/([^/]*[^/\$]|)\$?/?$# ? $1 : "\Q$OPT{'s'}\E");
  54.  
  55. my $head;
  56. if(  defined( $OPT{'a'} )  ) {
  57.     $head = <<EOT;
  58.     \@rem = '--*-Perl-*--
  59.     \@echo off
  60.     perl $OPT{'a'}
  61.     goto endofperl
  62.     \@rem ';
  63. EOT
  64. } else {
  65.     $head = <<EOT;
  66.     \@rem = '--*-Perl-*--
  67.     \@echo off
  68.     if "%OS%" == "Windows_NT" goto WinNT
  69.     perl $OPT{'o'}
  70.     goto endofperl
  71.     :WinNT
  72.     perl $OPT{'n'}
  73.     if NOT "%COMSPEC%" == "%SystemRoot%\\system32\\cmd.exe" goto endofperl
  74.     if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  75.     if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
  76.     goto endofperl
  77.     \@rem ';
  78. EOT
  79. }
  80. $head =~ s/^\t//gm;
  81. my $headlines = 2 + ($head =~ tr/\n/\n/);
  82. my $tail = "\n__END__\n:endofperl\n";
  83.  
  84. @ARGV = ('-') unless @ARGV;
  85.  
  86. foreach ( @ARGV ) {
  87.     process($_);
  88. }
  89.  
  90. sub process {
  91.  my( $file )= @_;
  92.     my $myhead = $head;
  93.     my $linedone = 0;
  94.     my $taildone = 0;
  95.     my $linenum = 0;
  96.     my $skiplines = 0;
  97.     my $line;
  98.     my $start= $Config{startperl};
  99.     $start= "#!perl"   unless  $start =~ /^#!.*perl/;
  100.     open( FILE, $file ) or die "$0: Can't open $file: $!";
  101.     @file = <FILE>;
  102.     foreach $line ( @file ) {
  103.     $linenum++;
  104.     if ( $line =~ /^:endofperl\b/ ) {
  105.         if(  ! exists $OPT{'u'}  ) {
  106.         warn "$0: $file has already been converted to a batch file!\n";
  107.         return;
  108.         }
  109.         $taildone++;
  110.     }
  111.     if ( not $linedone and $line =~ /^#!.*perl/ ) {
  112.         if(  exists $OPT{'u'}  ) {
  113.         $skiplines = $linenum - 1;
  114.         $line .= "#line ".(1+$headlines)."\n";
  115.         } else {
  116.         $line .= "#line ".($linenum+$headlines)."\n";
  117.         }
  118.         $linedone++;
  119.     }
  120.     if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
  121.         $line = "";
  122.     }
  123.     }
  124.     close( FILE );
  125.     $file =~ s/$OPT{'s'}$//oi;
  126.     $file .= '.bat' unless $file =~ /\.bat$/i or $file =~ /^-$/;
  127.     open( FILE, ">$file" ) or die "Can't open $file: $!";
  128.     print FILE $myhead;
  129.     print FILE $start, ( $OPT{'w'} ? " -w" : "" ),
  130.            "\n#line ", ($headlines+1), "\n" unless $linedone;
  131.     print FILE @file[$skiplines..$#file];
  132.     print FILE $tail unless $taildone;
  133.     close( FILE );
  134. }
  135. __END__
  136.  
  137. =head1 NAME
  138.  
  139. pl2bat - wrap perl code into a batch file
  140.  
  141. =head1 SYNOPSIS
  142.  
  143. B<pl2bat> B<-h>
  144.  
  145. B<pl2bat> [B<-w>] S<[B<-a> I<argstring>]> S<[B<-s> I<stripsuffix>]> [files]
  146.  
  147. B<pl2bat> [B<-w>] S<[B<-n> I<ntargs>]> S<[B<-o> I<otherargs>]> S<[B<-s> I<stripsuffix>]> [files]
  148.  
  149. =head1 DESCRIPTION
  150.  
  151. This utility converts a perl script into a batch file that can be
  152. executed on DOS-like operating systems.  This is intended to allow
  153. you to use a Perl script like regular programs and batch files where
  154. you just enter the name of the script [probably minus the extension]
  155. plus any command-line arguments and the script is found in your B<PATH>
  156. and run.
  157.  
  158. =head2 ADVANTAGES
  159.  
  160. There are several alternatives to this method of running a Perl script. 
  161. They each have disadvantages that help you understand the motivation
  162. for using B<pl2bat>.
  163.  
  164. =over
  165.  
  166. =item 1
  167.  
  168.     C:> perl x:/path/to/script.pl [args]
  169.  
  170. =item 2
  171.  
  172.     C:> perl -S script.pl [args]
  173.  
  174. =item 3
  175.  
  176.     C:> perl -S script [args]
  177.  
  178. =item 4
  179.  
  180.     C:> ftype Perl=perl.exe "%1" %*
  181.     C:> assoc .pl=Perl
  182.     then
  183.     C:> script.pl [args]
  184.  
  185. =item 5
  186.  
  187.     C:> ftype Perl=perl.exe "%1" %*
  188.     C:> assoc .pl=Perl
  189.     C:> set PathExt=%PathExt%;.PL
  190.     then
  191.     C:> script [args]
  192.  
  193. =back
  194.  
  195. B<1> and B<2> are the most basic invocation methods that should work on
  196. any system [DOS-like or not].  They require extra typing and require
  197. that the script user know that the script is written in Perl.  This
  198. is a pain when you have lots of scripts, some written in Perl and some
  199. not.  It can be quite difficult to keep track of which scripts need to
  200. be run through Perl and which do not.  Even worse, scripts often get
  201. rewritten from simple batch files into more powerful Perl scripts in
  202. which case these methods would require all existing users of the scripts
  203. be updated.
  204.  
  205. B<3> works on modern Win32 versions of Perl.  It allows the user to
  206. omit the ".pl" or ".bat" file extension, which is a minor improvement.
  207.  
  208. B<4> and B<5> work on some Win32 operating systems with some command
  209. shells.  One major disadvantage with both is that you can't use them
  210. in pipelines nor with file redirection.  For example, none of the
  211. following will work properly if you used method B<4> or B<5>:
  212.  
  213.     C:> script.pl <infile
  214.     C:> script.pl >outfile
  215.     C:> echo y | script.pl
  216.     C:> script.pl | more
  217.  
  218. This is due to a Win32 bug which Perl has no control over.  This bug
  219. is the major motivation for B<pl2bat> [which was originally written
  220. for DOS] being used on Win32 systems.
  221.  
  222. Note also that B<5> works on a smaller range of combinations of Win32
  223. systems and command shells while B<4> requires that the user know
  224. that the script is a Perl script [because the ".pl" extension must
  225. be entered].  This makes it hard to standardize on either of these
  226. methods.
  227.  
  228. =head2 DISADVANTAGES
  229.  
  230. There are several potential traps you should be aware of when you
  231. use B<pl2bat>.
  232.  
  233. The generated batch file is initially processed as a batch file each
  234. time it is run.  This means that, to use it from within another batch
  235. file you should preceed it with C<call> or else the calling batch
  236. file will not run any commands after the script:
  237.  
  238.     call script [args]
  239.  
  240. Except under Windows NT, if you specify more than 9 arguments to
  241. the generated batch file then the 10th and subsequent arguments
  242. are silently ignored.
  243.  
  244. Except when using F<CMD.EXE> under Windows NT, if F<perl.exe> is not
  245. in your B<PATH>, then trying to run the script will give you a generic
  246. "Command not found"-type of error message that will probably make you
  247. think that the script itself is not in your B<PATH>.  When using
  248. F<CMD.EXE> under Windows NT, the generic error message is followed by
  249. "You do not have Perl in your PATH", to make this clearer.
  250.  
  251. On most DOS-like operating systems, the only way to exit a batch file
  252. is to "fall off the end" of the file.  B<pl2bat> implements this by
  253. doing C<goto :endofperl> and adding C<__END__> and C<:endofperl> as
  254. the last two lines of the generated batch file.  This means:
  255.  
  256. =over
  257.  
  258. =item No line of your script should start with a colon.
  259.  
  260. In particular, for this version of B<pl2bat>, C<:endofperl>,
  261. C<:WinNT>, and C<:script_failed_so_exit_with_non_zero_val> should not
  262. be used.
  263.  
  264. =item Care must be taken when using C<__END__> and the C<DATA> file handle.
  265.  
  266. One approach is:
  267.  
  268.     .  #!perl
  269.     .  while( <DATA> ) {
  270.     .      last   if  /^__END__$/;
  271.     .      [...]
  272.     .  }
  273.     .  __END__
  274.     .  lines of data
  275.     .  to be processed
  276.     .  __END__
  277.     .  :endofperl
  278.  
  279. The dots in the first column are only there to prevent F<cmd.exe> to interpret
  280. the C<:endofperl> line in this documentation.  Otherwise F<pl2bat.bat> itself
  281. wouldn't work.  See the previous item. :-)
  282.  
  283. =item The batch file always "succeeds"
  284.  
  285. The following commands illustrate the problem:
  286.  
  287.     C:> echo exit(99); >fail.pl
  288.     C:> pl2bat fail.pl
  289.     C:> perl -e "print system('perl fail.pl')"
  290.     99
  291.     C:> perl -e "print system('fail.bat')"
  292.     0
  293.  
  294. So F<fail.bat> always reports that it completed successfully.  Actually,
  295. under Windows NT, we have:
  296.  
  297.     C:> perl -e "print system('fail.bat')"
  298.     1
  299.  
  300. So, for Windows NT, F<fail.bat> fails when the Perl script fails, but
  301. the return code is always C<1>, not the return code from the Perl script.
  302.  
  303. =back
  304.  
  305. =head2 FUNCTION
  306.  
  307. By default, the ".pl" suffix will be stripped before adding a ".bat" suffix
  308. to the supplied file names.  This can be controlled with the C<-s> option.
  309.  
  310. The default behavior is to have the batch file compare the C<OS>
  311. environment variable against C<"Windows_NT">.  If they match, it
  312. uses the C<%*> construct to refer to all the command line arguments
  313. that were given to it, so you'll need to make sure that works on your
  314. variant of the command shell.  It is known to work in the F<CMD.EXE> shell
  315. under Windows NT.  4DOS/NT users will want to put a C<ParameterChar = *>
  316. line in their initialization file, or execute C<setdos /p*> in
  317. the shell startup file.
  318.  
  319. On Windows95 and other platforms a nine-argument limit is imposed
  320. on command-line arguments given to the generated batch file, since
  321. they may not support C<%*> in batch files.
  322.  
  323. These can be overridden using the C<-n> and C<-o> options or the
  324. deprecated C<-a> option.
  325.  
  326. =head1 OPTIONS
  327.  
  328. =over 8
  329.  
  330. =item B<-n> I<ntargs>
  331.  
  332. Arguments to invoke perl with in generated batch file when run from
  333. Windows NT (or Windows 98, probably).  Defaults to S<'-x -S "%0" %*'>.
  334.  
  335. =item B<-o> I<otherargs>
  336.  
  337. Arguments to invoke perl with in generated batch file except when
  338. run from Windows NT (ie. when run from DOS, Windows 3.1, or Windows 95).
  339. Defaults to S<'-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'>.
  340.  
  341. =item B<-a> I<argstring>
  342.  
  343. Arguments to invoke perl with in generated batch file.  Specifying
  344. B<-a> prevents the batch file from checking the C<OS> environment
  345. variable to determine which operating system it is being run from.
  346.  
  347. =item B<-s> I<stripsuffix>
  348.  
  349. Strip a suffix string from file name before appending a ".bat"
  350. suffix.  The suffix is not case-sensitive.  It can be a regex if
  351. it begins with `/' (the trailing '/' is optional and a trailing
  352. C<$> is always assumed).  Defaults to C</.plx?/>.
  353.  
  354. =item B<-w>
  355.  
  356. If no line matching C</^#!.*perl/> is found in the script, then such
  357. a line is inserted just after the new preamble.  The exact line
  358. depends on C<$Config{startperl}> [see L<Config>].  With the B<-w>
  359. option, C<" -w"> is added after the value of C<$Config{startperl}>.
  360. If a line matching C</^#!.*perl/> already exists in the script,
  361. then it is not changed and the B<-w> option is ignored.
  362.  
  363. =item B<-u>
  364.  
  365. If the script appears to have already been processed by B<pl2bat>,
  366. then the script is skipped and not processed unless B<-u> was
  367. specified.  If B<-u> is specified, the existing preamble is replaced.
  368.  
  369. =item B<-h>
  370.  
  371. Show command line usage.
  372.  
  373. =back
  374.  
  375. =head1 EXAMPLES
  376.  
  377.     C:\> pl2bat foo.pl bar.PM 
  378.     [..creates foo.bat, bar.PM.bat..]
  379.     
  380.     C:\> pl2bat -s "/\.pl|\.pm/" foo.pl bar.PM
  381.     [..creates foo.bat, bar.bat..]
  382.     
  383.     C:\> pl2bat < somefile > another.bat
  384.     
  385.     C:\> pl2bat > another.bat
  386.     print scalar reverse "rekcah lrep rehtona tsuj\n";
  387.     ^Z
  388.     [..another.bat is now a certified japh application..]
  389.     
  390.     C:\> ren *.bat *.pl
  391.     C:\> pl2bat -u *.pl
  392.     [..updates the wrapping of some previously wrapped scripts..]
  393.     
  394.     C:\> pl2bat -u -s .bat *.bat
  395.     [..same as previous example except more dangerous..]
  396.  
  397. =head1 BUGS
  398.  
  399. C<$0> will contain the full name, including the ".bat" suffix
  400. when the generated batch file runs.  If you don't like this,
  401. see runperl.bat for an alternative way to invoke perl scripts.
  402.  
  403. Default behavior is to invoke Perl with the B<-S> flag, so Perl will
  404. search the B<PATH> to find the script.   This may have undesirable
  405. effects.
  406.  
  407. On really old versions of Win32 Perl, you can't run the script
  408. via
  409.  
  410.     C:> script.bat [args]
  411.  
  412. and must use
  413.  
  414.     C:> script [args]
  415.  
  416. A loop should be used to build up the argument list when not on
  417. Windows NT so more than 9 arguments can be processed.
  418.  
  419. See also L</Disadvantages>.
  420.  
  421. =head1 SEE ALSO
  422.  
  423. perl, perlwin32, runperl.bat
  424.  
  425. =cut
  426.  
  427.  
  428. __END__
  429. :endofperl
  430.