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

Required Conclusion:

 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 
+6
source share
1 answer

This is apparently a mistake in Rakudo / MoarVM, returning to the fact that MoarVM expects a separate grapheme as a 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 separation line is used instead of the whole line).

As a quick workaround (but be careful that it reads the entire file in memory), use

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

instead of $infile.lines() .

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

+5
source

Source: https://habr.com/ru/post/984073/


All Articles