Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Problem with utf8 after nearly 4096 bytes

by gvieira (Initiate)
on Sep 02, 2013 at 05:41 UTC ( #1051888=note: print w/replies, xml ) Need Help??

in reply to Problem with utf8 after nearly 4096 bytes

I'm not sure if I understood your advice, but I changed this:

undef $/; my $text = <$fh>;

For this:

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

But I still get the same result. Is that what you mean by read the file line by line? When I did this kind of read on "pure Perl" I used:

open FL, "<"."file.txt"; binmode(FL, ":utf8");

And it worked fine, but now I'm sending the file from a html page(<input type="file"...) so I don't know any other way to get the file handle besides that. Thanks for the help and sorry about the stupidity, I'm just a beginner on perl programming.

Replies are listed 'Best First'.
Re^2: Problem with utf8 after nearly 4096 bytes
by McA (Priest) on Sep 02, 2013 at 07:34 UTC


    it's just a guess from my side what "Anonymous Monk" wanted to avoid with his recommendation. When you read a file block wise it could happen that the last byte in your buffer is the first byte of a two or more byte representation of a character. An example, the German has the following UTF-8 representation: 0xc3 0x84

    xxxxxx 0xc3|0x84 xxxxxx -----------^ End of buffer

    This could lead to decoding errors. But when you read AND DECODE linewise you can be pretty sure that all bytes read until NL (or whatever your line ending is) can be decoded properly.

    Putting a decoding layer to your filehandle should also work with the handle you get from an upload, so

    binmode($fh, ":utf8");

    should be valid too.


Re^2: Problem with utf8 after nearly 4096 bytes
by Anonymous Monk on Sep 02, 2013 at 08:00 UTC

    I'm not sure if I understood your advice,...

    I made two suggestions that would reduce memory usage, please try them both out, and if your problem is solved, then you know one of the two solved it ...

    Growing a string line/by/line does not decrease memory usage, that is still slurping the whole file into memory

    Another thing to check is your version of (get the latest for bug fixes), and check that no POST_MAX has been set

    Also, try a different file :)

Re^2: Problem with utf8 after nearly 4096 bytes
by Random_Walk (Prior) on Sep 02, 2013 at 09:59 UTC

    Your new version is still copying the entire file into mem. What you should try is changing your current approach:

    undef $/; my $bigfile = <$fh>; # Do some processing on $bigfile
    to something like this
    while (my $line = <$fh>) { # Do some processing on $line }


    Pereant, qui ante nos nostra dixerunt!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1051888]
LanX ... reflecting root problems
choroba uses real lists so he can easily insert as many items in between as needed
LanX questions the integerity of this approach. ..
LanX doesn't sound natural
[LanX]: welcome to a new episode of Big Perl Theory...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2018-03-20 10:30 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (250 votes). Check out past polls.