简体   繁体   English

如何使用Perl一次阅读段落?

[英]How do I read paragraphs at a time with Perl?

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

In your first script, with the $/ variable set to default "\\n", the <DATA> will only return one line at a time. 在第一个脚本中,将$ /变量设置为默认“ \\ n”,<DATA>一次将仅返回一行。

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). 我相信第二个脚本可以满足您的要求,只是<>不会在'return'上终止读取,而是在<ctrl-d>上由于您的$ /设置终止读取(正如其他人指出的,<>读取来自STDIN,但我想您已经知道并且正在使用它来调节输出)。

If you really want to regulate the output with 'return' then you need to do more with $/ in the loop. 如果您真的想通过“ return”调节输出,则需要在循环中使用$ /进行更多操作。

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

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}). 启用Slurp模式 (并将该模式​​包含在{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? 这个小家伙会从STDIN而不是从DATA读取-这真的是您想要的吗?

Using <> that way (interactively) in paragraph mode is going to be confusing. 在段落模式下以交互方式使用<>会造成混乱。 It won't return when you hit "return"; 当您点击“ 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(). 或者也许是POSIX :: getchar()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM