Jim Gibson wrote:

On Jun 13, 2013, at 10:51 AM, lee wrote:

+ Is "print" and "printf" pretty much the same thing implementation
  wise?  I'm wondering if "printf" might involve more overhead so it
  might be less efficient, depending on what you're doing.

They are pretty much the same. print will use its default formats for
converting numerical data to strings. printf will use the format
specifiers you provide. There can't be much difference in execution
speed, probably not even enough to measure.

print and printf both use filehandles the same:

$ perl -Mwarnings -Mstrict -e'
print  STDERR "Hello print\n";
printf STDERR q/%s/, "Hello printf\n";
'
Hello print
Hello printf


Perl allows interpolation in double quoted strings so you shouldn't use it in printf's format string:

$ perl -Mwarnings -Mstrict -e'
my ( $x, $y, $z ) = ( "10%", "15%", "26%" );
print  "Hello $x, $y, $z\n";
printf "Hello $x, $y, $z\n";
'
Hello 10%, 15%, 26%
Invalid conversion in printf: "%," at -e line 4.
Invalid conversion in printf: "%," at -e line 4.
Invalid conversion in printf: "%\012" at -e line 4.
Hello 10%, 15%, 26%


print is affected by the variables $, and $\ but printf isn't:

$ perl -Mwarnings -Mstrict -e'
my ( $x, $y, $z ) = ( "10%", "15%", "26%" );
( $,, $\ ) = ( "<*MIDDLE*>", "<*THE END*>" );
print  "Hello", $x, $y, $z, "\n";
printf "Hello %s, %s, %s\n", $x, $y, $z;
'
Hello<*MIDDLE*>10%<*MIDDLE*>15%<*MIDDLE*>26%<*MIDDLE*>
<*THE END*>Hello 10%, 15%, 26%




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to