Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Problem with utf8 after nearly 4096 bytes

by gvieira (Initiate)
on Sep 07, 2013 at 23:07 UTC ( #1052847=note: print w/ replies, xml ) Need Help??


in reply to Problem with utf8 after nearly 4096 bytes

Hi guys,

I'm reviving this discussion because I'm so much closer of the solution.

I finally discovered what is the real problem: Perl only recognizes the first 4096 bytes as utf8 because only the first block of text have the BOM. So he thinks that the first block is utf8 (and it goes ok) but recognizes all the other blocks as Unicode. So if I put a few en dashs ("") I can force the code to interpretate Unicode as utf8. Buuuut, I can't expect that the users to put these en dashs. The program will be used by some oldschool Linguistics researchers, with almost none computer knowledge.

I tryed to force dashs by concatenating each line of the file with an en dash:

while(my $l = <$fh>){ $text .= "".$l; }

But I get this error:

Wide character in print at (eval 12) line 94.

Any idea of how could I force this (or another) character to make perl understand that the block is in utf8? If there's any other way to do it, would be awesome too.

Thanks guys

Vieira


Comment on Re: Problem with utf8 after nearly 4096 bytes
Download Code
Re^2: Problem with utf8 after nearly 4096 bytes
by farang (Hermit) on Sep 08, 2013 at 01:47 UTC

    Perl only recognizes the first 4096 bytes as utf8 because only the first block of text have the BOM. So he thinks that the first block is utf8 (and it goes ok) but recognizes all the other blocks as Unicode.
    That cannot be. Perl does not use BOMs to determine encodings. If a file is opened with :encoding(utf-8) as utf::all does, the entire file is assumed to be in that encoding.

    Here's something to try: get rid of any BOMs completely. In your original code, add this after populating $text.

    $text =~ s/\x{FEFF}//g;
    See this old discussion: UTF-8 text files with Byte Order Mark.

    Also, make sure utf-8 encoding is correctly specified in your HTML header.

      You have a point. Even without BOM the program still recognize that the first block is in utf8. How could he don't think the same of the rest of the file?

      There any way I can print these en dashs on file? Maybe using her hex value?

        You have a point. Even without BOM the program still recognize that the first block is in utf8. How could he don't think the same of the rest of the file?
        That's just it, Perl will threat the whole file as utf-8. It seems you are likely corrupting the file during upload. If you post the relevant code you are using to handle the upload, probably some monks can either see what's wrong with it or give a alternate approach for uploading which avoids the problem.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2014-08-23 10:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (173 votes), past polls