write a function to return an evaluated string using eval, Help!

J

Jason

I'm trying to write a fuction to take any string and resolve it by
replacing any simple string scalar in it with its value and keep
everything else the same, eg:

$resolved=evalString($beforeResolve) #there may be . or \ etc in
$beforeResolve which I want to keep it as it is.

Here is the full test code I used, which doesn't work:
sub evalString {

my $beforeResolve=shift;
my $resolved;
my $evalString=q/$resolved="$beforeResolve";/;

eval $evalString;

return $resolved;

}

$str1="test1";
$str2="test2";
$beforeResolve='$str1.$str2'; #I am expecting resoved results like
this: test1.test2
print "resolved:" . evalString($beforeResolve) . "\n"; #NOT WORKING
????
print eval '$str1.$str2'; #this returns "test1test2" where dot is
gone, not what I want!

Thanks,
Jason
 
J

John W. Krahn

Jason said:
I'm trying to write a fuction to take any string and resolve it by
replacing any simple string scalar in it with its value and keep
everything else the same, eg:

$resolved=evalString($beforeResolve) #there may be . or \ etc in
$beforeResolve which I want to keep it as it is.

perldoc -q "How can I expand variables in text strings"


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top