3

I know in perl you can interpolate scalars by simply doing this:

"This is my $string"

However, I'm wondering if there is a way where I can interpolate actual perl code to be evaluated? An idea of what I want can be seen with ruby strings:

"5 + 4 = #{5 + 4}"

And it will evaluate whatever is in between the {}.

Does anyone know of a way to do this in perl? Thanks!

srchulo
  • 5,143
  • 4
  • 43
  • 72

4 Answers4

12

You can use the following trick:

"5 + 4 = @{[ 5 + 4 ]}"

Alternatively, you can use sprintf:

sprintf("5 + 4 = %d", 5 + 4);

Either of these yields the desired string. Arguably, sprintf is safer than the first one, as you can restrict the type of the interpolated value somewhat. The first one is closer in spirit to what you desire, however.

Further reading:

Community
  • 1
  • 1
Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
2

If you want to interpolate computations in string, you can do 3 things:

  1. Use @{[]}. Inside the third brackets, place whatever expression you want.

  2. Use ${\ do {}}. Place your expression within the 2nd braces.

  3. Use a simple string concatenation: $targetstring = "$string1".func()."$string2", where func() will contain the dynamic content.

SexyBeast
  • 7,913
  • 28
  • 108
  • 196
  • The third one does not behave as desired if `func` returns a list, as it will be evaluated in scalar context. The second one has similar issues. – chepner Jul 31 '12 at 20:45
  • @Srchulo asked for a computation, he didn't mention that it can return a list. Of course then it will be evaluated in scalar context. Read the question carefully. Computation means computing something, not returning a list. – SexyBeast Jul 31 '12 at 20:50
  • Lists can be computed. Computation is not limited to arithmetic. Upon reading the question carefully, I see "interpolate actual perl code to be evaluated". – chepner Jul 31 '12 at 20:51
  • Then that would be stretching its definition. Not wrong, but entirely out of context here. – SexyBeast Jul 31 '12 at 20:53
  • And in fact, even that can be taken care of by the user in the function itself through `wantarray`. In this case, since it's a scalar context, `wantarray` would return `false` and the function can be coded to return the string interpolation of the array instead of the array itself. Under normal circumstances, when the function is called by itself to return the array, `wantarray` would return `true`, and it would return the list. – SexyBeast Jul 31 '12 at 21:01
1
print "5 + 4 = ", 5 + 4;

Simply move it outside the quotes. Is there a point in forcing it to be within the quotes?

You can do a bit more formatting with printf, e.g.:

printf "5 + 4 = %s\n", 5 + 4;
TLP
  • 66,756
  • 10
  • 92
  • 149
-1

You can use eval:

eval('$a = 4 + 5');

Mind you, eval should be used sparingly, it is pretty unsafe.

SexyBeast
  • 7,913
  • 28
  • 108
  • 196
  • 3
    -1. This doesn't give you the desired sting. What was desired is for the value of a computation to be interpolated into a string; this simply evaluates some code. – Sebastian Paaske Tørholm Jul 31 '12 at 18:46
  • 2
    This cannot be interpolated, though. And there is no point in using `eval` for a string that is just as easily written as a statement. Unless you're reading it from a file, or some such. – TLP Jul 31 '12 at 18:48
  • Of course he will be doing something complicated, nobody asks for this thing to do something so simple. I just gave him the idea. – SexyBeast Jul 31 '12 at 18:50
  • 1
    This gives the incorrect result. First `$a` will be interpolated (because of the double quotes), then you'll evaluate an expression like `9 = 4 + 5`, which will fail. – Ken Williams Aug 06 '12 at 14:13
  • Sorry, forgot to give single-quote, gave double-quotes instead. Thanks. – SexyBeast Aug 06 '12 at 14:16