#!/usr/bin/perl -w use strict; use warnings; my $p_operations = { '+' => sub { $_[0] + $_[1] }, '-' => sub { $_[0] - $_[1] }, '*' => sub { $_[0] * $_[1] }, '/' => sub { $_[0] / $_[1] }, }; # Get operation $op, and values $x and $x in whatever fashion # ... # For testing my $x = 12; my $y = 7; my $op = "*"; my $result = $p_operations->{$op}->($x, $y); print "The result of $x $op $y is: $result\n";