Hello Perl Monks,

I have a question regarding a very strange behavior which implies the bytes pragma and two UTF-8 strings.

Bellow is the code which illustrates the problem:

use utf8; use 5.010; use strict; use warnings; binmode(STDOUT, ':encoding(UTF-8)'); sub get_bytes { my ($string) = @_; use bytes; map { bytes::ord bytes::substr($string, $_, 1) } 0 .. bytes::lengt +h($string) - 1; } my $s1 = "møøse"; my $s2 = "m\xF8\xF8se"; say $s1; say $s2; say "Equal: ", $s1 eq $s2; say join(" ", get_bytes($s1)); say join(" ", get_bytes($s2));

The output (with perl-5.22.0):

møøse
møøse
Equal: 1
109 195 184 195 184 115 101
109 248 248 115 101

As shown, the strings are equivalent in UTF-8, but they become different once they are converted into bytes.

Is this a bug or do I miss something important in this conversion? Thanks!

Update: by replacing "m\xF8\xF8se" with decode_utf8(encode_utf8("m\xF8\xF8se")) it seems to work as expected.


In reply to UTF-8 strings and the bytes pragma by trizen

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.