tags:

views:

50

answers:

2

I've noticed a couple other questions about handling this, but all seem to suffer from:

  • Not working on windows
  • requiring the child to finish before retrieving output

What I would like to do is call a program (eg. tshark.exe) and process its output while it runs.

To date I have tried:

  • Backticks
  • Run3
  • Proc::Reliable

all without any success. I could spend all day trying and failing to find a module which helps me with this (ie. I have spent all day), but I figured it might be better if I just asked if anyone knew of one.

A: 

Check out IPC::Run. IPC::Open2 and IPC::Open3 might meet your needs too.

Good luck!

jsegal
+4  A: 

You don't need a module. Just learn about the pipe forms of the open command -- these work just fine on Windows.

my $pid = open (my $cmd_handle, "tshark.exe @options |");
# on success, $pid holds process identifier of the external command.

while (<$cmd_handle>) {
    # sets $_ to next line of output.
    # Will block until a line of output is ready.
    # Is  undef  when the command is complete.

    ... process $_ ...
}
close $cmd_handle;   # waits for command to complete if it hasn't completed yet
mobrule
Preferred "modern" incantation is `open my $handle, '-|', $command`. With pipes I sometimes get confused when to use `-|` and when to use `|-`.
mobrule
You have no idea how much stress you relieved with this answer. Thank you.
tzenes
+1 but you should check for errors on `close`.
Sinan Ünür