#!/usr/local/bin/perl use strict; use warnings; my %operators = ( '+' => sub { $_[0] + $_[1] }, '-' => sub { $_[0] - $_[1] }, '*' => sub { $_[0] * $_[1] }, '/' => sub { $_[0] / $_[1] } ); $|++; # Flush output automatically print 'First operand: '; chomp (my $operand1 = <>); print 'Operator: '; chomp(my $symbol = <>); print 'Second operand: '; chomp(my $operand2 = <>); die "Unknown operator $symbol\n" if (!exists $operators{$symbol}); print "----------------\nResult: ", $operators{$symbol}->($operand1, $operand2), "\n";