How do I read paragraphs at a time with Perl?

前端 未结 3 1582
暗喜
暗喜 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:21

    I guess you're expecting this line

    local $/ = "";
    

    to change the behaviour of

    
    

    to keep reading until the end of the data.

    But in fact it takes something like this

    {
        local $/;  # $/ becomes undef in this block
        ...
    }
    

    to enable slurp mode (and to contain that mode to the scope inside the {curlys}).

    In effect it's saying "forget about thinking of newlines as the end-of-record marker",

    Besides that... there's a tie fighter in your code!

    while(  ) {
        print "\n-------------------------\n\n";
        print;
        <>;    # <-- Feel the power of the DARK SIDE!!!
    }
    

    This little guy will read from STDIN, not from DATA - is that really what you want?

提交回复
热议问题