use strict; use warnings; my (%ops, $first, $second, $op, $f); %ops = ( '+' => { 'label' => 'Sum', 'op' => sub { return $_[0] + $_[1]; } }, '-' => { 'label' => 'Difference', 'op' => sub { return $_[0] - $_[1]; } }, '*' => { 'label' => 'Product', 'op' => sub { return $_[0] * $_[1]; } }, '/' => { 'label' => 'Quotient', 'op' => sub { return $_[0] / $_[1]; } } ); $ops{'M'} = { 'label' => 'Multiplication', 'table' => 1, 'op' => $ops{'*'}{'op'} }; sub getval { chomp($_ = ); ($_) = m/($_[0])/ if $_[0]; return $_; } while (1) { print "Enter the first number: "; last if !($first = getval('\d+')); print "Enter the second number: "; last if !($second = getval('\d+')); print <($first, $second); } else { print ' ', join ' ', 1..$second; for $f (1..$first) { print "\n"; print join ' ', $f, map { $ops{$op}{'op'}->($f, $_) } 1..$second; } } print "\n\n"; }