good chemistry is complicated,
and a little bit messy -LW
Reading partial lines/strings from a pipeby slightly72 (Initiate)
|on Oct 10, 2007 at 17:46 UTC||Need Help??|
slightly72 has asked for the
wisdom of the Perl Monks concerning the following question:
I am trying to write some Perl code to communicate with a server. The issue here is that the server replies with strings (i.e. not full lines) -- for example, a query for some port number is answered with "XXXX" and not "XXXX\n" (or any other line/string terminator). I am using open2 to launch the server:
Then at some point I write the command to the stdin of the server (now piped through $wrt). The problem is with reading from the stdout of the server, piped through $rdr.
If I try:
my $reply = <$rdr>;
the program deadlocks since a full line is expected but never arrives.
I've tried to read character by character from the file (it's not a large performance penalty since the replies are quite short, at most 8-10 chars long) -- however, I don't know when to stop reading these characters, if I'm trying to read over the length of the reply, I get deadlocked again.
The replies are in no particular format or fixed length.
Ideally, I would like to handle the reply with something like:
I have looked into File::Tail, but that seems to work only for regular files and not pipes. I've looked also into using threads, as per a reply to a similar problem on perlmonks, but again the issue is that that solution worked with 'records' (i.e. usually lines).
The server is proprietary software, so I can't get into the sources to modify the replies.
Right now, I'm at a loss for a solution, so any help is really appreciated. Oh, and if the solution is not too OS dependent (my script should work on both Linux and Windows) it would be terrific.
Thank you in advance,