in reply to Using Linux Commands in Perl - `Backticks`

See the pipe-form of open:

my $cmd = 'netcat localhost 12345 |'; my $pid = open my $netcat, $cmd or die "Couldn't spawn [$cmd]: $!"; for (1..100) { my $line = <$netcat>; print $line; }; END { # Clean up leftover processes kill 9 => $pid if $pid; };

Replies are listed 'Best First'.
Re^2: Using Linux Commands in Perl - `Backticks`
by JavaFan (Canon) on Sep 30, 2010 at 21:47 UTC
    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.

      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?
Re^2: Using Linux Commands in Perl - `Backticks`
by ThirtySecondNoob (Novice) on Sep 30, 2010 at 23:20 UTC

    Thank you so much... This works perfectly! I originally wanted to get one line, parse it, and move on to the next line, so I had thought that there was a netcat -option that would have it timed for you. This works much better, and I can get to the good stuff for the code now.

    Hopefully I can get some practice piping now, too. :)