Perl 5 version 6.1 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 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".
Perl permits the following universally-known flags between the
%and the conversion letter:
- 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"
- number minimum field width
- .number "precision": digits after decimal point for
- floating-point, max length for string, minimum length
- for integer
- l interpret integer as C type "long" or "unsigned long"
- h interpret integer as C type "short" or "unsigned short"
- If no flags, interpret integer as C type "int" or "unsigned"
There are also two Perl-specific flags:
- V interpret integer as Perl's standard integer type
- v interpret string as a vector of integers, output as
- numbers separated either by dots, or by an arbitrary
- string received from the argument list when the flag
- is preceded by C<*>
Where a number would appear in the flags, an asterisk (
*) may be used instead, in which case Perl uses the next item in the parameter list as the given number (that is, as the field width or precision). If a field width obtained through
*is negative, it has the same effect as the
vflag is useful for displaying ordinal values of characters in arbitrary strings:
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: