Perl 与Mojolicious或HTML::Entities一起使用的Umlaut有关的Git::Repository问题
我在网站上显示有关Umlauts的git日志条目时遇到问题。我不知道在哪里寻找解决方案,所以我在这里寻求帮助。我确实认为这可能是编码的问题,但是Perl 与Mojolicious或HTML::Entities一起使用的Umlaut有关的Git::Repository问题,perl,mojolicious,Perl,Mojolicious,我在网站上显示有关Umlauts的git日志条目时遇到问题。我不知道在哪里寻找解决方案,所以我在这里寻求帮助。我确实认为这可能是编码的问题,但是使用utf8在我的尝试中没有任何效果。我努力解释这个问题,希望找到一个有用的答案。非常感谢 因此,我在提交消息中使用umlauts创建了一个repo: echo "Hello Wörld!" > a_file.txt git init git add a_file.txt git commit -m "Some Ümlaut: üöä" 我现在可
使用utf8
在我的尝试中没有任何效果。我努力解释这个问题,希望找到一个有用的答案。非常感谢
因此,我在提交消息中使用umlauts创建了一个repo:
echo "Hello Wörld!" > a_file.txt
git init
git add a_file.txt
git commit -m "Some Ümlaut: üöä"
我现在可以在cmd中查看它,没有问题:
$ git log
...
Some Ümlaut: üöä
我还可以用Perl打印这些内容,不会出现问题。
我会打电话给:
use Git::Repository;
my $repo = Git::Repository->new(work_tree => ".");
my $log = $repo->run( "log" );
print "$log\n";
这给了我与上面的shell示例相同的输出
当我使用Mojolicious
时会出现问题。
以下是一个例子:
use Mojolicious::Lite;
get '/' => sub {
my $self = shift;
use Git::Repository;
my $repo = Git::Repository->new(work_tree => ".");
my $log = $repo->run( "log" );
$self->render(text => "$log -- möre Ümläut\n" );
};
app->start;
当我运行此命令时,字符串中的Umlaut将起作用,但来自commit消息的Umlaut不会起作用。为了说明这一点,我可以按如下方式运行上述Perl:
perl mojo.pl daemon
然后我用curl调用该网站:
$ curl http://127.0.0.1:3000
...
Some Ãmlaut: üöä -- möre Ümläut
正如我所说:Git的Umlauts失败了,剩下的就可以了
所以我觉得我很聪明,把它们翻译成HTML实体:
use strict;
use warnings;
use Git::Repository;
use HTML::Entities 'encode_entities';
my $repo = Git::Repository->new(work_tree => ".");
my $log = $repo->run( "log" );
print "$log\n";
my $htmlified = encode_entities($log);
print "$htmlified\n";
但如果这样说,只有第一个输出是好的HTML::Entities
与Mojolicous具有相同的问题:
...
Some Ümlaut: üöä
...
Some Ümlaut: üöä
问题是在Git::Repository
中,还是在哪里失败?
我在Ubuntu12.04上使用Perl5.16进行测试。
谢谢你的帮助。我知道了怎么做,
decode\u utf8()
是你的朋友。
但我仍然不知道为什么需要这一步
事情是这样的:
use Mojolicious::Lite;
# we need this lib, part of core
use Encode;
get '/' => sub {
my $self = shift;
use Git::Repository;
my $repo = Git::Repository->new(work_tree => ".");
my $log = $repo->run( "log" );
# this call does the trick
my $wtf = decode_utf8($log);
$self->render(text => "$wtf -- möre Ümläut\n" );
};
app->start;
希望这也能帮助其他人。如果有人认为应该有一个错误报告给上述LIB之一,请告诉这里。我不知道这是否是一个解决方法、bug或特性:-P这也适用于上面发布的使用HTML::Entities的示例。打印到标准输出现在不再需要更多的工作(
Some)�mlaut:��代码>),但HTML实体工作正常:SomeÜ;mlaut:üöä代码>原因很可能是Mojolicious在渲染之前会仔细编码;因此,对来自外部源的输入进行解码非常重要。unicode的一个关键租户是在程序的边界处进行解码和编码。现在请注意:问题是,Git::Repository不返回Perl字符,而是返回“原样”字节。正确,因此必须对它们进行解码。Unicode很难,我们去购物吧:-)