sponsor Vim development Vim logo Vim Book Ad

basic Tip #638: Editing ActiveState Perl batch files

 tip karma   Rating 2/2, Viewed by 1114 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   January 19, 2004 2:28      complexity:   basic
author:   David DelGreco      as of Vim:   5.7

To run Perl scripts under Windows, you can either add the .pl extension to the PATHEXT env variable, or use pl2bat, which comes with ActiveState's Perl and makes a very nice batch file.  A quirk of Perl under Win32 is that piping doesn't work with .pl files (as in bar.pl | foo.pl) but works fine with the batch files.  It has something to do with how Windows loads files.  Anyway, this all works fine, but every time I do any extensive edits to a perl/batch file, I have to set cindent and syntax=perl or it drives me crazy.  This tip modifies filetype.vim to check batch files to see if they're really perl scripts in disguise.

--->First, find these lines:

" Batch file for MSDOS (*.cmd is close enough)
au BufNewFile,BufRead *.bat,*.cmd,*.sys setf dosbatch

--->Then change them to this:

" Batch file for MSDOS (*.cmd is close enough)
au BufNewFile,BufRead *.bat,*.cmd,*.sys call FTCheck_bat()

" Perl scripts converted to bat by pl2bat have a unique string that
" identifies the file.  It should be the first line.
fun! FTCheck_bat()
  if exists("g:filetype_bat")
    exe "setf " . g:filetype_bat
  else
    let l = getline(nextnonblank(1))
    if l =~ '--\*-Perl-\*--'
      setf perl
    else
      setf dosbatch
    endif
  endif
endfun


--->That's it!  This is very specific to look for the string pl2bat adds to the file, but can be easily modified to your needs.

 rate this tip  Life Changing Helpful Unfulfilling 

<< execute accidently inserted commands | Comment highlight #ifdef DEBUG for code-read ease (C/C++) >>

Additional Notes

Anonymous, January 19, 2004 4:10
Add this line at the top of your  converted pl2bat script and everything should  work automatically:
@rem vim: set ft=perl:
See :h modeline
Same anonymous, January 19, 2004 4:16
A little offtopic about perl:
Under windows you _can_ use piping _not converting to bat files_

#file  test.pl
print "hi!\n";

#file  test1.pl
$str = <>;
print "\$str=$str\n";

Now just run perl test.pl | perl test1.pl
And you see piping. :)
Anonymous, January 20, 2004 2:23
Offtopic response.  Sorry. It's <STDIN> that doesn't work correctly.  I've got a number of scripts with multiple command line options with know --specifiers.  I know if I use Getopt:: it will clear out @ARGV (or I can do it myself), but the files have a long-standing syntax and I just opted to read from STDIN.  Curiously, it works if you pipe to 'perl foo.pl' or if you use the batch syntax, which does the same thing, but it actually does not work if you specify <STDIN> for reading.  Try it.
David DelGreco, January 21, 2004 13:52
Thanks to the other posters.  I still haven't mastered the depths of Vim and just didn't think of the modeline.  I'll read up on that.  It's certainly a more direct and reliable of enabling the proper syntax for a file without relying on the extension.

The last poster was closer to what I was talking about (incompletely).  Try this code:

# foo.pl
($file, $p1, $p2) = @ARGV;
if ($file eq '-') {  $s = <STDIN>  }
else {  open F, $file;  $s = <F>  }
print $s;

Then try this:

some_filter.pl  |  foo.pl  -  param1  param2

For some reason, this doesn't work.  Using <> instead of <STDIN> works, and running this works:

some_filter.pl  |  perl foo.pl  -  param1  param2

But not the way I have it.  I suppose tihs may point to a bug in Perl on Windows, but I could also write my scripts differently.  Thanks for some enlightenment on two subjects.

</offtopic>
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo