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

"Why" is secondary here (if not tertiary), but I have a small perl script that is a command prompt (looping until the quit statement is issued by the user). It sends commands to different places based on what kind of command they are. I've built some of my own functionality of vi in there using PortableReadKey. I can scroll through the command history and stuff, but I need more of the vi functionality than I care to write (search/replace, insert before, insert after, ...). I think I should be able to use VI by way of an open3 pipe, but I just can't nail it down. I can get some stuff to work but not all. Is there anyone here who's ever been on enough K to have wanted to do something like that? Thanks!

Replies are listed 'Best First'.
Re: VI In A Perl Script
by dsheroh (Monsignor) on Feb 15, 2008 at 16:29 UTC
    I've never wanted to do anything like that, but do you need the vi editing to actually happen within your application? If not, you could do like most text-mode email clients and create a temporary file, fire up an editor session to edit that file, read the temp file back in when the editor closes, and then delete the temp file when you're done with it. This also has the advantage of being easy to set up such that the user can (via config file or environment setting) specify the editor of their choice instead of being hardwired to vi.
      What I've done is set up a wrapper to R, which is command-line driven. In addition to a few other things, I want to add the command-history method that KSH would use at the command-line with vi. So I do want it in my application and not by launching it as you suggested. The other thing I thought of was to use the examples of printing with escape characters that I saw in PortableReadKey. For instance it does : printf("\e[%d;%dH", $y, $x); To move the cursor to an absolute column and row. I figure there should be codes for inserting text rather than overwriting as it does now when I move the cursor around the command line. A couple of things like that and I won't need to call vi, but I can't find them. Thanks.
Re: VI In A Perl Script
by almut (Canon) on Feb 15, 2008 at 17:16 UTC

    As you might know, open3 only allows to interface with another program via the standard file handles (stdin,stdout,stderr), so you'd hit a brick wall rather soon, in case you'd need to go beyond that (e.g. cursor control, or other ncurses-like functionality typically used by text-based UIs).

    Not sure what exactly you're trying to do, but if you're primarily interested in readline functionality, you might want to take a look at Term::Readline.

    You might also just do things the other way round, i.e. make use of vim's Perl interface, and run your commands from there...

Re: VI In A Perl Script
by Joost (Canon) on Feb 15, 2008 at 19:42 UTC
      If I had a dollar for every time someone has said "I've never wanted to do anything like this" I would have had someone do this for me :) I followed you suggestion about the Term::ReadLine::Gnu, but by I found that it didn't work exactly the way I wanted, or I didn't know how to use it. However, this is exactly what I want and I'm using it correctly -> Term::ReadLine::Zoid All vi functionality including an automatic history buffer is contained in it. Thanks guys.