Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: '+' to +

by nobull (Friar)
on May 29, 2005 at 12:47 UTC ( [id://461504]=note: print w/replies, xml ) Need Help??


in reply to Re: '+' to +
in thread '+' to +

Perhaps you could make the regexp ensure the operator is only one of the four specified operations and then performing an eval on the reconstructed operation?

I agree about laundering the input but I'd want to check that the whole input matches the expected pattern.

if ( my ($Code) = $Problem =~ /^\s*(\d+\s*[-+/*]\s*\d+)\s*=\s*$/ ) { if ( defined ( my $Answer = eval $Code ) ) { # do stuff with $Answer } else { # do stuff with $@ } } else { # Complain about invalid format }

I'd even tend to be a bit more liberal with my laundering to let though any simple arithmetic /^([-+/*\s\d().]+)=\s*$/.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://461504]
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: (3)
As of 2024-04-19 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found