On Fri, 18 Jan 2013, Shane McCarron wrote:

The @data list is then sorted (by desc) and used to populate a <select>
element in a web form.  Here's the weird thing.  When I am using this under
Apache on a Linux platform in a CGI application, I get a warning that I am
printing 'wide characters' when I use data from the 'desc' as the content
of an 'option' element.  What I *think* is that the data coming out of
DateTime::Locale is not properly encoded as Perl's utf8 internal encoding.
If I take the outout of the name() and native_name() methods and encode
them using "Encode", then it no longer complains.  Is this expected
behavior?

I think you have the cause a bit backwards.

That warning means that you're printing data marked as UTF-8 to an output that expects to receive binary data. By default, all handles expect binary.

Calling Encode::encode() turns Perl's UTF-8 flag _off_. I've always found this use of encode (and decode) confusing, since I'd expect encoding to produce UTF-8, which it does, but it produces _bytes_.

The solution you're using is probably not the best idea. It's better to mark your output filehandle as UTF-8 with binmode() or by passing a layer to open:

  open my $fh, '>:encoding(UTF-8)', ...;

In a webapp, you're probably printing to STDOUT so you can use binmode:

  binmode STDOUT, ':encoding(UTF-8)';

I'm not sure how this will interact with mod_perl's tying of STDOUT, though.


-dave

/*============================================================
http://VegGuide.org               http://blog.urth.org
Your guide to all that's veg      House Absolute(ly Pointless)
============================================================*/

Reply via email to