Created
January 15, 2020 08:12
-
-
Save renormalist/cbe27895e2bfb4a1ad14e1fe7e6f1875 to your computer and use it in GitHub Desktop.
Mojo app with Unicode clumsiness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# myapp.pl -start it with: | |
# ./myapp.pl prefork -l http://localhost:8084 | |
# The Unicode character used below is a small "umlaut letter a". | |
use utf8; | |
use 5.022; # should implicitely contain use feature 'unicode_strings' | |
use strict; | |
use warnings; | |
use Mojolicious::Lite; | |
use Mojo::JSON 'decode_json'; | |
use Mojo::Util 'dumper'; | |
use Encode 'encode'; | |
binmode(STDOUT, ":utf8"); | |
any '/' => sub { | |
my $c = shift; | |
my $utf8_sample = 'ä'; | |
say "UMLAUT.utf8?: " . utf8::is_utf8($utf8_sample); | |
say "UMLAUT: " . $utf8_sample; | |
my $query_string = $c->param('query_string'); | |
if ($query_string) { | |
say "INPUT: " . $query_string; | |
$c->stash->{query_string} = $query_string; # <-- (1) why no encode() needed here? | |
my $query = decode_json(encode('UTF-8', $query_string)); # <-- (2) encode() necessary? | |
say "QUERY: " . dumper($query); # <-- (3) still no unicode in output? | |
} | |
$c->render(template => 'index'); | |
}; | |
app->start; | |
__DATA__ | |
@@ index.html.ep | |
% layout 'default'; | |
% title 'Welcome'; | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<h1>Welcome to my UTF-8 problem! Thanks for any help.</h1> | |
%= form_for "/" => (method => 'POST', "accept-charset" => "UTF-8") => begin | |
%= text_area "query_string" => ($c->stash->{query_string} || '{ "foo" : "ä" }') => (rows => 5) | |
%= submit_button "Search" | |
% end | |
</body> | |
</html> | |
@@ layouts/default.html.ep | |
<!DOCTYPE html> | |
<html> | |
<head><title><%= title %></title></head> | |
<body><%= content %></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment