nzgrover has asked for the wisdom of the Perl Monks concerning the following question:
The above works but I not confident it is the best way of doing this or even that it will be robust in the real world. Any enlightenment much appreciated!my $result; my $named_pipe_name = '/path/to/pipe'; my $timeout = 5; #check to see if named pipe exists if (-p $named_pipe_name) { #fork in such a manner as to enable us to get out what the child # reads from the NP if (my $childpid = open(FROMCHILD, "-|")) { #parent #set up the a SIGCHLD handler that will read what the child has # written local $SIG{CHLD} = sub { $result .= <FROMCHILD>; }; #wait for our timeout sleep ($timeout); #kill the child (if it still exists) kill 'HUP', $childpid; } else { #child #Open for reading only. note using sysopen without create flag # to prevent creation of the named pipe as a normal file if (sysopen(FIFO, $named_pipe_name, O_RDONLY)) { my $return_string; #read from the name pipe till EOF while(my $this_line = <FIFO>) { chomp($this_line); $return_string .= $this_line; } close(FIFO); #tell the parent what we read print STDOUT $return_string; } else { print STDOUT "ERROR: Reading $named_pipe_name: $!"; } #exit causes the SIGCHLD to be sent. exit; } } print STDOUT $result;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Reading from a Named Pipe
by ikegami (Patriarch) on Sep 28, 2006 at 03:21 UTC | |
by nzgrover (Scribe) on Sep 28, 2006 at 21:28 UTC | |
|
Re: Reading from a Named Pipe
by cdarke (Prior) on Sep 28, 2006 at 07:43 UTC | |
by revdiablo (Prior) on Sep 28, 2006 at 15:43 UTC | |
by nzgrover (Scribe) on Sep 28, 2006 at 20:42 UTC | |
|
Re: Reading from a Named Pipe
by revdiablo (Prior) on Sep 28, 2006 at 04:20 UTC | |
by nzgrover (Scribe) on Sep 28, 2006 at 20:28 UTC |