user3114045 user3114045 - 1 year ago 89
Perl Question

Difference between Sprintf and printf in Perl

What is the difference between

sprintf
and
printf
in Perl?

I'm really confused with those functions.

I know about
printf
. It is used for
STDOUT
, 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download