Perl Encode - UK characters

半腔热情 提交于 2019-12-11 12:30:29

问题


This is a part 2 question from This Question.

So I'm trying out the :encode functionality but having no luck at all.

use Encode;
use utf8;

# Should print: iso-8859-15
print "Latin-9 Encoding: ".find_encoding("latin9")->name."\n"; 

my $encUK = encode("iso-8859-15", "UK €");
print "Encoded UK: ".$encUK."\n";

Results:

Encoded UK: UK €

Shouldn't the results be encoded? what am I doing wrong here?

EDIT:

Added the suggested:

use utf8;

and now I get this:

Encoded UK: UK �

pulling hair out now :/


回答1:


Don't pull your hair. You did everything right, are finished and are already getting the intended data; the output is confusing you because you probably look at it from a terminal that is not set up for Latin-9, but for a different encoding, presumably UTF-8.

> perl -e'use utf8; use Encode; print encode "Latin-9", "Euro €"'
Euro �

> perl -e'use utf8; use Encode; print encode "Latin-9", "Euro €"' | hex
0000  45 75 72 6f 20 a4                                 Euro .

Codepoint A4 is indeed the Euro symbol in Latin-9.




回答2:


I think perhaps you are not encoding the character properly in your script. What does your editor think is its encoding?

e.g. I just tried this, to circumvent that entirely:

use Encode;

# Should print: iso-8859-15
print "Latin-9 Encoding: ".find_encoding("latin9")->name."\n";

my $encUK = encode("iso-8859-15", "\xA3");
print "Encoded UK: ", $encUK, "\n";

output:

 
Latin-9 Encoding: iso-8859-15  
Encoded UK: £  



回答3:


"use utf8;" is, since Perl 5.8, only used to tell Perl that your source file is encoded in UTF-8.

So does the encoding of your source really matches what you're telling to Perl?

With 'vim' must use this option to write the file in UTF-8:

:set fenc=utf8

And to get back UTF-8 when you load the file, you must define fileencodings in your .vimrc:

set fileencodings=ucs-bom,utf-8,latin9


来源:https://stackoverflow.com/questions/3046595/perl-encode-uk-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!