parthi parthi - 7 months ago 24
Perl Question

Difference between printf, print,sprintf in perl?

What is the specific usage of

print, printf, sprintf
in Perl?
All three keywords used for printing purpose, but can someone differentiate it briefly?
Thanks so much in advance.

Answer

Short:

See the manuals:

Long:

print is the default output function. It does no formatting but may append a line break if Perl is called with -l:

print $foo;
print "Hello $world";
print $filehandle $something;

sprintf is a formatter and doesn't do any printing at all:

$result = sprintf('The %s is %d', 'answer', 42);

printf is the same as sprintf, but actually prints the result:

printf 'This is question %d on %s', 36882022, 'StackOverflow';

See the sprintf documentation for more details on valid placeholders/format strings.

Comments