"Perl for Perl Newbies" - Part 4The sprintf function

2. The sprintf function

The sprintf built-in function can be used to translate a format string with some conversions embedded inside, and some parameters into a formatted string. Each conversion is specified by the starting character of the percent sign (%), and can have a type and several parameters that will dictate how it will be formatted in the output string. The output string is returned by sprintf.

Here's an example that illustrates its use:


use strict;
use warnings;

print sprintf("Hello \"%s\"! Your lucky number is %i.\n", "Nathan", 65);

The output is:

Hello "Nathan"! Your lucky number is 65.

As you can see, the first conversion is taken from the first argument, and the second one from the second argument. That's how sprintf works: processing the conversions from the arguments in order.

Written by Shlomi Fish