Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Simplest Possible Way To Disable Unicode

by Anonymous Monk
on May 23, 2011 at 23:55 UTC ( #906384=note: print w/replies, xml ) Need Help??


in reply to Simplest Possible Way To Disable Unicode

$ perl -e " print chr(999) " Wide character in print at -e line 1. ϧ $ perl -e " binmode STDOUT; print chr(999) " Wide character in print at -e line 1. ϧ $ perl -Mdiagnostics -e " print chr(999) " Wide character in print at -e line 1 (#1) (S utf8) Perl met a wide character (>255) when it wasn't expecting one. This warning is by default on for I/O (like print). The eas +iest way to quiet this warning is simply to add the :utf8 layer to the output, e.g. binmode STDOUT, ':utf8'. Another way to turn off the warning is to add no warnings 'utf8'; but that is often closer to cheating. In general, you are supposed to explicitly mark the filehandle with an encoding, see open and perlfunc/binmode. ϧ
So to silence the warning, use
$ perl -e " no warnings q[utf8]; print chr(999) " ϧ
using bytes would also silence it, but would change the semantics
$ perl -e " use bytes; print chr(999) " τ
perluniintro, perlunicode

Replies are listed 'Best First'.
Re^2: Simplest Possible Way To Disable Unicode
by ikegami (Pope) on May 24, 2011 at 00:37 UTC

    Simply silencing the warning is not the solution.

    print chr(200); print chr(1000);

    will continue to be different than

    print chr(200) . chr(1000);

    as shown here:

    >perl -we"no warnings qw( utf8 ); print chr(200); print chr(1000);" | +perl -nE"say length;" 3 >perl -we"no warnings qw( utf8 ); print chr(200) . chr(1000);" | perl +-nE"say length;" 4

    Update: Mistakenly used 100 instead of 200 originally.

      Simply silencing the warning is not the solution.

      So what is the solution?

Re^2: Simplest Possible Way To Disable Unicode
by ikegami (Pope) on May 24, 2011 at 01:21 UTC

    I don't see when use bytes; would possibly be useful to solve this issue. Instead of print warning and returning the possibly useful UTF-8 encoding of the input, it simply destroys the high bits of the bad data.

    $ perl -we'use bytes; $_ = chr(1000); print;' | od -t x1 0000000 e8 0000001 $ perl -we'use bytes; $_ = chr(232); print;' | od -t x1 0000000 e8 0000001

    Whereas the original situation had a chance of providing something useful, this isn't the case with use bytes;. It possibly make things worse and hides the error.

      I don't see when use bytes; would possibly be useful to solve this issue.
      Agreed. It’s not even clear to me what sort of problem it would solve.

      I’ve never been fond of od output myself. And you shouldn’t need it, either. This tells the story clearly enough:

      % perl -wle 'print ord do { use bytes; chr(1000) }' 232 % perl -wle 'print ord do { use bytes; chr(232) }' 232 % perl -wle 'print ord do { no bytes; chr(1000) }' 1000

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2021-04-18 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?