Perl 5 version 8.0 documentation
- sprintf FORMAT, LIST
Returns a string formatted by the usual
printfconventions of the C library function
sprintf. See below for more details and see sprintf(3) or printf(3) on your system for an explanation of the general principles.
Perl does its own
sprintfformatting--it emulates the C function
sprintf, but it doesn't use it (except for floating-point numbers, and even then only the standard modifiers are allowed). As a result, any non-standard extensions in your local
sprintfare not available from Perl.
sprintfdoes not do what you probably mean when you pass it an array as your first argument. The array is given scalar context, and instead of using the 0th element of the array as the format, Perl will use the count of elements in the array as the format, which is almost never useful.
sprintfpermits the following universally-known conversions:
- %% a percent sign
- %c a character with the given number
- %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
In addition, Perl permits the following widely-supported conversions:
- %X like %x, but using upper-case letters
- %E like %e, but using an upper-case "E"
- %G like %g, but with an upper-case "E" (if applicable)
- %b an unsigned integer, in binary
- %p a pointer (outputs the Perl value's address in hexadecimal)
- %n special: *stores* the number of characters output so far
- into the next variable in the parameter list
Finally, for backward (and we do mean "backward") compatibility, Perl permits these unnecessary but widely-supported conversions:
- %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
Note that the number of exponent digits in the scientific notation produced by
%Gfor numbers with the modulus of the exponent less than 100 is system-dependent: it may be three or less (zero-padded as necessary). In other words, 1.23 times ten to the 99th may be either "1.23e99" or "1.23e099".
%and the format letter, you may specify a number of additional attributes controlling the interpretation of the format. In order, these are:
- format parameter index
An explicit format parameter index, such as
2$. By default sprintf will format the next unused argument in the list, but this allows you to take the arguments out of order. Eg:
one or more of: space prefix positive number with a space + prefix positive number with a plus sign - left-justify within the field 0 use zeros, not spaces, to right-justify # prefix non-zero octal with "0", non-zero hex with "0x", non-zero binary with "0b"
- vector flag
The vector flag
v, optionally specifying the join string to use. This flag tells perl to interpret the supplied string as a vector of integers, one for each character in the string, separated by a given string (a dot
.by default). This can be useful for displaying ordinal values of characters in arbitrary strings:
- printf "version is v%vd\n", $^V; # Perl's version
Put an asterisk
vto override the string to use to separate the numbers:
You can also explicitly specify the argument number to use for the join string using eg
- printf '%*4$vX %*4$vX %*4$vX', @addr[1..3], ":"; # 3 IPv6 addresses
- (minimum) width
Arguments are usually formatted to be only as wide as required to display the given value. You can override the width by putting a number here, or get the width from the next argument (with
*) or from a specified argument (with eg
If a field width obtained through
*is negative, it has the same effect as the
- precision, or maximum width
You can specify a precision (for numeric conversions) or a maximum width (for string conversions) by specifying a
.followed by a number. For floating point formats, this specifies the number of decimal places to show (the default being 6), eg:
For integer conversions, specifying a precision implies that the output of the number itself should be zero-padded to this width:
For string conversions, specifying a precision truncates the string to fit in the specified width:
You can also get the precision from the next argument using
You cannot currently get the precision from a specified number, but it is intended that this will be possible in the future using eg
- printf '<%.*2$x>', 1, 6; # INVALID, but in future will print "<000001>"
For numeric conversions, you can specify the size to interpret the number as using
ll. For integer conversions, numbers are usually assumed to be whatever the default integer size is on your platform (usually 32 or 64 bits), but you can override this to use instead one of the standard C types, as supported by the compiler used to build Perl:
- l interpret integer as C type "long" or "unsigned long"
- h interpret integer as C type "short" or "unsigned short"
- q, L or ll interpret integer as C type "long long" or "unsigned long long"
- (if your platform supports such a type, else it is an error)
For floating point conversions, numbers are usually assumed to be the default floating point size on your platform (double or long double), but you can force 'long double' with
llif your platform supports them.
The size specifier 'V' has no effect for Perl code, but it supported for compatibility with XS code; it means 'use the standard size for a Perl integer (or floating-point number)', which is already the default for Perl code.
- order of arguments
Normally, sprintf takes the next unused argument as the value to format for each format specification. If the format specification uses
*to require additional arguments, these are consumed from the argument list in the order in which they appear in the format specification before the value to format. Where an argument is specified using an explicit index, this does not affect the normal order for the arguments (even when the explicitly specified index would have been the next argument in any case).
- printf '<%*.*s>', $a, $b, $c;
$afor the width,
$bfor the precision and
$cas the value to format, while:
- print '<%*1$.*s>', $a, $b;
$afor the width and the precision, and
$bas the value to format.
Here are some more examples - beware that when using an explicit index, the
$may need to be escaped:
If Perl understands "quads" (64-bit integers) (this requires either that the platform natively support quads or that Perl be specifically compiled to support quads), the characters
- d u o x X b i D U O
print quads, and they may optionally be preceded by
- ll L q
- %lld %16LX %qo
You can find out whether your Perl supports quads via Config:
If Perl understands "long doubles" (this requires that the platform support long doubles), the flags
- e f g E F G
may optionally be preceded by
- ll L
- %llf %Lg
You can find out whether your Perl supports long doubles via Config: