class Format { }

A Format is an immutable object containing the logic for converting a set of values to a string given a sprintf compatible format specification. Acts as a standard string in every way, except that it can also be called as a Callable with arguments to produce a string, just as sprintf.

Available as of the 2023.06 release of the Rakudo compiler. Requires language level 6.e.

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

Methods§

method new§

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

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

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

method Callable§

method Callable(--> 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 a list of the directives seen in the given format. Intended for introspection purposes.

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

method arity§

method arity(--> List:D)

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

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

method count§

method count(--> List:D)

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

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