On Jan 2, 2008 12:06 AM, Adarsh Srivastava <[EMAIL PROTECTED]> wrote: > > 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). snip
Not true. If you aren't seeing the errors then you aren't checking $@ like I did in my example. Exceptions (die, croak, divide-by-zero, etc) thrown by a chunk of code in an eval (string or block) do not cause the program to halt, but it do set $@ to the error message that would have been sent. Anytime you do an eval you should check the status of this variable: eval $expr; die "got error [EMAIL PROTECTED] eval'ing [$expr]" if $@; snip > 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? snip As I said before, if the expression you want to execute is not a Perl expression you can't eval it. Perl doesn't do typecasting (since it has no types*), so the language is definitely not Perl. You should sit down and figure out what language is being used for your expressions. After you know all of the valid inputs you can determine if that language can be easily transformed into Perl. If it can't be transformed into Perl with a couple of simple regexes you are better off writing a parser. If it comes to that, take a look at Parse::RecDescent**. It might also be possible that the equations were meant for a program you can call from Perl. For instance, here is how Perl can wrap the standard UNIX command bc: #!/usr/bin/perl use strict; use warnings; use IPC::Open3; my ($in, $out, $err); my $pid = open3($in, $out, $err, "bc"); while (my $line = <>) { print $in $line; #send the expression to bc chomp(my $result = <$out>); #get bc's answer print "$result\n"; } snip > 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. snip You should consider using a different address for use with mailing lists then. As you can probably tell, I am partial to Gmail***. * at least not like ANSI C has types ** http://search.cpan.org/dist/Parse-RecDescent/lib/Parse/RecDescent.pm *** https://www.google.com/accounts/NewAccount?service=mail -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/