user3114045 user3114045 - 1 year ago 51
Perl Question

Difference between Sprintf and printf in Perl

What is the difference between

in Perl?

I'm really confused with those functions.

I know about
. It is used for
, but I want know in depth of these functions.

Answer Source

sprintf just returns a formatted string, printf prints it to a filehandle.

printf HANDLE "%s", $arg

can (very redundantly) be written as

$formatted = sprintf "%s", $arg
print HANDLE $formatted

Of course, this specific example is most naturally written as

print HANDLE $arg

because the format string I used for an example is so trivial as to be useless.

Of course, HANDLE is optional, and defaults to STDOUT, although you can also change the default with select.