Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hi folks,

It seems to me that mod_perl2 isn't doing the right thing wrt utf8.

This seems unlikely to me (it's widely used software), so I'd appreciate a second opinion.

Here are two ways of constructing a string containing the unicode character U+00B4 (ACUTE ACCENT):

binmode STDOUT, ':utf8'; my $strA = "Bob\x{B4}s files"; use Encode; my $strB = Encode::decode_utf8("Bob\xC2\xB4s files"); say "strA is $strA"; say "strA is $strB"; say "strings are " . (($strA eq $strB) ? "eq" : "not eq"); say "strA flag is: ".Encode::is_utf8($strA); say "strB flag is: ".Encode::is_utf8($strB);

Because the U+00B4 is within latin1 perl is willing and able to treat strA as a byte-string (no utf8 flag set) as per perldoc perlunicode docco for \x: for characters under 0x100, note that Perl may use an 8 bit encoding internally, for optimization and/or backward compatibility.

If I have a mod_perl2 handler which just sends text/plain utf8 content and send those two strings via $r->print then I see different results in the browser (strA doesn't render correctly). Note that $r->binmode seems to do nothing.

To me, this is a mod_perl bug. In perl, the strings are equivalent. It seems mod_perl2 is ignoring the is_utf8 flag and only sending the raw internal representation (latin1 for strA and utf8 for strB).

That said, this has surely been gone over before, so could someone please put me straight.

(Workaround 1 - avoid use of \x escapes and either use decode_utf8 to build string literals or the use utf8; pragma and literal utf8 sequences in your source.)

(Workaround 2 - if your strings come from an external source (e.g. Data::Dumper) you can do:

if (!Encode::is_utf8($str) && $str =~ m/\x80-\xff/) { $str = Encode::encode_utf8($str); # Get utf8 byte seq Encode::_utf8_on($str); # and flip the flag }

But that's fugly since it assumes loads about internals and has a runtime cost which isn't always fun.


In reply to mod_perl2 and utf8 by jbert

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (7)
    As of 2014-07-11 03:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (217 votes), past polls