Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^3: Text::CSV encoding parse()

by jcb (Parson)
on Aug 14, 2019 at 03:24 UTC ( #11104428=note: print w/replies, xml ) Need Help??

in reply to Re^2: Text::CSV encoding parse()
in thread Text::CSV encoding parse()

That means that you are declaring to the browser that your output is UTF-8. Is it actually UTF-8?

Replies are listed 'Best First'.
Re^4: Text::CSV encoding parse()
by slugger415 (Monk) on Aug 14, 2019 at 17:48 UTC
      not sure

      Get information. If you have no better idea, use the dumpstr() function in t/ from DBD::ODBC. Just copy the few lines into your code and print its result for each string that should be UTF-8.


      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Seconding afoken here — if you do not know, find out!

      Try the hexdump function from this sample: (lightly tested, hopefully correct)
      #!/usr/bin/perl use strict; use warnings; # given: string of bytes # return: hexdump of argument sub hexdump ($) { use bytes; my @bytes = map {[$_, ord]} split //, shift; return '['.join(' ', map {sprintf('%02x', $_->[1])} @bytes).']' .'|'.join('', map { ($_->[1] >= 0x20 && $_->[1] < 0x7F) ? $_->[0] : '.' } @bytes).'|' } use utf8; my $text = q[search/¿Cuales son las partes de una cadena de conexión?? +scope]; print hexdump($text), "\n"; __END__

      Sample output:

      [73 65 61 72 63 68 2f c2 bf 43 75 61 6c 65 73 20 73 6f 6e 20 6c 61 73 +20 70 61 72 74 65 73 20 64 65 20 75 6e 61 20 63 61 64 65 6e 61 20 64 +65 20 63 6f 6e 65 78 69 c3 b3 6e 3f 3f 73 63 6f 70 65]|search/..Cuale +s son las partes de una cadena de conexi..n??scope|

      I am fairly sure that if hexdump dies, the string you gave it was definitely not UTF-8. :-)

        A simple hex dump of bytes is not always sufficient.

        Perl scalars can store strings in two ways: legacy, using characters that match bytes, and Unicode, using characters that can have values larger than 255. In the first case, the unicode flag (unfortunately called "the UTF8 flag") is cleared, in the second case, it is set. You can query it using the core function utf8::is_utf8($scalar). More details: utf8, perlunifaq

        Things get interesting when encoding and decoding happens, as the outside world usually talks bytes, not characters. If you handle all data as binary, "Unicode in, Unicode out" seems to work magically, as long as you don't touch the bytes. Perl does not see Unicode, but a string of bytes. The difference is subtile, but can cause huge problems. length('Bär') is suddenly not 3, but 4 (UTF-8), 6 (UTF-16) or 12 (UTF-32). You can't match the 'ä' in 'Bär'. And so on. See Match full utf-8 characters for a real-world example.

        The correct way is, of course, to handle encodings correctly. That way, perl decodes input to strings of characters, and encodes those strings of characters back on the way to output. length('Bär') will always be 3, you can match the 'ä' in 'Bär', and so on.

        To see if all encoding and decoding happened as expected, you have to look at the unicode flags of the scalars. Comparing actual length and expected length is also useful. Especially when working not only with STDIN and STDOUT, but e.g. also with a database interface, you really want to know if you have successfully read and written a string as Unicode or just happened to get the same bytes back as you wrote, and the other side saw just Mojibake. That's the main reasons why t/40UnicodeRoundTrip.t, t/41Unicode.t, and t/ in DBD::ODBC exist. Let me tell from experience gained while developing the initial Unicode patch for DBD::ODBC: It is really easy to mess up the encoding and decoding.

        Oh, and just a little extra: CGI does NOT change STDOUT to use UTF-8 encoding when using the -utf8 pragma. It also does NOT change STDOUT when you send a document encoded as UTF-8. You have to do so manually (binmode STDOUT,':encoding(utf-8)'). If you find "wide character in print" warnings in the web server's error log, you have your string encodings right, but STDOUT was not set to UTF-8. If you don't see those warnings, with warnings enabled and no binmode STDOUT, your strings most likely aren't encoded properly.


        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11104428]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2021-08-06 04:13 GMT
Find Nodes?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)

    Results (44 votes). Check out past polls.