我正在尝试这个以及其他一些东西,但它每次都会截断文件:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
将此更改为open $file, :a似乎也会截断文件.这是关于macOS的2018.04.
Perl6 &open语义基于POSIX,具有以下映射:
:mode<ro> --> O_RDONLY
:mode<wo> --> O_WRONLY
:mode<rw> --> O_RDWR
:create --> O_CREAT
:append --> O_APPEND
:truncate --> O_TRUNC
:exclusive --> O_EXCL
Run Code Online (Sandbox Code Playgroud)
为方便起见,提供了以下快捷方式:
: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
Run Code Online (Sandbox Code Playgroud)
并非Rakudo支持的所有平台(例如Windows,JVM,甚至POSIX本身)都不支持模式和标志的所有可能组合,因此只保证上述组合的行为符合预期(或者至少应该以这种方式表现).
简而言之,一个简单的:a绝对应该做你想要它做的事情,它在我的Windows盒子上这样做.如果它真的在MacOS上截断,我会认为这是一个错误.
使用:mode<wo>, :append作品,尽管这并不是大多数人在看到以下内容时首先想到的: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;
}
Run Code Online (Sandbox Code Playgroud)
问题是 Perl 6 倾向于默默地忽略命名参数。看起来,roast/open.t实际上并没有通过用户界面测试这些东西。它使用了各种可能不应该重构的技巧。
| 归档时间: |
|
| 查看次数: |
268 次 |
| 最近记录: |