perl - 来自编码函数的检查参数

标签 perl encode

为什么我从第二个循环(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       :   

最佳答案

该行为在documentation中进行了描述。 (参见下面的片段) - 它修改数据并在 $s 中留下未处理的部分。由于没有错误,它基本上清除了您的变量。

*CHECK* = Encode::FB_QUIET
  If *CHECK* is set to Encode::FB_QUIET, (en|de)code will immediately
  return the portion of the data that has been processed so far when an
  error occurs. The data argument will be overwritten with everything
  after that point (that is, the unprocessed part of data). This is
  handy when you have to call decode repeatedly in the case where your
  source data may contain partial multi-byte character sequences, (i.e.
  you are reading with a fixed-width buffer). Here is a sample code that
  does exactly this:

    my $buffer = ''; my $string = '';
    while(read $fh, $buffer, 256, length($buffer)){
      $string .= decode($encoding, $buffer, Encode::FB_QUIET);
      # $buffer now contains the unprocessed partial character
    }

*CHECK* = Encode::FB_WARN
  This is the same as above, except that it warns on error. Handy when
  you are debugging the mode above.

关于perl - 来自编码函数的检查参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5079705/

相关文章:

perl - 如何让 Perl 脚本识别 Win32 cmd 控制台中的参数?

python - 多台主机上的并行 rsync

perl - 为什么 perl 警告打开我的 $fh, $file 缺少括号?

php - 存储在 BLOB 中 - 仅 1 KB 数据,不存储图像

java - URL java 中的特殊字符不起作用

perl - Windows XP 上的 CPAN 问题

arrays - 比较两个数组并找出差异

ffmpeg - 使用输入流(或 url)时 ffmpeg 可以搜索吗?

iOS:@encode 并直接使用 -> 访问子类的属性

javascript - 将字符串编码为 ISO-8859-1