Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Reading an Unicode File

by donno20 (Sexton)
on Apr 23, 2003 at 11:21 UTC ( [id://252515]=perlquestion: print w/replies, xml ) Need Help??

donno20 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,
I read an unicode file and just print out its content. but the while loop just stop somewhere in the middle of the file. If I changed the file to ASCII, then the while loop can loop throughly. here is my simple code:
open RS, "< $rs_file"; while (my $line = <RS>){ print "$. $line"; } close RS;
Any input would be greatly appreciated.

Replies are listed 'Best First'.
Re: Reading an Unicode File
by jmcnamara (Monsignor) on Apr 23, 2003 at 11:27 UTC

    If you are using Windows you should binmode the filehandle, this will avoid interpretation of ^Z (ASCII 26) as the end of file character:
    open RS, $rs_file or die "Error message here: $!\n"; binmode RS; while (<RS>) { print $., "\t", $_; }

    --
    John.

Re: Reading an Unicode File
by John M. Dlugosz (Monsignor) on Apr 23, 2003 at 16:04 UTC
    What do you mean by "A(n) Unicode file"? UTF-8, UTF-16, UCS-2, or what? The <RS> construct, without further telling it otherwise, will work for UTF-8 but not the others. In UTF-8 you don't need "binmode" either.

    If the file is using some other encoding, you need to set the input record separator to the proper byte sequence, and also use binmode.

    In Perl 5.8, there is built-in support for reading files in other encodings. You can use the extended open syntax to specify, and all should work fine without further intervention.

    —John

Re: Reading an Unicode File
by donno20 (Sexton) on Apr 24, 2003 at 02:17 UTC
    Thanks monks, I made it !!
    ^_^

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://252515]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found