Rules / Types, classes & roles / basic

Format Reference

Convert values to a string given a format specification

What it is #

Routines #

Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.

method new #

Creates a new Format object from a sprintf compatible format string.

method Callable #

method Callable(--> Callable:D)

Returns Callable:D.

Returns the Callable that was created from the given format. Intended for introspection purposes only, as one can call the Format object directly.

method directives #

method directives(--> List:D)

Returns List:D.

Returns a list of the directives seen in the given format. Intended for introspection purposes.

method arity #

method arity(--> List:D)

Returns List:D.

Returns the minimal number of positional arguments that is needed for this format. Intended for introspection purposes.

method count #

method count(--> List:D)

Returns List:D.

Returns the maximal number of positional arguments that is needed for this format. Intended for introspection purposes.

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

2 no-output · 4 ok · 1 rakupp-differs

class Format { }

Not executed: the documentation states no expected output for this example.

use v6.e.PREVIEW;
my $f = Format.new("'%5s'");
say $f;                         # OUTPUT: «'%5s'␤»
say $f("foo");                  # OUTPUT: «'  foo'␤»
Output
'%5s'
'  foo'

Documentation, Rakudo and Raku++ all agree.

method new($format --> Format:D)

Not executed: the documentation states no expected output for this example.

use v6.e.PREVIEW;
my $d = Format.new("%05d");
say $d;                         # OUTPUT: «%05d␤»
say $d(42);                     # OUTPUT: «00042␤»
Output
%05d
00042

Documentation, Rakudo and Raku++ all agree.

use v6.e.PREVIEW;
my $d = Format.new("%05d%3x:%s");
say $d.directives;              # OUTPUT: «(d x s)␤»
Documentation
(d x s)
Rakudo
(d x s)
Raku++

Raku++ disagrees with both the documentation and Rakudo — a defect.

use v6.e.PREVIEW;
my $d = Format.new("%05d%3x:%s");
say $d.arity;                   # OUTPUT: «3␤»
Output
3

Documentation, Rakudo and Raku++ all agree.

use v6.e.PREVIEW;
my $d = Format.new("%05d%3x:%s");
say $d.count;                   # OUTPUT: «3␤»
Output
3

Documentation, Rakudo and Raku++ all agree.