Trying to solve a problem with either the eval or s///e functions, and I haven't succeeded yet.

I want to store an expression/equation in mysql. The expression contains the perl variable names. At runtime, I retrieve the record into a variable. And I evaluate the expression with the variables' current values.

Contrived example, of course. But I hope it's possible to do what I want. I've tried variations of dereferencing with $$variable, eval, and s///e. Haven't found the right syntax to calculate and return the result into a scalar variable.

Generally whenever you want to execute a string or stored exppression like that as a statement, look for a function named eval or something comparable in the language. Programmers and language creators alike are lazy beasts: they often choose the same names for things, just look at how many languages have a print thingy ;-).

Do be mindful though, that when you do eval $str you are basically saying:

local $@;
{
put $str here as if I wrote it myself
and set $@ to contain any exception from perl.
}

So be warey of what you eval EXPR. You wouldn't want to execute eval(`rm -rf /var/db/mysql`) now would you? If you've got a lot of evals to do, you should also note that eval is slow.

Although I can only assume that you are responsible for the veracity of the code inserted into the database, it would still be prudent to check the value of $@after the eval statement to ensure that the string executes without error.

Likewise, it is better to find out early that an error has occurred rather than wait until later when the culprit has been masked many times over.