Well, I don't think it's undiplomatic to expect a brand new user to take a couple of minutes to read before posting his question. I would certainly read the last few discussions on any board before I posted (not to mention use the search function). Lamentably, most people who come to forums like this one want to get instant gratification in the form of someone handing them a solution as soon as they ask their question, which is right after they create a user account (or not), which is right after they visit the site for the first time.
It's a courtesy to every such site in the world to search before asking, and there are plenty of notices that this is expected at PerlMonks. Most new users don't do that, and it generally goes unadmonished. But when the answer to the question and plenty of links to documentation are contained in a post that is still on the front page of most views, I think it's fair to point out that fact, and even to the fact that the user could have found it with extremely minimal effort, which I did by adding the words "all of" to my post.
I also linked to the node so he could read the whole discussion, and I edited the subject to reference the use of binmode, which, as you pointed out in that thread, is what he needs. I really think overall that was low on the undiplomacy scale, but then I am not the best judge of that. ++your post because you might be.
The way forward always starts with a minimal test.
| [reply] [d/l] |
This isn't SO. Although we expect people to do their own 'homework', we should welcome newbs, and at minimum advise them (with links to internal PM nodes) of what is expected here, and not just throw a link to a node that might solve their issue.
We're about education here... whether that be answering a homework-type question (against the 'rules', just because we want to code), or advising someone new how to ask proper questions. Everyone I've met (in person or not), those who speak shit sometimes (but often have deeper, business-level info) and those who critique (sometimes embarassingly so) code you're presenting all have the same agenda... to teach a howto, a newer or most often better ways to do things.
You've made some good posts here and updated some posts with more informative information, but I feel that we should welcome a newb properly at all costs.
-stevieb
| [reply] |
It's not so much the warnings I'm worried about. I'm fully aware that I can get rid of the warnings. What I'm confused about (and I admit, I should of been more clear on this) is why one script will print out the Chinese character I want and the other prints out 女.
I am more concerned with the "why" and not a solution. I feel that if I know why something is happening I can find a solution on my own (not to say I wouldn't appreciate one).
| [reply] |
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.
| [reply] [d/l] [select] |