Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: sysread() is deprecated on :utf8 handles

by ikegami (Pope)
on Jul 21, 2017 at 03:19 UTC ( #1195666=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1195666]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2019-12-15 23:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?