Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Is there a Variable to literal expression shortcut?

by true (Pilgrim)
on May 29, 2003 at 08:32 UTC ( [id://261511]=perlquestion: print w/replies, xml ) Need Help??

true has asked for the wisdom of the Perl Monks concerning the following question:

Not sure how to put this in words, forgive me for any mis-direction. Is there a shorthand way of converting a variable into a literal math expression? The goal being to solve the equation. Example:
my $var = "(2+2)"; my $result = (2+2); print $result;
Up till now i've written routines that split each equation by sign and solves from left to right. Wondering if there is a faster way. Thanks for reading.

jtrue

Replies are listed 'Best First'.
Re: Is there a Variable to literal expression shortcut?
by broquaint (Abbot) on May 29, 2003 at 09:24 UTC
    And as always there's a module on CPAN to do this very thing
    use Math::Expression; my $me = Math::Expression->new; print $me->Eval( $me->Parse("2 + 2") ); __output__ 4
    Not as slimline as string eval, but not as potentially dangerous either :) See. the Math::Expression docs for more info.
    HTH

    _________
    broquaint

Re: Is there a Variable to literal expression shortcut?
by jepri (Parson) on May 29, 2003 at 08:37 UTC
    You are going to be delighted at this:

    my $expression = "(2+2)"; my $result = eval $expression;

    You can have a lot of fun with eval.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: Is there a Variable to literal expression shortcut?
by shotgunefx (Parson) on May 29, 2003 at 09:10 UTC
    I'd be wary of where your input is coming from. If it's user supplied and you use eval(), they can type any command in there with potentially disasterous results. If you go this route, you want to check out Safe.

    You might find the snippet at Evaluate Expressions. does the trick. There are also modules on CPAN. Look for the term Expression as I can't recall the names off the top of my head.

    -Lee

    "To be civilized is to deny one's nature."
Re: Is there a Variable to literal expression shortcut?
by Bilbo (Pilgrim) on May 29, 2003 at 09:14 UTC

    eval certainly does what you want. I assume that your expression is coming from some sort of user input. If so then you might want to check what it actually contains because eval will just run it as a perl program, so a user could do things that you don't want them to. You might want to do something like checking that the string contains only certain characters:

    my @tests = ("1 + 2", "2*3", "(2 + 5) / 3", "print 'a test', "E = m * c**2", "3**2 + 2"); for my $expr (@tests) { # Does the string contain any characters that we # don't want? if ($expr =~ m![^0-9 + \- / * \( \) \s]!x) { print "$expr: Invalid\n"; } else { print "$expr = ", eval $expr, "\n"; } }
Re: Is there a Variable to literal expression shortcut?
by Enlil (Parson) on May 29, 2003 at 08:40 UTC
    If i understand you correctly, what you are looking for is eval
    For example:
    use strict; use warnings; my $var = "(2+2)"; my $result = eval $var; print $result;
    -enlil
Re: Is there a Variable to literal expression shortcut?
by eyepopslikeamosquito (Archbishop) on May 29, 2003 at 08:38 UTC

    I don't really understand your question very well, but will eval do what you want? For example:

    my $var = "(2+2)"; print eval $var;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://261511]
Approved by jepri
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found