Hello,

Thanks a lot Chas, Jenda , Venkat...the solution (eval)works fine, now
that I have implemented it. There seem to be some issues though:

1. Perl doest seem to catch errors like divide-by-zero error. Eg: for an
input expression like 99 / 0, it simply displays nothing as output. (no
errors thrown).
2. Typecast variables are considered to be erroneous inputs. Eg: an
input like ' (int)var1' in the expression is not evaluated as a typecast
variable.

Is designing a separate parser for this the only solution that I have?

Thanks again,
Adarsh

P.S- If at all some kind of disclaimer is coming is coming with these
mails of mine, please excuse me for that. I am trying to get them
removed but they keep attaching automatically.



-----Original Message-----
From: Chas. Owens [mailto:[EMAIL PROTECTED] 
Sent: Saturday, December 29, 2007 10:54 PM
To: Jenda Krynicky
Cc: beginners@perl.org
Subject: Re: converting text expressions (like "1+1") to values

On Dec 29, 2007 11:34 AM, Jenda Krynicky <[EMAIL PROTECTED]> wrote:
> From: "Chas. Owens" <[EMAIL PROTECTED]>
> > On Dec 28, 2007 10:15 AM, Adarsh Srivastava
> > <[EMAIL PROTECTED]> wrote:
> > > Hello,
> > >
> > > Is there any inbuilt/external function or library that can convert
a text
> > > expression (eg. "22 + 23") and evaluate the resulting value?( 45
in this
> > > case).
> > snip
> >
> > Well, the string form of eval will do this; however, it is very
> > dangerous.  What if the string contained valid Perl code* to do
> > something on your system?  Any time you use the string form of eval
> > you should first run the string through a regex make sure it only
> > contains things you expect it to.
>
> Another way to restrict what the evaled code may do is to use the
> Safe.pm module.
>
> use Safe;
>
> $safe = new Safe;
> $safe->reval("22+23");
snip

Nice, and it is even part of Core Perl (I really need to sit down and
go over corelist), but the default opmask isn't safe enough.  To quote
the perldoc for Opode, "If safety matters to you (and why else would
you be using the Opcode module?) then you should not rely on the
definition of this, or indeed any other, optag!".  Given this problem
I would say the following code is appropriate; however, all of this
assumes that the string to be eval'ed will be valid Perl code.  If the
expressions you are getting expect to be able to use x^y or pow(x,y)
instead of x**y for raising x to y, you will still need to write your
own parser.

use strict;
use warnings;
use Safe;

our $matheval = Safe->new;
$matheval->allow_only(qw<atan2 sin cos exp log sqrt pow multiply
i_multiply divide i_divide modulo i_modulo add i_add subtract
i_substract int abs>);
.
.
.

my $expr = get_expresion();
my $result = $matheval->reval($expr);
die "got error [EMAIL PROTECTED] when eval'ing [$expr]" if $@;

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


The information contained in this e-mail message and in any
attachments/annexure/appendices is confidential to the 
recipient and may contain privileged information. 
If you are not the intended recipient, please notify the
sender and delete the message along with any 
attachments/annexure/appendices. You should not disclose,
copy or otherwise use the information contained in the
message or any annexure. Any views expressed in this e-mail 
are those of the individual sender except where the sender 
specifically states them to be the views of 
Toshiba Embedded Software India Pvt. Ltd. (TESI),Bangalore.

Although this transmission and any attachments are believed to be
free of any virus or other defect that might affect any computer 
system into which it is received and opened, it is the responsibility
of the recipient to ensure that it is virus free and no responsibility 
is accepted by Toshiba Embedded Software India Pvt. Ltd, for any loss or
damage arising in any way from its use.




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to