Perl 运算符:$|++;美元符号管加号
我正在开发一个已经发布的 perl 代码的新版本,并发现了这一行:
$|++;
AFAIK, $|与管道有关,如 中所述这个链接,我明白这一点,但我无法弄清楚 ++ (加号)在这里意味着什么。
先感谢您。
编辑:找到答案 在此链接中:
简而言之:它强制在下一条语句之前打印(刷新)到控制台,以防脚本太快。
有时,如果您将 print 语句放入运行速度非常快的循环中,则在程序终止之前您将看不到 print 语句的输出。有时,您甚至根本看不到输出。解决这个问题的方法是在每个 print 语句之后“刷新”输出缓冲区;这可以在 perl 中使用以下命令执行:
$|++;
[更新] 正如 r 所指出的。施瓦茨,我说错了;上面的命令导致 print 刷新下一个输出之前的缓冲区。
I'm working on a new version of an already released code of perl, and found the line:
$|++;
AFAIK, $| is related with pipes, as explained in this link, and I understand this, but I cannot figure out what the ++ (plus plus) means here.
Thank you in advance.
EDIT: Found the answer in this link:
In short: It forces to print (flush) to your console before the next statement, in case the script is too fast.
Sometimes, if you put a print statement inside of a loop that runs really really quickly, you won’t see the output of your print statement until the program terminates. sometimes, you don’t even see the output at all. the solution to this problem is to “flush” the output buffer after each print statement; this can be performed in perl with the following command:
$|++;
[update]
as has been pointed out by r. schwartz, i’ve misspoken; the above command causes print to flush the buffer preceding the next output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
$|
默认为 0;执行$|++
会将其增加到 1。将其设置为非零会启用当前选定文件句柄的自动刷新,默认情况下为STDOUT
,并且很少更改。所以作用就是保证
print
语句之类的立即输出。如果您要输出到套接字等,这非常有用。$|
defaults to 0; doing$|++
thus increments it to 1. Setting it to nonzero enables autoflush on the currently-selected file handle, which isSTDOUT
by default, and is rarely changed.So the effect is to ensure that
print
statements and the like output immediately. This is useful if you're outputting to a socket or the like.正如您所发现的,
$|
是$OUTPUT_AUTOFLUSH
的缩写。++
递增该变量。<代码>$| = 1 将是执行此操作的干净方法(恕我直言)。
$|
is an abbreviation for$OUTPUT_AUTOFLUSH
, as you had found out. The++
increments this variable.$| = 1
would be the clean way to do this (IMHO).这是一个古老的习惯用法,来自 IO::Handle 之前的时代。在现代代码中,这应该写成
use IO::Handle;
STDOUT->autoflush(1);
It's an old idiom, from the days before IO::Handle. In modern code this should be written as
use IO::Handle;
STDOUT->autoflush(1);
它增加了自动刷新,这很可能相当于打开它。
It increments autoflush, which is most probably equivalent to turning it on.