Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Is there a Variable to literal expression shortcut?

by Bilbo (Pilgrim)
on May 29, 2003 at 09:14 UTC ( [id://261518]=note: print w/replies, xml ) Need Help??


in reply to Is there a Variable to literal expression shortcut?

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"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://261518]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-19 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found