in reply to Reading from a pipe
You have to close the writing end of the pipe in the parent. The fork duplicates the pipe's handles, and the readline (i.e. my @val = <READ>) doesn't stop reading until the pipe is fully closed (i.e. in both child and parent).
This should work:
... sub _Forked { my $ip = shift; pipe (READ,WRITE); die "Couldn't fork" unless defined (my $pid = fork()); if ($pid == 0) { # CHILD my @ping = `ping $ip -w 2 -q | sed -n '\$p'`; close (READ); select WRITE; print "@ping\n"; close (WRITE); # exit (0); } # close WRITE; # <--- return ($pid); } ...
Note that you have to move the creation of the pipe into the _Forked() routine (otherwise you'd have a dysfunctional pipe with a closed writing end upon subsequent calls of the routine...).
That said, I'm not sure why you're using an array here, as you seem to be outputting the last line of the ping only, anyway. But maybe this is just an oversimplified example.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Reading from a pipe
by hello_world (Acolyte) on Nov 20, 2011 at 13:51 UTC | |
by Eliya (Vicar) on Nov 20, 2011 at 14:18 UTC | |
by hello_world (Acolyte) on Nov 20, 2011 at 15:13 UTC | |
by hello_world (Acolyte) on Nov 20, 2011 at 15:27 UTC |