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!
You can use the following trick:
Alternatively, you can use
sprintf:Either of these yields the desired string. Arguably,
sprintfis 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: