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;
}
}
Changing this to open $file, :a
also seems to truncate the file.
This is 2018.04 on macOS.
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
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).
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.
Using :mode<wo>, :append
works although this wouldn't be the first thing most people are going to reach for when they see :a
:
my $file = 'primes.txt';
sub MAIN ( Int:D $low, Int:D $high where * >= $low ) {
unless my $fh = open $file, :mode<wo>, :append {
die "Could not open '$file': {$fh.exception}";
}
for $low .. $high {
$fh.put: $_ if .is-prime;
}
$fh.close;
}
The problem is that Perl 6 tends to silently ignore named parameters. It also looks like roast/open.t doesn't actually test this stuff through the user interface. It plays various tricks that should probably be unrefactored.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With