When I used Encode::_utf8_off I only intended to turn UTF8 bit off, without any reencoding, so to avoid "wide char" error

I don't believe that you didn't care what it produced as long as the warning went away. As for what you wanted to produce, you stated you wanted UTF-8. ("binmode=>':utf8'").

As far as I see, utf8::encode($foo) also simply turns UTF8 bit off, so effect of these two calls is the same, (unless I am misunderstandiong)

$ perl -MEncode=_utf8_off -E'$_="\xC9ric"; _utf8_off($_); say;' ?ric $ perl -E'$_="\xC9ric"; utf8::encode($_); say;' Éric

The message said you had non-bytes in your string. The solution is to encode your text into bytes. The problem has nothing to do with the UTF8 flag. As you've found out, that can't be done via the binmode parameter, so you have to do your own encoding. This has noting to do with the UTF8 flag, and doesn't require any mucking into Perl internals.

Yet 'Encode' module is huge, so it is overkill also.

That's why I used utf8::encode instead of Encode::encode_utf8. That and the fact utf8::encode has the same calling convention as _utf8_off.

_utf8_on and _utf8_off should never ever be called. If you do need to muck with the internals to work around some XS bug, utf8::upgrade and utf8::downgrade will help.


In reply to Re^5: can not use utf8 with File::Slurp by ikegami
in thread can not use utf8 with File::Slurp by vkon

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.