$! contains error message in locale's language and encoding as bytes, it isn't decoded into Unicode scalar:

$ LANG=ru_RU.UTF-8 perl -CS -MEncode=decode -E ' sub show { printf "is_utf8=%d, length=%d, msg=%s\n", utf8::is_utf8($_), length, $_; } $_=$!=111; show(); $_=decode("UTF-8", $_); show(); ' is_utf8=0, length=40, msg=Ð Ñоединении оÑказано is_utf8=1, length=21, msg=[here should be correct Russian "connection +refused" but perlmonks's parser turn it into html entities]

So, when we've nice Unicode-enabled application, which correctly write Unicode messages into UTF-8 log files, and then we get some error and try to write $! into log we get junk instead of error message.

It's clear what it's nearly impossible to manually decode('UTF-8') in each and every place we get $! - especially because this error may happens inside 3rd-party module. So, is it possible to somehow fix/workaround this issue without forcing LANG=en_US.UTF-8?


In reply to encoding for $! by powerman

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.