我拿到了以下代码来尝试文件的打开和写入(不使用spurt):
sub MAIN {
my $h = open 'somefile.txt', :w, :a;
for 1..4 {
$fh.put: "hello";
}
$fh.close;
}
我期望的是每次运行都会在文件中追加4行带有“hello”的内容。但是似乎它仍然会覆盖文件,在运行2次或更多次后仍只有4行。
$ perl6 opening.p6
$ cat somefile.txt
hello
hello
hello
hello
$ perl6 opening.p6
$ cat somefile.txt
hello
hello
hello
hello
添加或移除 :a
或 :append
似乎不会影响这种行为,我错过了什么吗?
open
调用中仅使用:r
、:w
、:a
、:x
、:update
、:rw
、:ra
或:rx
中的 一个 模式。您不应将它们组合使用,也不应使用其他模式。 - raiph