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.

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.

-nuffin
zz zZ Z Z #!perl

In reply to Re: sockets and such in Perl by nothingmuch
in thread sockets and such in Perl by scotchfx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.