我正在尝试打开一个文件进行编写和使用printf来进行格式化,但文档和现实似乎并不一致.我错过了什么吗?
To exit type 'exit' or '^D'
> my $fh=open "test", :w;
IO::Handle<"test".IO>(opened, at octet 0)
> $fh.printf: "test";
No such method 'printf' for invocant of type 'IO::Handle'
in block <unit> at <unknown file> line 1
Run Code Online (Sandbox Code Playgroud)
但根据文档我的代码似乎没问题:
https://docs.perl6.org/routine/printf
Run Code Online (Sandbox Code Playgroud)
非常感谢你 !!
文档中的printf()示例对我也不起作用:
~/p6_programs$ perl6 -v
This is Rakudo version 2016.11 built on MoarVM version 2016.11
implementing Perl 6.c.
~/p6_programs$ cat 4.pl6
my $fh = open 'outfile.txt', :w;
$fh.printf: "The value is %d\n", 32;
$fh.close;
~/p6_programs$ perl6 4.pl6
No such method 'printf' for invocant of type 'IO::Handle'
in block <unit> at 4.pl6 line 3
Run Code Online (Sandbox Code Playgroud)
您可以将其sprintf()用作解决方法:
my $fh = open 'outfile.txt', :w;
$fh.say: sprintf "The value is %d", 32;
$fh.close;
Run Code Online (Sandbox Code Playgroud)
或者fmt():
my $fh = open 'outfile.txt', :w;
$fh.say: 32.fmt("The value is %d");
$fh.close;
Run Code Online (Sandbox Code Playgroud)