Often I have a task to read/write data to/from pipes without perl buffered IO (because same pipes used in select()). AFAIK this should be implemented via sysread/syswrite, and sysread/syswrite are documented to sometimes return less data, than available and sometimes return EINTR (let's talk about blocking pipes for now). So every time I end up with the following wrappers:
# args: ($file, $buffer, $length) # returns data in $buffer and number of bytes read, or undef on EOF sub sysreadfull { my ($file, $len) = ($_[0], $_[2]); my $n = 0; while ($len - $n) { my $i = sysread($file, $_[1], $len - $n, $n); if (defined($i)) { if ($i == 0) { return $n; } else { $n += $i; } } elsif ($!{EINTR}) { redo; } else { return $n ? $n : undef; } } return $n; } # args: ($file, $buffer) # returns number of bytes actually written sub syswritefull { my ($file, $len) = ($_[0], length($_[1])); my $n = 0; while ($len - $n) { my $i = syswrite($file, $_[1], $len - $n, $n); if (defined($i)) { $n += $i; } elsif ($!{EINTR}) { redo; } else { return $n ? $n : undef; } } return $n; }
Question is: It's should be very common problem for everyone who deals with IPC or network programming, why nobody wrote CPAN module with such wrappers? Maybe because there is easier way ? Maybe ":raw" handles? I saw similar code only here (but for non-blocking), but seems it has bug

In reply to sysread/syswrite wrappers by vsespb

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.