Just learning the new unicode in perl (jumping from 5.6.1 to 5.8.2), I was indeed confused with the concepts. Investing more does reveal a bug, I believe, but it could also be another misunderstanding.

Below is the shortest program that shows the behaviour. I do think it is a bug in the Text::CSV_XS module together with perl 5.8.2.

If I run the program without options, using simple IO::File methods, the string is recognized as utf8. Running it with '-c, using CSV_XS, the string is converted correctly, but not recognized or marked as utf8. Running it with '-cf' to force the utf8 flag on, results in decent behaviour again.

The program expects as input a little file encoded in latin1 or windows-1252. I tested with a file with e-acute/e-acute/n/newline.

#!/usr/bin/perl # expect as input file named "file" with these contents: # $ hexdump file # 00000000 e9 e9 6e 0a use Getopt::Std; use IO::File; use Text::CSV_XS; use Encode; binmode(STDOUT, ":utf8"); # -c = use CSV_XS instead of simple IO # -f = force utf8 flag on getopts('cf'); my $io = IO::File->new(); $io->open("file", "<:raw:encoding(cp1252)") || die("$0: open inputfile: $!\n"); my $csvin = Text::CSV_XS->new({ 'binary' => 1 }); if ($opt_c) { $cols = $csvin->getline($io); $s = $$cols[0]; } else { $s = <$io>; chomp($s); } Encode::_utf8_on($s) if ($opt_f); print "[$s] ", utf8::is_utf8($s) ? "Is UTF8" : "NOT utf8", " ", utf8::valid($s) ? "Is valid" : "NOT valid"; print "\nLENGTH: ", length($s), " SUBSTR3: [", substr($s,0,3), "]", " SPLIT: [", join(" ", split(//, $s)), "]\n";

In reply to Re: Re: substr on utf8-strings by Anonymous Monk
in thread substr on utf8-strings by Anonymous Monk

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.