我正在努力读取 UTF8 文本文件并将所有出现的 unicode 字符(摄氏度)替换为其他字符串。
#!/usr/bin/env perl
use 5.030;
use warnings;
use utf8;
use Perl6::Slurp;
my $s= "Hello. This is 2.3℃ .";
$s =~ s/2.3/two-point-three /gms;
$s =~ s/\x{2103}/degrees celsius/gms;
print "STRING: '$s'\n";
my $fs= slurp("test.md");
$fs =~ s/2.3/two-point-three /gms;
$fs =~ s/\x{2103}/degrees celsius/gms;
print "FSYSTM: '$fs'";
我的 test.md 文件读起来就像字符串一样。
Hello. This is 2.3℃ .
为什么输出
STRING: 'Hello. This is two-point-three degrees celsius .'
FSYSTM: 'Hello. This is two-point-three ℃ .
使用
Perl6::Slurp
读取非ascii文件时需要指定文件编码:
以下对我有用:
my $fs= slurp('<:utf8', "test.md");