Perl6 IO :: Handle doesn't have a printf method, doesn't match the documentation, or did I miss something?

I am trying to open a file for writing and use printf for formatting, but the documentation and reality do not seem to be consistent. Did I miss something?

 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 

But my code looks ok according to the documentation:

 https://docs.perl6.org/routine/printf 

Many thanks!

+6
source share
2 answers

The printf() example in the docs doesn't work for me:

 ~/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 

You can use sprintf() as a workaround:

 my $fh = open 'outfile.txt', :w; $fh.say: sprintf "The value is %d", 32; $fh.close; 

or fmt() :

 my $fh = open 'outfile.txt', :w; $fh.say: 32.fmt("The value is %d"); $fh.close; 
+7
source

Apparently IO::Handle.printf was added on November 27, 2016 , and Rakudo 2016.11 was marked on November 19 , so I assume your Rakudo is older than that.

+8
source

Source: https://habr.com/ru/post/1013901/


All Articles