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 $@;
asdlfkjhasdglk (Score:1)
$str = '", print(qq!you lose\n!),"'
eval(qq{"$str"})
# you lose
Re: (Score:1)
Re: (Score:1)
Re: (Score:1)
Re: (Score:1)