我正在嘗試撰寫一個 perl 腳本來識別帶有 Windows EOL 字符的檔案,但\r匹配似乎不起作用。
這是我的測驗腳本:
#!/usr/bin/perl
use File::Slurp;
$winfile = read_file('windows-newlines.txt');
if($winfile =~ m/\r/) {
print "winfile has windows newlines!\n"; # I expect to get here
}
else {
print "winfile has unix newlines!\n"; # But I actually get here
}
$unixfile = read_file('unix-newlines.txt');
if($unixfile =~ m/\r/) {
print "unixfile has windows newlines!\n";
}
else {
print "unixfile has unix newlines!\n";
}
這是它的輸出:
winfile has unix newlines!
unixfile has unix newlines!
我在 Windows 上運行它,我可以在 Notepad 中確認檔案肯定具有正確的 EOL 字符:

uj5u.com熱心網友回復:
除非binmode是真的(不在您的代碼中)read_file將在 Windows 上更改\r\n為。\n從代碼:
# line endings if we're on Windows
${$buf_ref} =~ s/\015\012/\012/g if ${$buf_ref} && $is_win32 && !$opts->{binmode};
為了保持原始編碼設定 binmode,如檔案中所示:
my $bin = read_file('/path/file', { binmode => ':raw' });
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/420219.html
標籤:
上一篇:如何lcfirst每組詞?
