Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

stu42j (6348)

  (email not shown publicly)

Journal of stu42j (6348)

Thursday May 31, 2007
10:25 PM

Delayed String Interpolation (aka, always check the FAQ)

[ #33395 ]

I was trying to fix some code today that was attempting to do delayed string interpolation. Something like:

$str = 'Foo: $foo';  # Actually defined in a config file.
$foo = 'bar';  # ...later
print eval("\$str"), "\n";

Except that just gives you:

Foo: $foo

My first thought was to check the FAQ but then, no, I think I've seen something like this in the eval doc. No, that's not helpful. Oops, gotta catch the bus...

Then I figure I'd take a shot at it before bed. I give Google a try but the closest hits are on Ruby. After an a-ha moment I came up with this:

print eval(qq{"$str"}), "\n";

Which works. But then as I start writing this post, I figure I'd better check the FAQ before I sound like an idiot. Yes, it is in the FAQ although my solution seems much simpler. Am I overlooking any problems with my solution?

Here's what the FAQ suggests:

eval { $text =~ s/(\$\w+)/$1/eeg };
die if $@;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.