DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 16th February 2009
qsecofr qsecofr is offline
Port Guard
 
Join Date: May 2008
Location: Portland
Posts: 18
Thanked 0 Times in 0 Posts
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
  #2   (View Single Post)  
Old 16th February 2009
qsecofr qsecofr is offline
Port Guard
 
Join Date: May 2008
Location: Portland
Posts: 18
Thanked 0 Times in 0 Posts
Default

I may have found the answer here:
http://www.arl.wustl.edu/projects/fp...og/ch05_01.htm

ex.

Code:
# put some code inside $str
$str = '$c = $a + $b'; # Perl doesn't care what's inside $str
$a = 10; $b = 20;
eval $str;             # Treat $str as code, and execute it.
print $c;              # prints 30
Reply With Quote
  #3   (View Single Post)  
Old 16th February 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

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.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.

Last edited by TerryP; 16th February 2009 at 07:49 AM.
Reply With Quote
  #4   (View Single Post)  
Old 16th February 2009
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 2,883
Thanked 190 Times in 160 Posts
Default

Quote:
Originally Posted by qsecofr View Post
Code:
# put some code inside $str
$str = '$c = $a + $b'; # Perl doesn't care what's inside $str
$a = 10; $b = 20;
eval $str;             # Treat $str as code, and execute it.
print $c;              # prints 30
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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ Syntax highlighting in emacs rex FreeBSD General 1 12th October 2008 03:21 AM
PHP regular expression help cajunman4life Programming 2 16th August 2008 05:17 PM
Syntax Highlighting JMJ_coder Programming 17 22nd June 2008 02:24 PM
I need help with make.conf syntax troberts FreeBSD Ports and Packages 4 1st June 2008 03:58 AM
Vi type syntax applications corey_james Off-Topic 9 28th May 2008 04:15 PM


All times are GMT. The time now is 08:06 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick