打印 $/ 而不是 \n 是错误的吗?
Is it wrong to print $/ instead of \n?
Perl 的文档说 $/
是:
The input record separator, newline by default. This influences Perl's
idea of what a "line" is.
那么,以下是不是基本错误:
print STDERR $var, $/;
而不是:
print STDERR "$var\n";
?
如果我做前者会出什么问题?
也许您正在寻找输出记录分隔符?
IO::Handle->output_record_separator( EXPR )
$OUTPUT_RECORD_SEPARATOR
$ORS
$\
The output record separator for the print operator.
If defined, this value is printed after the last of print's arguments. Default is "undef".
You cannot call "output_record_separator()" on a handle, only as a static method. See IO::Handle.
Mnemonic: you set "$\
" instead of adding "\n
" at the end of the print. Also, it's just like $/
, but it's what you get "back" from Perl.
例如,
$\ = $/;
print STDERR $var;
您需要输出记录分隔符 $\,因为 xxfelixxx 已回答。
$/
如您阅读的那样是 input record separator
。对其进行操作会影响 Perl 读取您提供的文件数据的方式。例如:
open my $fh, "<", $filename or die $!;
local $/; # enable localized slurp mode
my $content = <$fh>;
close $fh;
以上导致文件的全部内容在标量 $content 中吞噬,因为我们重置了 $/
。
考虑以下代码:
#!/usr/bin/perl
use strict;
use warnings;
my $content;
{local $/; $content = <DATA>}
print "Content is $content";
__DATA__
line 1
line 2
line 3
输出:
Content is line 1
line 2
line 3
但是如果你不重置 $/,就像下面的代码:
#!/usr/bin/perl
use strict;
use warnings;
my $content = <DATA>;
print "Content is $content";
__DATA__
line 1
line 2
line 3
输出将是 Content is line 1
。
这是因为输入记录分隔符被设置为换行,并且在第一行之后返回。
$/
默认为LF(U+000A)。这与 "\n"
[1] 生成的字符相同。所以除非你改变 $/
,否则 $/
和 "\n"
是等价的。如果你真的改了$/
,那么只有你知道为什么,因此只有你知道$/
还是"\n"
更合适。
- 在古老的 MacOS 机器上,
$/
的默认值是 CR (U+000D),但这也是 "\n"
在那里产生的。
Perl 的文档说 $/
是:
The input record separator, newline by default. This influences Perl's idea of what a "line" is.
那么,以下是不是基本错误:
print STDERR $var, $/;
而不是:
print STDERR "$var\n";
?
如果我做前者会出什么问题?
也许您正在寻找输出记录分隔符?
IO::Handle->output_record_separator( EXPR ) $OUTPUT_RECORD_SEPARATOR $ORS $\The output record separator for the print operator. If defined, this value is printed after the last of print's arguments. Default is "undef".
You cannot call "output_record_separator()" on a handle, only as a static method. See IO::Handle.
Mnemonic: you set "
$\
" instead of adding "\n
" at the end of the print. Also, it's just like$/
, but it's what you get "back" from Perl.
例如,
$\ = $/;
print STDERR $var;
您需要输出记录分隔符 $\,因为 xxfelixxx 已回答。
$/
如您阅读的那样是 input record separator
。对其进行操作会影响 Perl 读取您提供的文件数据的方式。例如:
open my $fh, "<", $filename or die $!;
local $/; # enable localized slurp mode
my $content = <$fh>;
close $fh;
以上导致文件的全部内容在标量 $content 中吞噬,因为我们重置了 $/
。
考虑以下代码:
#!/usr/bin/perl
use strict;
use warnings;
my $content;
{local $/; $content = <DATA>}
print "Content is $content";
__DATA__
line 1
line 2
line 3
输出:
Content is line 1
line 2
line 3
但是如果你不重置 $/,就像下面的代码:
#!/usr/bin/perl
use strict;
use warnings;
my $content = <DATA>;
print "Content is $content";
__DATA__
line 1
line 2
line 3
输出将是 Content is line 1
。
这是因为输入记录分隔符被设置为换行,并且在第一行之后返回。
$/
默认为LF(U+000A)。这与 "\n"
[1] 生成的字符相同。所以除非你改变 $/
,否则 $/
和 "\n"
是等价的。如果你真的改了$/
,那么只有你知道为什么,因此只有你知道$/
还是"\n"
更合适。
- 在古老的 MacOS 机器上,
$/
的默认值是 CR (U+000D),但这也是"\n"
在那里产生的。