dimar has asked for the wisdom of the Perl Monks concerning the following question:


Can someone provide a very simple proof-of-concept perl sample that works as a text filter for (g)Vim?


H is editing text with Vim. Vim can filter a region of text using (among other things) the built-in Vim-script language or an external program. H already has a trusty arsenal of perl scripts that can do various text-filtering operations, and H wants to use those instead of essentially re-writing them all in Vim-speak.


H is able to get Vim filtering to work with other external programs, but not with perl. For example:

!sort <-- this Vim command calls a binary, OK ! <-- this Vim command calls a perl script, FAIL
The root of the problem appears to be that H cannot figure out how to get the input from Vim through STDIN.

Proposed Solution:

H simply wants to see a bare-bones simple perl script that can filter text successfully from Vim (just like !sort can do) just to prove that it can indeed be done. It doesn't have to be sort, just something simple like:

#!/usr/local/perl ### my $sTest = ''; while(<STDIN>){$sTest .= $_;} print " _IT_WORKED_ $sTest _IT_WORKED_ ";

NOTE: The previous code might as well be pseudocode, because it is not working. When run from Vim, the target text is not filtered, but simply blanked out. The _IT_WORKED_ tags do, however, appear.
NOTE: H has not been able to find any examples in the usual search venues.
NOTE: Alternate solution proposals are welcome, but see Constraints.



Update:Thanks for all the suggestions, unfortunately none of them worked on Windows, this seems to be a problem with inter-process communication between Vim and STDIN on Windows ... still looking for a solution
Update:Bingo. bmann and Re: Batch files in Win/Perl helped re-emphasize the long-known (yet forgotten) Windows bug ... pl2bat has a solution. Knee-jerk nauseated aversion to the words 'MSDOS' and 'batch' caused repressed memory syndrome...

20050219 Edit by castaway: Changed title from 'HOWTO: A simple, perl-based text filter for Vim'