您现在的位置是:首页 > 电脑 > 

在使用data :: dumper PERL打印时,不会遵守口音(Accents not respected in printing out with data::dumper PERL)

2025-07-19 09:57:59
在使用data :: dumper PERL打印时,不会遵守口音(Accents not respected in printing out with data::dumper PERL) 我想打印出一个关联数组的内容。 为此,我使用Data :: dumper。 所以,例如,如果关联数组被称为“%w”,我写: print OUT Dumper(\
在使用data :: dumper PERL打印时,不会遵守口音(Accents not respected in printing out with data::dumper PERL)

我想打印出一个关联数组的内容。 为此,我使用Data :: dumper。

所以,例如,如果关联数组被称为“%w”,我写:

print OUT Dumper(\%w);

这就是问题所在:有些词语如“récente”被打印为“récente”。

如果我只写:

print OUT %w;

我没有问题,所以“récente”将打印为“récente”。

所有用于该脚本的文本文件都在utf8中。 此外,我使用模块“utf8”,我总是指定字符编码系统。

例如。 :

open( I, '<', $file_in); binmode(I,":utf8");

我很确定这个问题与Data :: dumper有关。 有没有办法解决这个问题或其他方式来打印关联数组的内容?

谢谢。

I would like to print out the content of an associative array. For this I'm using Data::dumper.

So, for exemple, if the associative array is called "%w", I write :

print OUT Dumper(\%w);

Here's the problem: there are some words like "récente" that are printed out as "r\x{e9}cente".

If I write just :

print OUT %w;

I've no problems, so "récente" it will be printed out as "récente".

All text files used for the script are in utf8. Moreover I use the module "utf8" and I specify always the character encoding system.

For ex. :

open( I, '<', $file_in); binmode(I,":utf8");

I'm pretty sure that the problem is related to Data::dumper. Is there a way to solve this or another way to print out the content of an associative array?

Thank you.

最满意答案

这是故意的。 Data::Dumper的输出用于在eval为Perl代码时生成相同的数据结构。 为了限制字符编码的效果,非ASCII字符将被转义使用转义。 除此之外,设置$Data::Dumper::Useqq = 1是明智的,这样任何不可打印的字符都会使用转义字符转储。

Data::Dumper并不是真正意义上的显示数据结构的方式 - 如果您有特定的格式要求,请自己编写必要的代码。 例如

use utf8; use feature 'say'; open my $out, ">:utf8", $filename or die "Can't open $filename: $!"; my %hash = ( bárewørdş => '–Uni·code–', ); say { $out } "{"; for my $key (sort keys %hash) { say { $out } " $key: $hash{$key}"; } say { $out } "}";

产生

{ bárewørdş: –Uni·code– }

This is intentional. The output by Data::Dumper is intended to produce the same data structure when evaluated as Perl code. To limit the effect of character encodings, non-ASCII characters will be dumped using escapes. In addition to that, it's sensible to set $Data::Dumper::Useqq = 1 so that any unprintable characters are dumped using escapes.

Data::Dumper isn't really meant as a way to display data structures – if you have specific formatting requirements, just write the necessary code yourself. For example

use utf8; use feature 'say'; open my $out, ">:utf8", $filename or die "Can't open $filename: $!"; my %hash = ( bárewørdş => '–Uni·code–', ); say { $out } "{"; for my $key (sort keys %hash) { say { $out } " $key: $hash{$key}"; } say { $out } "}";

produces

{ bárewørdş: –Uni·code– }

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/697356.html

相关标签:无
上传时间: 2023-08-27 09:39:25
留言与评论(共有 7 条评论)
本站网友 中医医师
17分钟前 发表
我使用Data
本站网友 最新汇率
27分钟前 发表
dumper PERL打印时
本站网友 索贝
21分钟前 发表
所以
本站网友 满月
16分钟前 发表
除此之外
本站网友 三七胶囊
8分钟前 发表
dumper PERL) 我想打印出一个关联数组的内容
本站网友 张鸿渐
6分钟前 发表
所有用于该脚本的文本文件都在utf8中