How do I read paragraphs at a time with Perl?

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

    Using <> that way (interactively) in paragraph mode is going to be confusing. It won't return when you hit "return"; instead, it will read until it gets a non empty line (the start of a paragraph), then read until it gets an empty line (the end of that paragraph), then continue reading until it gets a non-empty line (the start of the following paragraph - which will be buffered, not returned) so it knows that it's discarded any extra empty lines.

    Perhaps you should be using:

    local $/ = "\n"; <>
    

    at the end of your loop instead. Or maybe POSIX::getchar().

    0 讨论(0)
  • 2020-12-21 19:11

    In your first script, with the $/ variable set to default "\n", the <DATA> 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 <ctrl-d> 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( <DATA> ) {
        print "\n-------------------------\n\n";
        print;
        $/ = "\n"; # default so that the following terminates the read on 'return'
        <>;
        $/ = ""; 
    }   
    
    0 讨论(0)
  • 2020-12-21 19:21

    I guess you're expecting this line

    local $/ = "";
    

    to change the behaviour of

    <DATA>
    

    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( <DATA> ) {
        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?

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