in reply to Re: Perl non-blocking IPC
in thread Perl non-blocking IPC

Thanks for the responses.

By 'receive commands', I just mean that some other program writes a text message to the file that the tk-program is looking at (through the fileevent method). The tk-program then gets a call to the _process_fileevent method, reads the file handle and does whatever it says.

I am attempting to use the 'kill 9' command from your code however I and using the FileHandle module to open the pipe so I don't now how to get the pid of the tail process. Any suggestions?

$fh = FileHandle->new("tail -f -n 25 command_file |");

Thanks again Brian

Replies are listed 'Best First'.
Re^3: Perl non-blocking IPC
by zentara (Cardinal) on Jul 06, 2011 at 18:27 UTC
    I don't now how to get the pid of the tail process. Any suggestions?

    $fh = FileHandle->new("tail -f -n 25 command_file |");

    The only way to get the $pid is to use a piped-open as described in perldoc perlipc, or use something like IPC::Open3, but you sometimes get a shell pid, which then executes your program.

    So use something like this:

    my $fh = new FileHandle; my $pid = open( $fh , "tail -f -n 25 command_file | ") or die ; #..... # now you can check the output of ps to see if you get a shell $pid # or just your process $pid # then to make sure you kill all shells and commands, if you see # the $pid is the shell use Proc::Killfam; $mw->protocol('WM_DELETE_WINDOW' => sub { killfam 9,$pid; exit; }); # or put the killfam in the callback of an Exit button, etc
    Before I get slapped on the wrist for using killfam 9, remember that 9 is an instant kill, you may want to use a softer signal like 15, to allow the program to gracefully close.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thanks to all,

      That works great. I feel kind of guilty, I always seem to be asking for help and never giving (not that I have the expertise) help.

      Thanks again

      Brian

Re^3: Perl non-blocking IPC
by Somni (Friar) on Jul 07, 2011 at 00:23 UTC
    It's been a while since I've seen actual FileHandle usage. You don't need it; assuming you're using a perl newer than 5.00505:

    open(my $fh, '-|', qw( tail -f -n command_file)) || die("Unable to run tail: $!.\n");

    You can pass a single string, but whenever possible, call system(), open(), etc. with a list for the command; it's safer, because you're guaranteed not to get filtered through /bin/sh.