Perl operator: $|++; dollar sign pipe plus plus

前端 未结 4 505
春和景丽
春和景丽 2020-12-17 10:24

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

相关标签:
4条回答
  • 2020-12-17 10:33

    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);

    0 讨论(0)
  • 2020-12-17 10:41

    $| 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).

    0 讨论(0)
  • 2020-12-17 10:50

    It increments autoflush, which is most probably equivalent to turning it on.

    0 讨论(0)
  • 2020-12-17 10:51

    $| defaults to 0; doing $|++ thus increments it to 1. Setting it to nonzero enables autoflush on the currently-selected file handle, which is STDOUT 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.

    0 讨论(0)
提交回复
热议问题