in reply to Is it possible to store an arthimetric operator in a variable?
Alternatively, you can use a dispatch table. It's very similar to the solution already provided, except that $op can now be displayed and serialized.
my %ops = ( '==' => sub { $_[0] == $_[1] }, '<' => sub { $_[0] < $_[1] }, ); ... if (...) { $op = '=='; } else { $op = '<'; } ... if ($ops{$op}->($num1, $num2)) { print("$num1 $op $num2\n"; } else { print("$num1 not $op $num2\n"; }
|
|---|