可靠的Perl编码与File :: Slurp [英] Reliable Perl encoding with File::Slurp

查看:611
本文介绍了可靠的Perl编码与File :: Slurp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在文件中用 // 替换每一次 http:// 。该文件可能是(至少)在 UTF-8 CP1251 CP1255



以下工作?

 使用File :: Slurp; 
我的$ Text = read_file($ File,binmode =>':raw');
$ Text =〜s {http://} {//} gi;
write_file($ File,{atomic => 1,binmode =>':raw'},$ Text);

这似乎是正确的,但是我需要确保该文件不会受到任何编码的损害。请帮助我确定。

解决方案

这个答案不会确定,虽然我希望它可以帮助。 p>

我的脚本没有看到任何问题(用utf8 ans iso-8859-1测试没有问题),尽管似乎有关于File :: slurp来正确处理编码: http: //blogs.perl.org/users/leon_timmermans/2015/08/fileslurp-is-broken-and-wrong.html



在这个答案中作为一个类似的主题,作者建议使用File :: Slurper作为替代方案,因为编码处理更好: https://stackoverflow.com/a / 206682/6193608


I need to replace every occurrence of http:// with // in a file. The file may be (at least) in UTF-8, CP1251, or CP1255.

Does the following work?

use File::Slurp;
my $Text = read_file($File, binmode=>':raw');
$Text =~ s{http://}{//}gi;
write_file($File, {atomic=>1, binmode=>':raw'}, $Text);

It seems correct, but I need to be sure that the file will not be damaged whatever encoding it has. Please help me to be sure.

解决方案

This answer won't make you sure, though I hope it can help.

I don't see any problem with your script (tested with utf8 ans iso-8859-1 without problems) though there seems to be a discussion regarding the capacity of File::slurp to correctly handle encoding : http://blogs.perl.org/users/leon_timmermans/2015/08/fileslurp-is-broken-and-wrong.html

In this answer on a similar subject, the author recommends File::Slurper as an alternative, due to better encoding handling: https://stackoverflow.com/a/206682/6193608

这篇关于可靠的Perl编码与File :: Slurp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆