作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个 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 字符:
最佳答案
除非 binmode
为 true(这不在您的代码中)read_file
会将 \r\n
更改为 \n
在 Windows 上。来自 the code :
# line endings if we're on Windows
${$buf_ref} =~ s/\015\012/\012/g if ${$buf_ref} && $is_win32 && !$opts->{binmode};
为了保持原来的编码设置binmode,如图in the documentation :
my $bin = read_file('/path/file', { binmode => ':raw' });
关于perl - 如何让 Perl 匹配带有 Windows EOL 字符的文件中的\r,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70823524/
我是一名优秀的程序员,十分优秀!