People have mentioned
select.
I think the issue you are sufferring from is that you want nonblocking IO, and you have blocking IO.
The first thing to do is to read MJD's article Suffering from Buffering?.
Once you get a handle on the issues involved, look into setting nonblocking IO modes on the filehandles of the sockets. This is an unwieldly task to get right - it takes a lot of fidgeting and fumbling, but eventually you start getting a feel for what's happenning.
Once you think you can solve it with nonblocking IO and select - stop. Don't do it. It's been done. Look into Event which can help you get callbacks for each named passed, and will allow you to read from the input of the file.
I think I also have a simpler solution, without any of these issues.
- The first process in the pipeline is the simplest - it reads a file and prints it to a socket
- The second and third processes are the same
- They read a line from the socket, and then read lines from the file
- They stop if the line read from the file handle is equal to the line read on the socket, in which case the line is printed to it's printing socket
- or the next line is alphabetically after the line read from the socket, in which case the next line is read from the socket
- The third process will simply stop when it finds a match, instead of rereading
In a unix pipeline this should be something like:
perl -pe1 file | \
perl -e 'open my $fh, "<", shift; my $line; FH: { $line = <$fh>; my $i
+n; { $in = <STDIN>; redo if $in lt $line }; print $line if $line eq $
+in; redo }' | \
perl -e 'open my $fh, "<", shift; my $line; FH: { $line = <$fh>; my $i
+n; { $in = <STDIN>; redo if $in lt $line }; if ($line eq $in) { print
+ $line; exit }; redo }'
To translate to socketspace all you need is to replace STDIN/STDOUT with sockets opened to the right place.
Note that the code is untested.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.