【问题标题】:write non utf8 characters to file in perl在 perl 中将非 utf8 字符写入文件
【发布时间】:2012-01-01 16:25:36
【问题描述】:

在 perl 的脚本中,我将字符串写入以下列方式创建的临时文件:

IO::File->new_tmpfile

如何用同样的方式书写,非 utf8 字符?

【问题讨论】:

  • 好吧,只需使用适当标量的正常操作...您至少尝试过吗?哦,看看pack()
  • "Non utf8" 告诉我们您想要什么。你想要什么

标签: perl file utf-8


【解决方案1】:

您的IO::File 对象有一个binmode 方法,其工作方式与built-in of the same name 完全相同。您可以使用它来设置所需的 I/O 层。你可能想要这样的东西:

$fh->binmode(':utf8'); # or whatever character encoding you're using

【讨论】:

    【解决方案2】:

    IO::File->new_tmpfile 返回一个文件句柄。您可以像往常一样将print 发送到文件句柄。

     $fh = IO::File->new_tmpfile;
     print $fh "some stuff to write to the temp file.\n";
    

    【讨论】:

    • 如果 char 不是 utf8 char,我会在打印通常的方式时收到错误“Wide character”。
    猜你喜欢
    • 2011-11-04
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 2013-10-15
    相关资源
    最近更新 更多