Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl Unicode internals - mess with utf8
    primarykey
    data
    text
    <p>Before anyone will tells me to RTFM, I must say - I have digged through:</p> <ul> <li><a href="https://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default">Why does modern Perl avoid UTF-8 by default?</a></li> <li><a href="https://stackoverflow.com/q/3735721/632407">Checklist for going the Unicode way with Perl</a></li> <li><a href="https://stackoverflow.com/questions/7429964/how-to-match-string-with-diacritic-in-perl">How to match string with diacritic in perl?</a></li> <li><a href="https://stackoverflow.com/questions/6412799/perl-how-to-make-use-mydefaults-with-modern-perl-utf8-defaults">How to make &quot;use My::defaults&quot; with modern perl &amp; utf8 defaults?</a></li> <li>and many others (like <a href="http://perldoc.perl.org/perluniintro.html" rel="nofollow noreferrer">perluniintro</a> and others) - but - <em>sure</em>, missed something</li> </ul> <p>So, the basic code:</p> <pre><code>use 5.014; #getting 'unicode_strings' feature use uni::perl; #turning on many utf8 things use Unicode::Normalize qw(NFD NFC); use warnings; while(&lt;&gt;) { chomp; my $data = NFD($_); say "OK" if utf8::is_utf8($data); } </code></pre> <p>At this point, <em>from</em> the <em>utf8</em> encoded STDIN I got a correct <em>unicode</em> string in <code>$data</code>, e.g. "\w" will match multibyte <code>[\p{Alphabetic}\p{Decimal_Number}\p{Letter_Number}]</code> (maybe something more). That's ok and works.</p> <p>AFAIK <code>$data</code> does <strong>not</strong> contain utf8, but a string in <code>perl's internal Unicode</code> format.</p> <p>Now the questions:</p> <ul> <li>HOW can I ensure (test it), that any <code>$other_data</code> contains valid Unicode string?</li> <li>For what purpose is the utf8::is_utf8($data)? The whole <a href="http://perldoc.perl.org/utf8.html" rel="nofollow noreferrer">utf8</a> pragma is a mystery for me.</li> </ul> <p>I understand that the <code>use utf8;</code> is only for the purpose of telling Perl that my source code is in utf8 (so do similar things as when my script starts with BOM flag - for BigEndian) - from Perl's point of view, my source code is like an external file - and Perl should know in what encoding it is...</p> <p>In the above example <code>utf8::is_utf8($data)</code> will print OK - but I don't understand WHY.</p> <p>Internally Perl does not use utf8, so my utf8 data-file is converted into Perl's internal Unicode, so why does the <code>utf8::is_utf8($data)</code> return true for <code>$data</code>, which is <em>not</em> in utf8 format? Or it is misnamed and the function should be named as uni::is_unicode($data)???</p> <p>Thanks in advance for clarification.</p> <p>Ps: @brian d foy - yes, I still don't have the <em>Effective Perl Programming</em> book - I will get it - I promise :) /joking/</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload