I have found ways of doing this if the variables in the string are simple scalars such as “$foo = 5” using regex. But the issue is what if the variable in the string is:
$foo->{bar} which is equal to 5.
So sample string is:
“This is a string with hash value of $foo->{bar}”.
How can I expand that out to read:
“This is a string with hash value of 5”
Thanks.
Edit for more explanation:
I have a string literal (I believe? I am not the best at the vocab of this) that is “Lorem Ipsum $foo->{bar} Lorem Ipsum” that I received from some text source. I want to take that string, replace all the variable names with the actual value of the variable in my code.
What you have there is called a “template”. As such, you are looking for a templating system.
Assuming those quotes aren’t actually in the string, the only template system I know capable of understanding that templating language is String::Interpolate.
If the quotes are part of the string, what you have there is Perl code. As such, you could get what you want by executing the string. This can be done using
eval EXPR.I strongly recommend against this. I’m not particularly found of String::Interpolate either. Template::Toolkit is probably the popular choice for templating system.