use warnings; use strict; my %arith; $arith{"+"} = sub { $_[0] + $_[1] }; $arith{"-"} = sub { $_[0] - $_[1] }; $arith{"*"} = sub { $_[0] * $_[1] }; $arith{"/"} = sub { $_[0] / $_[1] }; my $problem = "6 + 1"; my $answer; if ( $problem =~ /^(\d+)\s*(\S)\s*(\d+)$/ and exists $arith{$2} ) { eval { $answer = $arith{$2}->( $1, $3 ); } } else { die "Oops!\nOperation syntax not understood.\n"; } die "Woops!\n$@" if $@; print "$answer\n";