在perl6中,如何以段落模式读取文件? [英] In perl6, how do you read a file in paragraph mode?

查看:116
本文介绍了在perl6中,如何以段落模式读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

data.txt:

  hello world 
goodbye mars

goodbye perl6
hello perl5

myprog.py:

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

用于$ infile.lines(nl =>\\\
\\\
) - > $ para {
说$ para;
表示' - 'x 10;



$ b实际输出:

$ b pre> hello world
----------
goodbye mars
----------

----------
再见perl6
----------
回到perl5
---- ------

所需的输出:

  hello world 
goodbye mars
-----------
goodbye perl6
回到perl5
-----------

...

  $ perl6 -v 
这是在MoarVM版本2015.03上创建的perl6版本2015.03-21-gcfa4974
syncfile。 c:38 , syn cfile.c:119 syncfile.c :91 ,它显示了分隔符字符串的最后一个字符,而不是整个字符串)。

作为一个快速的解决方法(但是要注意这个将整个文件读入内存),使用

  $ fname.IO.slurp.split(\\\
\\\

而不是 $ infile.lines()

如果这是一个已知的问题,您还应该在Freenode上提交错误报告或询问#perl6


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

解决方案

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.

这篇关于在perl6中,如何以段落模式读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆