SPVM::Comparator(3)

πŸ“¦ version 0.990125, in alpine 3.24

πŸ’– 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:

  1. If the first argument is greater than the second argument, returns 1.
  2. If the first argument is lower than the second argument, returns -1.
  3. 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 (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