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

I need a perl script to fork another program and wait for it to finish, while monitoring for keyboard input (to allow the user to abort and kill the forked process)

What is the easiest technique to use? A real simple example would help. This is on Linux. Thanks!

Replies are listed 'Best First'.
Re: how to implement ipc on linux?
by etcshadow (Priest) on Feb 16, 2005 at 03:53 UTC
    FYI, terminology-wise, what you're talking about is often called a "co-process". A great tool for working with co-processes is IPC::Run. It can work in a "wait for the other process to finish" mode (just run the command), or in a mode that allows you to start up a co-process, and pump it along in the controller process.

    Oh, another tool for co-processes is Expect.

    ------------ :Wq Not an editor command: Wq
      wow I sure found a lot of documentation on that IPC::Run module, but I didn't see exactly what I needed. Maybe what I need is hidden amongst all the complicated examples?

      All I need is this:

      system(command) until (done or user_aborted)
      thanks!
Re: how to implement ipc on linux?
by zentara (Cardinal) on Feb 16, 2005 at 13:49 UTC
    If you are using X, and are willing to use Tk, there is the easy-2-use Tk::Execute::Command module.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::ExecuteCommand; my $mw = MainWindow->new; my $ec_dir = $mw->ExecuteCommand( -command => 'dir; sleep 10; dir;', -entryWidth => 50, -height => 10, -label => '', -text => 'Execute dir ', )->pack; my $ec_date = $mw->ExecuteCommand( -command => 'date; sleep 10; date;', -entryWidth => 50, -height => 10, -label => '', -text => 'Execute date ', )->pack; my $dir_but = $mw->Button( -text => 'Execute dir', -background => 'hotpink', -command => sub{ $ec_dir->execute_command })->pack; my $date_but = $mw->Button( -text => 'Execute date', -background => 'lightgreen', -command => sub{ $ec_date->execute_command })->pack; MainLoop;

    I'm not really a human, but I play one on earth. flash japh
      thanks for the excellent example (and I am using Tk now!) but I don't really need all those unecessary widgets on the screen (like stdout).

      I just need something like (pseudocode)

      system(command) until (done or user_aborted_with_keypress) quit if done; goto next step if user hit 'N' goto previous step if user hit 'P'
      I could use Tk if it would be more sparse on the interface tho...

      thanks!

        I don't have an example handy, but Tk::ExecuteCommand has "advertised subwidgets", which means you can access the various components separately, and change their appearance. So you could make the stdout textbox, nearly invisible. For that matter, you could run it with the window "withdrawn", to make it totally invisible. You also could setup custom key bindings, to call the same "Quit" subroutine, as the Execute/Stop button has. Read perldoc Tk::ExecuteCommand and look for the "ADVERTISED SUBWIDGETS" section. You possibly could do something like:
        my $rotext = $ec->Subwidget(ROText); $rotext->configure(-height => 0);
        and you could add key bindings, like
        $text->bind('<Control-r>',sub{$ec->kill_command });
        Of course, this only works from X, if you want it to work from the command line, try POE; maybe the POE Cookbook has some examples.

        I'm not really a human, but I play one on earth. flash japh