来自编码函数的检查参数
为什么我从第二个循环(CHECK 参数集)得到不同的输出?
#!/usr/bin/env perl
use warnings;
use 5.012;
use Encode qw(encode);
my $s = 'a';
for my $encoding ( 'iso-8859-1', 'iso-8859-15', 'cp1252', 'cp850' ) {
my $encoded = encode( $encoding, $s );
my $c = unpack '(B8)*', $encoded;
printf "%-12s:\t%8s\n", $encoding, $c;
}
say "-------------------";
for my $encoding ( 'iso-8859-1', 'iso-8859-15', 'cp1252', 'cp850' ) {
my $encoded = encode( $encoding, $s, Encode::FB_WARN );
my $c = unpack '(B8)*', $encoded;
printf "%-12s:\t%8s\n", $encoding, $c;
}
# iso-8859-1 : 01100001
# iso-8859-15 : 01100001
# cp1252 : 01100001
# cp850 : 01100001
# -------------------
# iso-8859-1 : 01100001
# Use of uninitialized value $c in printf at ./perl1.pl line 20.
# iso-8859-15 :
# Use of uninitialized value $c in printf at ./perl1.pl line 20.
# cp1252 :
# Use of uninitialized value $c in printf at ./perl1.pl line 20.
# cp850 :
Why do I get from the second loop (CHECK-argument set) a different output?
#!/usr/bin/env perl
use warnings;
use 5.012;
use Encode qw(encode);
my $s = 'a';
for my $encoding ( 'iso-8859-1', 'iso-8859-15', 'cp1252', 'cp850' ) {
my $encoded = encode( $encoding, $s );
my $c = unpack '(B8)*', $encoded;
printf "%-12s:\t%8s\n", $encoding, $c;
}
say "-------------------";
for my $encoding ( 'iso-8859-1', 'iso-8859-15', 'cp1252', 'cp850' ) {
my $encoded = encode( $encoding, $s, Encode::FB_WARN );
my $c = unpack '(B8)*', $encoded;
printf "%-12s:\t%8s\n", $encoding, $c;
}
# iso-8859-1 : 01100001
# iso-8859-15 : 01100001
# cp1252 : 01100001
# cp850 : 01100001
# -------------------
# iso-8859-1 : 01100001
# Use of uninitialized value $c in printf at ./perl1.pl line 20.
# iso-8859-15 :
# Use of uninitialized value $c in printf at ./perl1.pl line 20.
# cp1252 :
# Use of uninitialized value $c in printf at ./perl1.pl line 20.
# cp850 :
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
文档中描述了该行为(请参阅下面的片段) - 它修改数据并将未处理的部分留在
$s
中。由于没有错误,它基本上清除了你的变量。The behavior is described in documentation (see snip below) - it modifies data and leaves unprocessed portion in
$s
. Since there is no error, it basically clears your variable.当
CHECK
设置为Encode::FB_QUIET
时,数据参数将被覆盖:When
CHECK
is set toEncode::FB_QUIET
, the data argument is overwritten:您可以通过 Encode::LEAVE_SRC 中的 or 来防止覆盖
You can prevent the overwriting by oring in Encode::LEAVE_SRC