On Sun, Jan 19, 2003 at 06:44:31PM +0100, Jacques Lederer wrote:
> Hello,
>
> When you write
>
> $calc=3+5-2;
> print $calc."\n";
>
> you get 6. (number, not the string "3+5-2")
>
> When you write
>
> while (<STDIN>) {
> $calc=$_;
> print $calc."\n";
> last;
> }
>
> if you run that last one and type 3+5-2, you get 3+5-2.(string
> "3+5-2", not the number 6)
>
> Why is it so? And how can I get it to calculate the thing?
>
> I have gone through all of the perldoc perlfaqs. Maybe the answer is
> so simple that I don't see it. I am just trying to build a simple
> calculator. I have found a rather complicate solution which works, but
> there must be a very simple solution, isn't it?
The input you get is a string. If you want to treat it as a Perl
expression you need C<eval>.
$calc = eval;
This does what you want, plus a lot more.
Be careful not to type "unlink <*>" for example.
Another option is to write the parsing code yourself.
--
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]