【发布时间】:2012-07-24 09:32:38
【问题描述】:
当我从 SQLite3 数据库中获取 Cyrillic 文本时,在某些情况下 perl(或 Mojolicious,或 DBIx::Class - 我真的不知道)无法解码字节流。例如,给定文本:
1984г1ф!!11четыре
输出将是:
1984г1ф!!11������
1984\x{433}1\x{444}!!11\x{fffd}\x{fffd}\x{fffd}\x{fffd}\x{fffd}\x{fffd}
为什么会这样?如何解决这个问题?
更新:我能够追踪到这个问题的根源。看起来格式错误的字符串是从网页上的用户输入中获取的,并作为参数发送到控制器操作:code here。
执行保存操作会产生以下日志:
[2012/07/24 14:06:09] [DEBUG] 15703 Mojolicious.Plugin.RequestTimer - POST /admin/node/save (Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1).
[2012/07/24 14:06:09] [DEBUG] 15703 Mojolicious.Routes - Routing to a callback.
[2012/07/24 14:06:09] [DEBUG] 15703 Mojolicious.Routes - Routing to controller "MyApp::Admin" and action "save".
Wide character in print at /home/nikita/perl5/lib/perl5/Log/Log4perl/Appender/File.pm line 245.
Wide character in print at /home/nikita/perl5/lib/perl5/Log/Log4perl/Appender/Screen.pm line 39.
[2012/07/24 14:06:09] [DEBUG] 15703 MyApp.Admin - 123123!!!11������������������
更新 2:我使用 Morbo 作为开发服务器,我的应用程序布局包含 meta 标头:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
更新 3: 奇怪,但有时字符串被正确编码和显示:
[2012/07/24 14:55:52] [DEBUG] 16451 Mojolicious.Routes - Routing to a callback.
[2012/07/24 14:55:52] [DEBUG] 16451 Mojolicious.Routes - Routing to controller "MyApp::Admin" and action "save".
[2012/07/24 14:55:52] [DEBUG] 16451 MyApp.Admin - 112!!ЫВафывафывп
[2012/07/24 14:55:52] [DEBUG] 16451 Mojolicious.Plugin.RequestTimer - 302 Found (0.326543s, 3.062/s).
[2012/07/24 14:55:52] [DEBUG] 16451 Mojolicious.Plugin.RequestTimer - GET /admin (Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1).
如果我第二次做同样的事情,我会得到:
[2012/07/24 14:57:30] [DEBUG] 16451 Mojolicious.Routes - Routing to controller "MyApp::Admin" and action "save".
Wide character in print at /home/nikita/perl5/lib/perl5/Log/Log4perl/Appender/File.pm line 245.
Wide character in print at /home/nikita/perl5/lib/perl5/Log/Log4perl/Appender/Screen.pm line 39.
[2012/07/24 14:57:30] [DEBUG] 16451 MyApp.Admin - 112!!��������������������
[2012/07/24 14:57:30] [DEBUG] 16451 Mojolicious.Plugin.RequestTimer - 302 Found (0.362417s, 2.759/s).
[2012/07/24 14:57:30] [DEBUG] 16451 Mojolicious.Plugin.RequestTimer - GET /admin (Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1).
【问题讨论】:
-
Show your code 这样我们就可以reproduce the problem。
-
你需要什么代码?潜在故障点太多。
-
生成minimal test case。如果您是一个糟糕的程序员并且没有能力这样做,请提供一切:您的所有代码和示例数据以运行代码。 - UTF-8 in SQLite/DBIC works fine for me.
-
查看问题更新。
-
你为什么不向我们展示你的代码?
标签: perl unicode mojolicious