In perl6, how do you read a file in paragraph mode?

感情迁移 提交于 2019-12-08 19:53:56

问题


data.txt:

hello world
goodbye mars

goodbye perl6
hello perl5

myprog.py:

my $fname = 'data.txt';
my $infile = open($fname, :r, nl => "\n\n");

for $infile.lines(nl => "\n\n") -> $para {
    say $para;
    say '-' x 10;
}

Actual output:

hello world
----------
goodbye mars
----------

----------
goodbye perl6
----------
back to perl5
----------

Desired output:

hello world
goodbye mars
-----------
goodbye perl6
back to perl5
-----------

...

$ perl6 -v
This is perl6 version 2015.03-21-gcfa4974 built on MoarVM version 2015.03

回答1:


This appears to be a bug in Rakudo/MoarVM, going back to the fact that MoarVM expects a single grapheme as separator instead of an arbitrary string (cf syncfile.c:38, syncfile.c:119 and syncfile.c:91, which shows that the last character of the separator string is used instead of the whole string).

As a quick workaround (but beware that this reads the entire file into memory), use

$fname.IO.slurp.split("\n\n")

instead of $infile.lines().

You should also file a bug report or ask in #perl6 on Freenode if this is a known issue.



来源:https://stackoverflow.com/questions/29178966/in-perl6-how-do-you-read-a-file-in-paragraph-mode

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