开发者

problem with perl format and utf data

I am using a perl format to print out lines to a file

ie.

format REPORT1 =
@<<<<<<<<  @<<<<<<<<  ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  @>>>>>       @>>>>>       @>>>>>    @>>>>>       @>>>>>
$write_student_number,  $write_serial, $write_name, $wr开发者_如何学JAVAite_attempted, $write_correct, $write_score, $write_percent_correct, $write_percent_score
~~                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                      $write_name
-----------------------------------------------------------------------------------------------------------------------------
.

I retrieve the $write_name from an oracle database that is using uft8 character set.

This data use to work fine until we converted the feeding database to uft8 as well. I'm not sure what it use to be, but it was not utf 8.

The format now gives lines such as (notice the << >> and @’s)

009999990  000000133  Anderson, Charlie                                       30           25           25      83.3         83.3

-----------------------------------------------------------------------------------------------------------------------------

009999951  000000132  Smith, Josée                                            29>>         21   @>>>    21>>>   70.0    @>>  70.0

                      ,

-----------------------------------------------------------------------------------------------------------------------------

009999934  000000131<<Quiring, Randy                                     <<    30@>>>>>     12           12 @>>  40.0>

Instead of

009999990  000000133  Anderson, Charlie                                        30           25           25      83.3         83.3

-----------------------------------------------------------------------------------------------------------------------------

009999951  000000132  Smith, Josée                                            29           21           21      70.0         70.0

-----------------------------------------------------------------------------------------------------------------------------

009999934  000000131  Quiring, Randy                                           30           12           12      40.0    

I tried using a bunch of different solutions I found on the web, but none worked.

binmode STDOUT, ":utf8";

use open qw( :std :encoding(UTF-8) );

use Encode;
use Encode 'is_utf8';

$first_name = decode_utf8($first_name);

I also tried

use Encode 'from_to';
from_to($write_name, "utf-8", "iso-8859-1");

open my $fh, ">:utf8", $filename
    or die "could not open $filename: $!\n";

and

from_to($write_name, "utf-8", "WINDOWS-1252");

which sort of works, except it just replaces the utf characters with ?s.

A solution found that does sorta work is if I print out the string to a file, then read the string back in to the file and store it in the variable again.

Just a note, the format works in a windows environment. The problem only happens in unix.

I'm hoping you have a better solution.


Use of format is discouraged due to its API being flat out crazy, you may want to work with Template::Toolkit instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜