Saturday, July 9, 2011

Perl String Interpolation: Evaluating Math Inside Quotes

Q: In Perl, is there a way to do simple arithmetic inside quotes?

Sometimes I may need to do things like the example below, but Perl doesn't interpret it the way I want, and I can only do it with another variable. Is there a trick?

Perl
$i = 5;
print "{$i*10}";

A: I suggest you use printf:
Perl
printf("%s", $i * 10);

No comments: