chomp(my $left = ); chomp(my $op = ); chomp(my $right = ); print $op eq '+' ? $left + $right : $op eq '-' ? $left - $right : $op eq '*' ? $left * $right : $op eq '/' ? $left / ($right||1) : "dunno operator '$op'", , "\n"; #### chomp( my $expr = ); # and to parse it with simple split my ($left,$op,$right) = split /\s+/,$expr # next step is code of above #### my %operations = ( '+' => sub { (shift) + (shift) }, '-' => sub { (shift) - (shift) }, '*' => sub { (shift) * (shift) }, '/' => sub { (shift) / (shift||1) }, ); my ($left,$op,$right) = ...; #wherever you get them from print exists $operations{$op} ? $operations{$op}->($left,$right) : "dunno operator '$op'", "\n"; #### chomp(my $expr = ); $expr =~ /(\d+)\s+([+-/*])\s+(\d+)/ or die "invalid input\n"; my ($left,$op,$right) = ($1,$2,$3); # ...