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*$/.