http://qs321.pair.com?node_id=1195666


in reply to sysread() is deprecated on :utf8 handles

It makes no sense to use sysread on a file handle with the :utf8 layer because the read might have ended mid-character.

sysread-using code should look something like this:

binmode($fh); # Or open with :raw my $buf = ''; while (1) { # Or this could be a select loop. my $rv = sysread($fh, $buf, BLOCK_SIZE, length($buf); die($!) if !defined($rv); last if !$rv; # Identify and extract message. # Using LF-terminated messages in this example. while (s/^([^\n]*)\n//) { my $msg = $1; process_message($msg); } } die("Premature EOF") if length($buf);

In your case, you just want to blindly forward everything you receive, so simply avoid decoding or encoding!

binmode($fh_in); # Or open with :raw binmode($fh_out); # Or open with :raw while (1) { # Or this could be a select loop. my $rv = sysread($fh, my $buf, BLOCK_SIZE, length($buf); die($!) if !defined($rv); last if !$rv; print($fh_out $buf); }

Replies are listed 'Best First'.
Re^2: sysread() is deprecated on :utf8 handles
by mje (Curate) on Jul 21, 2017 at 07:54 UTC

    Thanks ikegami . Some of the other replies ignore the fact you cannot mix any buffered IO with select and as I put on my updates, I was aware of getting a buffer from sysread with part of a UTF-8 sequence. However, I got lost in the issue a bit and missed the obvious which as you point out is that the code doesn't really care what it reads from the socket as it is simply passing it on.