How do I read paragraphs at a time with Perl?

前端 未结 3 1570
暗喜
暗喜 2020-12-21 18:37

When I write

#!/usr/bin/perl -w
use strict;

while(  ) {
    print \"\\n-------------------------\\n\\n\";
    print;
    <>;
}
         


        
3条回答
  •  情歌与酒
    2020-12-21 19:11

    In your first script, with the $/ variable set to default "\n", the will only return one line at a time.

    I believe the second script does what you want, it's just that <> won't terminate the read on a 'return' but rather on a due to your $/ setting (as someone else pointed out <> reads from STDIN but I think you already know that and are using it to regulate the output).

    If you really want to regulate the output with 'return' then you need to do more with $/ in the loop.

    while(  ) {
        print "\n-------------------------\n\n";
        print;
        $/ = "\n"; # default so that the following terminates the read on 'return'
        <>;
        $/ = ""; 
    }   
    

提交回复
热议问题