routine sprintf
Documentation for routine sprintf
assembled from the following types:
language documentation Independent routines
From Independent routines
(Independent routines) routine sprintf
Defined as:
method sprintf(*@args) multi sub sprintf(Cool:D $format, *@args)
Formats and returns a string, following the same language as Str.sprintf
, using as such format either the object (if called in method form) or the first argument (if called as a routine)
sprintf( "%s the %d%s", "þor", 1, "st").put; # OUTPUT: «þor the 1st» sprintf( "%s is %s", "þor", "mighty").put; # OUTPUT: «þor is mighty» "%s's weight is %.2f %s".sprintf( "Mjölnir", 3.3392, "kg").put; # OUTPUT: «Mjölnir's weight is 3.34 kg»
This function is mostly identical to the C library's sprintf
and printf
functions. The only difference between the two functions is that sprintf
returns a string while the printf
function writes to a filehandle. sprintf
returns a Str
, not a literal.
The $format
is scanned for %
characters. Any %
introduces a format token. Directives guide the use (if any) of the arguments. When a directive other than %
is used, it indicates how the next argument passed is to be formatted into the string to be created. Parameter indexes may also be used in the format tokens. They take the form N$
and are explained in more detail below.
The $format
may be defined enclosed in single or double quotes. The double-quoted $format
string is interpolated before being scanned and any embedded string whose interpolated value contains a %
character will cause an exception. For example:
my $prod = "Ab-%x-42"; my $cost = "30"; sprintf("Product $prod; cost: \$%d", $cost).put; # OUTPUT: «Your printf-style directives specify 2 arguments, but 1 argument was supplied» « in block <unit> at <unknown file> line 1»
When handling unknown input you should avoid using such syntax by putting all variables in the *@args
array and have one %
for each in $format
. If you need to include a $
symbol in the format string (even as a parameter index) either escape it or use the single-quoted form. For example, either of the following forms works without error:
sprintf("2 x \$20 = \$%d", 2*20).put; # OUTPUT: «2 x $20 = $40» sprintf('2 x $20 = $%d', 2*20).put; # OUTPUT: «2 x $20 = $40»
In summary, unless you need something very special, you will have fewer unexpected problems by using the single-quoted format string and not using interpolated strings inside the format string.
Directives
% | a literal percent sign |
c | a character with the given codepoint |
s | a string |
d | a signed integer, in decimal |
u | an unsigned integer, in decimal |
o | an unsigned integer, in octal |
x | an unsigned integer, in hexadecimal |
e | a floating-point number, in scientific notation |
f | a floating-point number, in fixed decimal notation |
g | a floating-point number, in %e or %f notation |
X | like x, but using uppercase letters |
E | like e, but using an uppercase "E" |
G | like g, but with an uppercase "E" (if applicable) |
b | an unsigned integer, in binary |
Compatibility:
i | a synonym for %d |
D | a synonym for %ld |
U | a synonym for %lu |
O | a synonym for %lo |
F | a synonym for %f |