| 1 | package Badger::Comparable; | ||
| 2 | |||
| 3 | use Badger::Class | ||
| 4 | 3 | version => 0.01, | |
| 5 | debug => 0, | ||
| 6 | import => 'CLASS', | ||
| 7 | base => 'Badger::Base', | ||
| 8 | utils => 'numlike is_object', | ||
| 9 | methods => { | ||
| 10 | eq => \&equal, | ||
| 11 | ne => \¬_equal, | ||
| 12 | lt => \&before, | ||
| 13 | gt => \&after, | ||
| 14 | le => \¬_after, | ||
| 15 | ge => \¬_before, | ||
| 16 | cmp => \&compare, | ||
| 17 | }, | ||
| 18 | overload => { | ||
| 19 | '==' => \&equal, | ||
| 20 | '!=' => \¬_equal, | ||
| 21 | '<' => \&before, | ||
| 22 | '>' => \&after, | ||
| 23 | '<=' => \¬_after, | ||
| 24 | '>=' => \¬_before, | ||
| 25 | '<=>' => \&compare, | ||
| 26 | fallback => 1, | ||
| 27 | 3 | ▶ | }; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 28 | |||
| 29 | |||
| 30 | sub compare { | ||
| 31 | 0 | ▶ | my $self = shift; |
|
Subroutine
compare: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 32 | 0 | shift->not_implemented; | |
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | sub equal { | ||
| 37 | 0 | ▶ | shift->compare(@_) == 0; |
|
Subroutine
equal: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | sub not_equal { | ||
| 42 | 0 | ▶ | shift->compare(@_) != 0; |
|
Subroutine
not_equal: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | sub before { | ||
| 47 | 4 | ▶ | shift->compare(@_) == -1; |
|
Subroutine
before: called CC=1 SCAR=0.0
Pod
documented
| |||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | sub after { | ||
| 52 | 4 | ▶ | shift->compare(@_) == 1; |
|
Subroutine
after: called CC=1 SCAR=0.0
Pod
documented
| |||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | sub not_before { | ||
| 57 | 1 | ▶ | shift->compare(@_) >= 0; |
|
Subroutine
not_before: called CC=1 SCAR=0.0
Pod
documented
| |||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | sub not_after { | ||
| 62 | 1 | ▶ | shift->compare(@_) <= 0; |
|
Subroutine
not_after: called CC=1 SCAR=0.0
Pod
documented
| |||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | 3 | 1; | |
| 67 | |||
| 68 | |||
| 69 | =head1 NAME | ||
| 70 | |||
| 71 | Badger::Comparable - base class for comparable objects | ||
| 72 | |||
| 73 | =head1 SYNOPSIS | ||
| 74 | |||
| 75 | package Your::Comparable::Object; | ||
| 76 | use base 'Badger::Comparable'; | ||
| 77 | |||
| 78 | # You must define a compare method that returns -1, 0 or +1 | ||
| 79 | # if the object is less than, equal to, or greater than the | ||
| 80 | # object passed as an argument. | ||
| 81 | |||
| 82 | sub compare { | ||
| 83 | my ($this, $that) = @_; | ||
| 84 | |||
| 85 | # for example: comparing by a surname field | ||
| 86 | return $this->surname | ||
| 87 | cmp $that->surname; | ||
| 88 | } | ||
| 89 | |||
| 90 | package main; | ||
| 91 | |||
| 92 | # assume $obj1 and $obj2 are instance of above object class | ||
| 93 | if ($obj1 < $obj2) { | ||
| 94 | # do something | ||
| 95 | } | ||
| 96 | |||
| 97 | =head1 DESCRIPTION | ||
| 98 | |||
| 99 | This module implements a base class for comparable objects. Subclasses need | ||
| 100 | only define a L<compare()> method and can inherit all the other methods | ||
| 101 | provided. Overloaded comparison operators are also defined. | ||
| 102 | |||
| 103 | =head1 METHODS | ||
| 104 | |||
| 105 | =head2 compare($that) | ||
| 106 | |||
| 107 | This method must be defined by subclasses. It received the implicit C<$self> | ||
| 108 | object reference as the first argument and the object it is being compared to | ||
| 109 | as the second. | ||
| 110 | |||
| 111 | The method can do whatever is necessary to compare the two objects. It should | ||
| 112 | return C<-1> if the C<$self> object should be ordered I<before> the C<$that> | ||
| 113 | object, C<+1> if it should be ordered I<after>, or 0 if the two objects are | ||
| 114 | considered the same. | ||
| 115 | |||
| 116 | =head2 equal($that) | ||
| 117 | |||
| 118 | Wrapper around L<compare()> that returns true if the two objects are equal | ||
| 119 | (L<compare()> returns C<0>). | ||
| 120 | |||
| 121 | =head2 not_equal($that) | ||
| 122 | |||
| 123 | Wrapper around L<compare()> that returns true if the two objects are not | ||
| 124 | equal (L<compare()> returns any non-zero value). | ||
| 125 | |||
| 126 | =head2 before($that) | ||
| 127 | |||
| 128 | Wrapper around L<compare()> that returns true if the C<$self> object is ordered | ||
| 129 | before the C<$that> object passed as an argument (L<compare()> returns C<-1>). | ||
| 130 | |||
| 131 | =head2 not_before($that) | ||
| 132 | |||
| 133 | Wrapper around L<compare()> that returns the logical opposite of the | ||
| 134 | L<before()> method, returning a true value if the C<$self> object is greater | ||
| 135 | than or equal to the L<$that> object passed as an argument (L<compare()> | ||
| 136 | returns C<0> or C<+1>). | ||
| 137 | |||
| 138 | =head2 after($that) | ||
| 139 | |||
| 140 | Wrapper around L<compare()> that returns true if the C<$self> object is ordered | ||
| 141 | after the C<$that> object passed as an argument (L<compare()> returns C<+1>). | ||
| 142 | |||
| 143 | =head2 not_after($that) | ||
| 144 | |||
| 145 | Wrapper around L<compare()> that returns the logical opposite of the | ||
| 146 | L<after()> method, returning a true value if the C<$self> object is less | ||
| 147 | than or equal to the L<$that> object passed as an argument (L<compare()> | ||
| 148 | returns C<-1> or C<0>). | ||
| 149 | |||
| 150 | =head1 OVERLOADED OPERATORS | ||
| 151 | |||
| 152 | =head2 == | ||
| 153 | |||
| 154 | This is mapped to the L<equal()> method. | ||
| 155 | |||
| 156 | if ($obja == $objb) { | ||
| 157 | # do something | ||
| 158 | } | ||
| 159 | |||
| 160 | =head2 != | ||
| 161 | |||
| 162 | This is mapped to the L<not_equal()> method. | ||
| 163 | |||
| 164 | if ($obja != $objb) { | ||
| 165 | # do something | ||
| 166 | } | ||
| 167 | |||
| 168 | =head2 < | ||
| 169 | |||
| 170 | This is mapped to the L<before()> method. | ||
| 171 | |||
| 172 | if ($obja < $objb) { | ||
| 173 | # do something | ||
| 174 | } | ||
| 175 | |||
| 176 | =head2 > | ||
| 177 | |||
| 178 | This is mapped to the L<after()> method. | ||
| 179 | |||
| 180 | if ($obja > $objb) { | ||
| 181 | # do something | ||
| 182 | } | ||
| 183 | |||
| 184 | =head2 <= | ||
| 185 | |||
| 186 | This is mapped to the L<not_after()> method. | ||
| 187 | |||
| 188 | if ($obja <= $objb) { | ||
| 189 | # do something | ||
| 190 | } | ||
| 191 | |||
| 192 | =head2 >= | ||
| 193 | |||
| 194 | This is mapped to the L<not_before()> method. | ||
| 195 | |||
| 196 | if ($obja >= $objb) { | ||
| 197 | # do something | ||
| 198 | } | ||
| 199 | |||
| 200 | =head1 AUTHOR | ||
| 201 | |||
| 202 | Andy Wardley L<http://wardley.org> | ||
| 203 | |||
| 204 | =head1 COPYRIGHT | ||
| 205 | |||
| 206 | Copyright (C) 2013 Andy Wardley. All Rights Reserved. | ||
| 207 | |||
| 208 | This module is free software; you can redistribute it and/or modify it | ||
| 209 | under the same terms as Perl itself. | ||
| 210 | |||
| 211 | =cut | ||