Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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

by slugger415 (Monk)
on Aug 14, 2019 at 17:48 UTC ( #11104470=note: print w/replies, xml ) Need Help??

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

Hi Jeb, not sure, but please see my sample

I'm getting this string returned from a database via SQL

Replies are listed 'Best First'.
Re^5: Text::CSV encoding parse()
by afoken (Canon) on Aug 14, 2019 at 23:03 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". ;-)
Re^5: Text::CSV encoding parse()
by jcb (Vicar) on Aug 14, 2019 at 23:55 UTC

    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". ;-)

        And of course my simple testing code has numerous surprises in edge cases. This seems to be par for the course when character encodings are involved. :-/

        But it might give our questioner some useful bit of data if he cannot get Wireshark working.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2021-01-21 03:02 GMT
Find Nodes?
    Voting Booth?