SPVM::Comparator(3)
π NAME π
okay so this is SPVM::Comparator(3) from the User Contributed Perl Documentation manual, and it's the interface type for the object comparation callback. iykyk π
β¨ USAGE β¨
Here's the usage example, exactly as it appears β code vibe check passed:
use Comparator;
use Point;
my $comparator = (Comparator)method : int ($object1 : object, $object2 : object); {
my $point1 = (Point)$object1;
my $point2 = (Point)$object2;
if ($point1->x > $point2->x) {
return 1;
}
elsif ($point1->x < $point2->x) {
return -1;
}
else {
return 0;
}
};
my $point1 = Point->new(1, 2);
my $point2 = Point->new(5, 6);
my $result = $comparator->($point1, $point2);
π DESCRIPTION π
Comparator is the interface type for the object comparation callback. that's the whole description, periodt. π
π INTERFACE METHODS π
The required method situation is literally this:
required method : int ($object1 : object, $object2 : object);
This method must receive two objects and return the following value:
- If the first argument is greater than the second argument, returns
1. - If the first argument is lower than the second argument, returns
-1. - If the first argument is equal to the second argument, returns
0.
π CLASS METHODS π
π default_comparator π
static method default_comparator : Comparator ();
Returns a default comparator.
The implementation of the default comparator is:
method : int ($object1 : Comparable, $object2 : Comparable) {
my $cmp = 0;
if ($object1 && $object2) {
$cmp = $object1->cmp($object1, $object2);
}
elsif ($object1) {
$cmp = 1;
}
elsif ($object2 ) {
$cmp = -1;
}
return $cmp;
}
return $default_comparator;
}
If $object1 and $object2 are defined, the comparison is performed by the Comparable#cmp method in the class of $object1, and returns its return value. slay.
If only $object1 is defined, returns 1.
If only $object2 is defined, returns -1.
If both $object1 and $object2 are not defined, returns 0.
π COPYRIGHT & LICENSE π
Copyright (c) 2023 Yuki Kimoto
MIT License
π and that's the whole page, bestie! this one shipped with perl v5.42.2, dated 2026-02-03. byeee! π
π¦ where this page comes from
- alpine 3.24: spvm-doc 0.990125-r0 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms