home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / bin / pl2bat.bat < prev    next >
Encoding:
DOS Batch File  |  1997-08-10  |  2.7 KB  |  106 lines

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