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

Hi, I am very new to perl and I am having the following requirement. how do i run "vim" from perl program to accept input from stdin and to output on to stdout. I am able to do this using KSH, but need to convert the same into perl. Any pointers are most welcome. Thanks Ravi

2004-11-30 Edited by Arunbear: Changed title from 'a small requirement', as per Monastery guidelines

Replies are listed 'Best First'.
Re: How to launch vim from Perl
by insaniac (Friar) on Nov 30, 2004 at 11:41 UTC
    why do you need vim for stdin and stdout? just use:
    $a = <STDIN>; # This is one way of how you read user input print $a, "\n"; # This is one way of printing to stdout

    or if you want achieve something like this cat some_file | some_perlscript.pl, then you could write something like this in your script:
    while(my $line = <>) { # reads from stdin print $line; # prints everything to stdout }

    but if you really want to start vim from a perl script, just use the backticks:
    `vim $some_file`;

    maybe if you gave us some more specs, we'd understand you better ;-)
    --
    to ask a question is a moment of shame
    to remain ignorant is a lifelong shame
      `vim $some_file` is not correct. The backticks attempt to capture the stdout of the program, which in this includes the interface, so launching vim pointless, because the interface isn't displayed and therefor is unusable. In this case, you want to use: system "vim"; which will allow vim to display it's own STDOUT and read from STDIN.

      As for reasoning, many nix type command line scripts need a fair amount of text input, so much that typing it all in to stdin, with the associated problems of editing, would be too much hassle. So you launch vim pointed at a temporary file, wait for them to type the necessary data, save it, and quit, then your program resumes and you read the file.
        So you launch vim pointed at a temporary file
        I prefer to lauch $ENV{EDITOR}. Use vim if the user doesn't have this set, but if the user has gone to the trouble to set her editor to emacs, don't force vim down her throat.

        thor

        Feel the white light, the light within
        Be your own disciple, fan the sparks of will
        For all of us waiting, your kingdom will come

        ah, you're right :-)
        i was too impulsive in my reply, so i hadn't tested what i said... shame on me :'(
        --
        to ask a question is a moment of shame
        to remain ignorant is a lifelong shame
Re: How to launch vim from Perl
by jaldhar (Vicar) on Dec 01, 2004 at 01:53 UTC