Created
January 15, 2020 08:21
Star
You must be signed in to star a gist
Mojo app with less 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'; | |
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) | |
my $query = decode_json($query_string); # <-- (2) error: Input is not UTF-8 encoded at [...] | |
say "QUERY: " . dumper($query); # <-- (3) | |
} | |
$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