Flushing Perl STDIN buffer

前端 未结 3 689
既然无缘
既然无缘 2021-01-04 19:10

Is there any way to clear the STDIN buffer in Perl? A part of my program has lengthy output (enough time for someone to enter a few characters) and after that output I ask f

3条回答
  •  天命终不由人
    2021-01-04 20:03

    { local $/;  }
    

    This temporarily - limited to scope of the block - sets $/, the input record seperator, to be undef, which tells perl to just read everything instead of reading a line at a time. Then reads everything available on STDIN and doesn't do anything with it, thus flushing the buffer.

    After that, you can read STDIN as normal.

提交回复
热议问题