in reply to interpolating a variable into an operator
Here is my offering, attempting to improve the readability. (Untested - this is just a concept):
IMHO, keeping the operator in the middle of operands makes this more readable.my %ops =( eq => sub{shift eq shift}, '==' => sub{shift == shift}, 'gt' => sub{shift > shift} ); sub compare{ my ($p1, $op, $p2) = @_; return $ops{$op}($p1, $p2); } # Now you can write stuff like if (compare('abc', 'eq', 'ABC'){ ..action }
Of course, there are other ways of doing this, using operator overloading, but I wouldnt go there for this simple case.
Update7/22 Thanks, Anneq: deleted ampersand in front of &$ops... changed sub() to sub{}.
Earth first! (We'll rob the other planets later)
|
|---|