in reply to Re^2: can not use utf8 with File::Slurp
in thread can not use utf8 with File::Slurp

I didn't say it was overkill; I said it was wrong. It is not the purpose of _utf8_off to encode to UTF-8, and it will only do so for some inputs.

Replies are listed 'Best First'.
Re^4: can not use utf8 with File::Slurp
by vkon (Curate) on Feb 15, 2011 at 21:44 UTC
    When I used Encode::_utf8_off I only intended to turn UTF8 bit off, without any reencoding, so to avoid "wide char" error

    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)

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

      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.

        _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.

        well, that's certainly useful.
        I'll update my tool bag.

        thanks for all the advices!
        :)