How do I read paragraphs at a time with Perl?

帅比萌擦擦* 提交于 2019-11-28 11:25:14

问题


When I write

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

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

after each "return" I get one line.

Why don't I get with the next script after each "return" one paragraph?

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

local $/ = "";

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

__DATA__
line one
line two
line three
line four
line five

line six
line seven
line eigth
line nine

line ten
line eleven
line twelve

回答1:


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'
    <>;
    $/ = ""; 
}   



回答2:


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?




回答3:


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



来源:https://stackoverflow.com/questions/1809469/how-do-i-read-paragraphs-at-a-time-with-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!