Search code examples
fileioraku

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


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

Solution

  • 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.