> whereas this version would need to change to ... <= 0
IMHO for a multiplying approach that's the best check, even for the <=> version.
Simply changing to ... < 0 would check for the open interval excluding the endpoints. And ... == 0 for endpoints only.
But half-open intervals like [$a,$b[ can't be covered with this approach.
So I'd rather stick with
or the newer chained version
and make sure the endpoints are previously swapped if necessary
- ($a,$b) = ($b,$a) if $b < $a;
update
and swapping can be made non-destructive by localizing it to a scope.
C:\tmp\e>perl
($a,$b) = (7,3);
my $x=5;
my $inside = do {
local ($a,$b) = ($b,$a) if $b < $a;
$a <= $x < $b ;
};
print "$x in [$a, $b]? $inside";
__END__
5 in [7, 3]? 1
C:\tmp\e>
(replace local with my for private vars)
|