CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Writing utf8 chars

 



whitejm12
New User

Feb 2, 2007, 1:02 PM

Post #1 of 2 (479 views)
Writing utf8 chars Can't Post

Pre-newbie question (I didn't see an answer in the FAQs).

ActivePerl 5.6.1

Part 1
I need to have my script (actually, it's somebody else's, and I've inherited it) preserve and write out Japanese chars to a .txt file. Note that I'm using the Ja chars themselves, not a &#xxxxx; representation of them.

I search for instances of "Class" in input files and replace them with the Ja translation of "Class" (pseudo-translated here):

my($x)=@_;
farf("before my $x \n");
$x=~s/^(\s*)CLASS?\b/$1Clas日本ses/i;

The problem is that, in the output file, the Ja chars are always corrupted:

Clas譌・譛ャses

I placed

use utf8

at the top of the script file, but I think I need to specify that $1Clas日本ses/i is to be treated as UTF-8 and not flattened. How do I do this? At each $x= line?

Part 2
Here's how I save the file:

my($filename, @contents)=@_;
open (MYOUT, ">$filename");
print MYOUT @contents;
close MYOUT;

When I save the file, I think I need to specify that it's going to be utf-8 encoded. I've tried

open(OUTFILE, ">:utf8", $MYOUT);

to no avail.

I looked through perldoc perluniintro or perldoc unicode, and the answer may be there, but I can't figure it out.

Thanks for any help!

John


deepumathewsc
New User

Feb 5, 2007, 11:22 AM

Post #2 of 2 (469 views)
Re: [whitejm12] Writing utf8 chars [In reply to] Can't Post

John,

include the following line at the start of your routine
use open ':utf8';

We had trouble in including extened Ascii chars in our scripts. After using the above it helps us

Also when you open files use
open (FILE, ">:utf8", "xyz.abc");

Regards,
Deepu

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives