View Single Post
  #1   (View Single Post)  
Old 16th February 2009
qsecofr qsecofr is offline
Port Guard
 
Join Date: May 2008
Location: Portland
Posts: 18
Default perl expression syntax

Hi,

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.

something like
Code:
my $a;
my $b;
my $c;
my $result;
my $row;

# do stuff.  in the process setting $a = 5, $b = 6, $c = 2
...

# at this point assume $a = 5, $b = 6, $c = 2, expression: '($a * $b) / $c'
$statement->execute($parm);
$row = $statement->fetchrow_hashref();

$result = s//$row->{'expression'}/e;
# here i want perl to see there are variables in $row->{'expression'}, and
#  substitute the current values of those variables, and
#  calculate so that $result == 15.
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.

Any perl expertise much appreciated!
TIA
Reply With Quote