Hi dweston. Sorry we didn't make you feel more welcome. Hopefully you've figured it out now from the earlier links I posted, and from your replies on StackOverflow. If not, read on.

The reason the first program fails to print out the Chinese character is that you never tell Perl that it has non-ASCII characters in the source. In the second program you don't say so either, but you encode the characters so Perl outputs them properly. In the first script, Perl tries to output as ASCII, which cuts off the high unicode characters since they span more than one octet.

To make Perl correctly output the Chinese characters in your first program, simply tell it what it's dealing with by stating use utf8; at the top.


#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use JSON;

my %genders_zh = (
  'Female'           => '女',
  'Male'             => '男',
  'Decline to State' => '',
);

my $gender   = 'Female';
my $hash_ref = {};

$hash_ref->{'detail_sex'} = $genders_zh{ $gender }; 

print JSON->new->utf8( 1 )->pretty( 1 )->encode( $hash_ref );
__END__

Output:

$ perl 1140925.pl
{
   "detail_sex" : "女"
}
$

Note that you are only avoiding the "Wide character in print" warning because you tell JSON to encode your output as utf8. If your statement didn't include the ->utf8( 1 ), you would have to deal with it another way, e.g. by calling binmode on your STDOUT:


#!/usr/bin/perl
use strict;
use warnings;
use utf8;
binmode STDOUT, 'utf8';

my %genders_zh = (
  'Female'           => '女',
  'Male'             => '男',
  'Decline to State' => '',
);

my $gender   = 'Female';

print $genders_zh{ $gender }, "\n"; 

__END__
Output:

$ perl 1140925.pl
女
$

Hope this helps.

The way forward always starts with a minimal test.

In reply to Re^5: Trouble with Chinese characters ( binmode ) by 1nickt
in thread Trouble with Chinese characters by dweston

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.