http://qs1969.pair.com?node_id=1094410

xyzzy has asked for the wisdom of the Perl Monks concerning the following question:

Short version: When using return to send a response, Dancer converts a Unicode string into ISO8859-1. When setting the content directly via the Dancer::Response->new() method, the response contains the correct string.

Long version: I have an extremely minimal Dancer app. At some point, I was going to expand it to do a lot more, but as of right now the only thing it does is return the currently-playing track of an MPD server running on the same machine. A static page with an HTML5 internet radio player sends a request and updates a "Now Playing:" span at regular intervals. I needed something quick and dirty without mucking about with the two MPD modules on CPAN, so I used a system call. For those unfamiliar with MPD, it is a music player with a server-client architecture. There are a plethora of clients available for all different platforms, but the most basic is a CLI client called mpc. Called with no arguments, it returns the server status:

xyzzy@asscat:~$ mpc
ДДТ - Чёрно-белые танцы
[playing] #27/31 1:21/6:03 (21%)
volume: n/a repeat: off random: off single: off consume: off
xyzzy@asscat:~$

Here's the first version:

get '/np' => sub { return `mpd | head -n1`; }

Simple enough. But instead of the Unicode, my span looks like this:

Now playing: ДДТ - Чёрно-белые танцы

I spent an hour trying to enable utf8, checking the HTTP headers, the meta tags on the page, even using Encode, but nothing worked. Then I rewrote my handler like so:

get '/np' => sub { Dancer::Response->new( status => 200, content => `mpd | head -n1`, ); }

Suddenly:

Now playing: ДДТ - Чёрно-белые танцы

Most of me only cares that it works now. But part of me is still baffled why one way works and the other way doesn't. What is it about return that mangles the sting encoding? It has to be something inherent in Dancer, because if I do

xyzzy@asscat:~$ perl -e'sub a {return `mpc|head -n1`}print a'
ДДТ - Герой

it works perfectly fine. Does anyone here know enough about Dancer's internals or is clever enough to figure this out?


$,=qq.\n.;print q.\/\/____\/.,q./\ \ / / \\.,q.    /_/__.,q..
Happy, sober, smart: pick two.