UTF8 with Perl CGI.pm and Ajax

It took me about three hours to figure out something this simple, so I’m posting this here!

If you’re doing ajax stuff with CGI.pm in perl, you’ll want to exchange your data in UTF8, to do so you need only one special line of code*:

my $json = decode utf8=>$q->param('json');

This will make perl work natively in UTF8 with $json.

If you want to put this (or part of it) in a database, you don’t need to do anything special because the DBD driver will convert your string from UTF8 to the database encoding (UCS-2 in my case).

*Of course, you’ll also need to use Encode;.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>