in reply to Re: Using Linux Commands in Perl - `Backticks`
in thread Using Linux Commands in Perl - `Backticks`

The OP asked to have it run for 100ms, not 100 lines. Your program may hang for an unknown time, it may be days before you've read 100 lines.

I would open a pipe, use a select loop and sysread to read from the pipe. After 100ms, terminate the loop and the pipe.

  • Comment on Re^2: Using Linux Commands in Perl - `Backticks`

Replies are listed 'Best First'.
Re^3: Using Linux Commands in Perl - `Backticks`
by Corion (Patriarch) on Sep 30, 2010 at 22:16 UTC

    Duh - I misread that. In that case, using select and alarm is likely the best approach.

    Luckily, the question is about Linux, where this approach will work. On Windows, reading from a pipe does not play well with select and/or IO::Select. The approaches of tunnelling the IO through (TCP) sockets (in the Win32 mkpipe emulation) do work for certain cases and horribly fail for other cases, always depending on the program spawned in such a fashion.

      Considering that select() already has a timeout, why would you use alarm?

        I've never used select with a timeout, so that's why I wasn't aware of that additional parameter. As you say, there is no reason to use alarm, as select does everything that's needed to read from a program for a limited amount of time.