我正在尝试编写一个 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";
}
Run Code Online (Sandbox Code Playgroud)
这是它的输出:
winfile has unix newlines!
unixfile has unix newlines!
Run Code Online (Sandbox Code Playgroud)
我在 Windows 上运行它,我可以在 Notepad++ 中确认这些文件肯定具有正确的 EOL 字符:
除非binmode是 true (这不在您的代码中)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};
Run Code Online (Sandbox Code Playgroud)
为了保留原始编码设置 binmode,如文档中所示:
my $bin = read_file('/path/file', { binmode => ':raw' });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
90 次 |
| 最近记录: |