6

I am trying to open a file for writing and use printf to do formatting, but documentation and reality do not seem to agree. Am I missing 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 seems okay according to documentation:

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

Thank you very much !!

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
lisprogtor
  • 5,677
  • 11
  • 17

2 Answers2

8

Apparently IO::Handle.printf was added on Nov 27, 2016 and Rakudo 2016.11 is tagged on Nov 19. So my guess is your Rakudo is older than that.

CIAvash
  • 716
  • 1
  • 6
  • 5
  • Thanks! I am using the Nov version: perl6 -v This is Rakudo version 2016.11 built on MoarVM version 2016.11 implementing Perl 6.c. – lisprogtor Jan 09 '17 at 10:33
7

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

~/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 could 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;
7stud
  • 46,922
  • 14
  • 101
  • 127