Forsaken has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks of Infinite Wisdom,

yet another question from a humble acolyte...

when I have a socket connection open, for some reason I am unable to print() to the filehandle concerned while another thread is doing a <> on the same filehandle. Makes sense, in a way, but it really messes up my evil plot :/ Using syswrite() as opposed to print will do the trick but this means mixing buffered and non-buffered IO and all the docs tell me this isn't the smartest of plans.

Ideas, suggestions, flames?

Replies are listed 'Best First'.
Re: filehandle/socket question (seek)
by tye (Sage) on Jul 18, 2004 at 17:08 UTC

    You are supposed to call seek between calls that use the same file handle in different ways (either one reading and one writing or one buffered and one unbuffered). seek($fh,0,0) in all the right places should fix your problem.

    - tye        

      the problem is that the reading and writing happen in separate threads, hence making "between calls" a very obscure concept in this context :-) situations might occur where reads and writes actually happen at the very same moment, in fact, it probably happens all the time the way the script is set up. It *seems* to be working fine though, but since all the docs advised against I thought I'd better go and ask if there's a more proper approach.

        In that case, dup(2)ing the file handle is probably a better idea:

        open IN, "<&SOCKET" or die ...;

        though selecting a suitable non-bareword file handle scheme is even better.

        my $in = ...; open $in, "<&".fileno($sock) or die ...;

        Where the first "..." depends on what you find appropriate. "undef" works for modern versions of Perl. Or you can use IO::Handle or other module or just do { local(*IN); \*IN }.

        - tye        

Re: filehandle/socket question
by pg (Canon) on Jul 18, 2004 at 16:53 UTC

    This doesn't matter. You will have a problem if you do non-buffered and buffered read at the same time, or non-buffered and buffered write at the same time. But a mixture of non-buffered write with buffered read, not a problem at all. Internally, there are two un-related pools.