Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to execute an if statement defined in a string? 2

Status
Not open for further replies.

gnewitall

Programmer
Sep 15, 2005
6
Given,

$string = "if ((33 * 11) > 1500) { $result = 1; } else { $result = 0; }"

How do I execute the if statement within the same perl script the string is defined in?

Thanks in advance.
 
This looks like it should work with [red]eval[/red] - but what is happening is the $result variable is being interpolated due to the double quotes. As $result has no value, at present, this is what would be evaluated:-

if ((33 * 11) > 1500) { = 1; } else { = 2; }

However, if you put this in single quotes the interpolation will not take place, and the $result scalars will not be treated as scalars at all, but will be treated literally - i.e. the $ will have no special meaning. You can do the same by leaving within double quotes and escaping the dollar signs:-

$string = "if ((33 * 11) > 1500) { \$result = 1; } else { \$result = 2; }";

Then you can proceed to evaluate the if statement:-

eval $string

... then finally print $result - which in this case will return 0 - the else part of the if statement


Kind Regards
Duncan
 
Or, just use single quotes:
Code:
$string = 'if ((33 * 11) > 1500) { $result = 1; } else { $result = 0; }';
eval $string;
print $result;
 
Or, just ignore what I said.

I skimmed his post, apparently I can't read today... more coffee for me!
 
LOL! Thanks for sticking up for me Trojan! :)

rharsh - no offence taken!


Kind Regards
Duncan
 
That's ok.
I do get annoyed when I (and others) post a good answer to a question and others post the same thing. It looks like they're trying to get credit for someone else's efforts.
In rhash's case here, he's just not awake yet.
Can't blame him for that! ;-)



Trojan.
 
Trojan

I think you are being a bit hard on rharsh here - with only six minutes between their posts, you could easily attribute this to a race condition caused by thinking and typing time!

Anyway, while all this eval() stuff is very interesting, I'm concerned as to why it needs to be done in the first place - like soft references, evaluating code snippets is always a risky business, and unless you are writing a debugger I can't really see what sort of design would require it.
 
I agree with steve in wondering why eval is necessary here.
 
Ok stevexff, point taken, maybe he was awake, maybe it was just a "race condition"! ;-)

eval is, indeed, the most dangerous way of doing it but it is also the easiest. After all, you reformat the string to look like perl code and let the perl compiler handle any manner of crazy combinations of expressions. Also the perl engine would generate error messages for you too.

I'm not sure why I'm defending it here since it was not my idea but hey, what the hell! :)

As I stated earlier, I would probably convert to rpn and process from there but that is quite complex by comparison.



Trojan.
 
I would just use single quotes........






(he said running out of the room before the door hits me in the arse!) [bigsmile] [peace] turns to run -> [profile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top