#!/usr/bin/env perl
# http://www.perl.com/pub/2012/04/perlunicook-standard-preamble.html
use utf8; # so literals and identifiers can be in UTF-8
use v5.12; # or later to get "unicode_strings" feature
use strict; # quote strings, declare variables
use warnings; # on by default
use warnings qw(FATAL utf8); # fatalize encoding glitches
use open qw(:std :utf8); # undeclared streams in UTF-8
use charnames qw(:full :short); # unneeded in v5.16
use Dancer;
use Encode ();
use HTML::Entities ();
use JSON::XS ();
use Devel::Peek;
use utf8;
our $string = "Hello – World";
get '/' => sub {
return 'ok crash';
};
get '/ok' => sub {
my $perl_internal = Encode::decode('utf-8', $string); # "use utf8;" above made $string be in utf-8, decode to Perl-internal (= Unicode, stored internally as utf-8, right?)
HTML::Entities::decode_entities($perl_internal); # in void context, expand entities in-place to Unicode
my $json = JSON::XS::encode_json([$perl_internal]); # pass a Perl data structure, and expect output to be a utf-8 encoded, binary string
Dump( $perl_internal );
Dump( $json );
my $ok = utf8::decode($json); # fairly good way to validate utf-8
return "JSON:$json
". ($ok ? "ok" : "not ok");
};
get '/crash' => sub {
my $perl_internal = Encode::decode('utf-8', $string);
HTML::Entities::decode_entities($perl_internal);
my $json = to_json([$perl_internal],{pretty => 0}); # JSON's to_json, in contrary to JSON::XS's to_json seems to do something wrong,
Dump( $perl_internal );
Dump( $json );
# or is less clever in automagically handling what I'm doing wrong while preparing the scalar
my $ok = utf8::decode($json);
return "JSON:$json
". ($ok ? "ok" : "not ok");
};
dance;