vsespb has asked for the wisdom of the Perl Monks concerning the following question:
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:
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# 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; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: sysread/syswrite wrappers
by BrowserUk (Patriarch) on Oct 12, 2016 at 13:32 UTC | |
by vsespb (Chaplain) on Oct 12, 2016 at 13:41 UTC | |
by BrowserUk (Patriarch) on Oct 12, 2016 at 14:20 UTC | |
by vsespb (Chaplain) on Oct 12, 2016 at 14:44 UTC | |
by BrowserUk (Patriarch) on Oct 12, 2016 at 15:10 UTC | |
| |
Re: sysread/syswrite wrappers
by choroba (Cardinal) on Oct 12, 2016 at 11:57 UTC | |
by vsespb (Chaplain) on Oct 12, 2016 at 12:58 UTC | |
Re: sysread/syswrite wrappers (select)
by tye (Sage) on Oct 12, 2016 at 13:08 UTC | |
by vsespb (Chaplain) on Oct 12, 2016 at 13:27 UTC |
Back to
Seekers of Perl Wisdom