perl6 IO::Handle 没有 printf 方法,与文档不一致,或者我遗漏了什么?
perl6 IO::Handle has no printf method, inconsistent with documentation, or I missed something?
我正在尝试打开一个文件进行写入并使用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
但根据文档,我的代码似乎没问题:
https://docs.perl6.org/routine/printf
非常感谢!!
the docs 中的 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
您可以使用 sprintf()
作为解决方法:
my $fh = open 'outfile.txt', :w;
$fh.say: sprintf "The value is %d", 32;
$fh.close;
或fmt()
:
my $fh = open 'outfile.txt', :w;
$fh.say: 32.fmt("The value is %d");
$fh.close;
显然 IO::Handle.printf
已添加到 Nov 27, 2016 and Rakudo 2016.11 is tagged on Nov 19。所以我猜你的 Rakudo 比那个年龄大。
我正在尝试打开一个文件进行写入并使用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
但根据文档,我的代码似乎没问题:
https://docs.perl6.org/routine/printf
非常感谢!!
the docs 中的 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
您可以使用 sprintf()
作为解决方法:
my $fh = open 'outfile.txt', :w;
$fh.say: sprintf "The value is %d", 32;
$fh.close;
或fmt()
:
my $fh = open 'outfile.txt', :w;
$fh.say: 32.fmt("The value is %d");
$fh.close;
显然 IO::Handle.printf
已添加到 Nov 27, 2016 and Rakudo 2016.11 is tagged on Nov 19。所以我猜你的 Rakudo 比那个年龄大。