如何在Perl 6中附加文件? [英] How do you append to a file in Perl 6?

查看:59
本文介绍了如何在Perl 6中附加文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试此操作以及其他一些操作,但是每次都会截断文件:

I was trying this and a few other things but it truncates the file each time:

my $file = 'primes.txt';
sub MAIN ( Int:D $low, Int:D $high where * >= $low ) {
    unless my $fh = open $file, :w, :append {
        die "Could not open '$file': {$fh.exception}";
    }

    for $low .. $high {
        $fh.put: $_ if .is-prime;
    }
}

将其更改为open $file, :a似乎也会截断该文件. 这是macOS上的2018.04.

Changing this to open $file, :a also seems to truncate the file. This is 2018.04 on macOS.

推荐答案

Perl6 &open语义基于 POSIX ,具有以下映射:

Perl6 &open semantics are based on POSIX, with the following mapping:

:mode<ro>  --> O_RDONLY
:mode<wo>  --> O_WRONLY
:mode<rw>  --> O_RDWR
:create    --> O_CREAT
:append    --> O_APPEND
:truncate  --> O_TRUNC
:exclusive --> O_EXCL

为方便起见,提供了以下快捷方式:

For convenience, the following shortcuts are provided:

:r      --> :mode<ro>
:w      --> :mode<wo>, :create, :truncate
:x      --> :mode<wo>, :create, :exclusive
:a      --> :mode<wo>, :create, :append
:update --> :mode<rw>
:rw     --> :mode<rw>, :create
:rx     --> :mode<rw>, :create, :exclusive
:ra     --> :mode<rw>, :create, :append

并非Rakudo支持的所有平台(例如Windows,JVM,甚至POSIX本身)均不支持模式和标志的所有可能组合,因此只能保证上述组合的行为符合预期(或至少应假定那样) ).

Not all platforms supported by Rakudo (eg Windows, JVM, not even POSIX itself) support all possible combinations of modes and flags, so only the combinations above are guaranteed to behave as expected (or are at least supposed to behave that way).

长话短说,一个简单的:a绝对可以完成您想要的操作,并且在我的Windows机器上也可以执行.如果它确实在MacOS上被截断,我会认为是一个错误.

Long story short, a simple :a absolutely should do what you want it to do, and it does so on my Windows box. If it really truncates on MacOS, I'd consider that a bug.

这篇关于如何在Perl 6中附加文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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