| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package TEquality; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 5 |  |  | 5 |  | 26 | use strict; | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 162 |  | 
| 4 | 5 |  |  | 5 |  | 26 | use warnings; | 
|  | 5 |  |  |  |  | 7 |  | 
|  | 5 |  |  |  |  | 207 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = '0.31'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 5 |  |  | 5 |  | 31 | use Class::Trait 'base'; | 
|  | 5 |  |  |  |  | 8 |  | 
|  | 5 |  |  |  |  | 110 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our %OVERLOADS = ( | 
| 11 |  |  |  |  |  |  | '==' => "equalTo", | 
| 12 |  |  |  |  |  |  | '!=' => "notEqualTo" | 
| 13 |  |  |  |  |  |  | ); | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | our @REQUIRES = ("equalTo"); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub notEqualTo { | 
| 18 | 3 |  |  | 3 |  | 10 | my ( $left, $right ) = @_; | 
| 19 | 3 |  |  |  |  | 93 | return not $left->equalTo($right); | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | sub isSameTypeAs { | 
| 23 | 2 |  |  | 2 |  | 3 | my ( $left, $right ) = @_; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | # we know the left operand is an object right operand must be an object | 
| 26 |  |  |  |  |  |  | # and either right is derived from the same type as left or left is | 
| 27 |  |  |  |  |  |  | # derived from the same type as right | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 2 |  | 66 |  |  | 19 | return ( ref($right) | 
| 30 |  |  |  |  |  |  | && ( $right->isa( ref($left) ) || $left->isa( ref($right) ) ) ); | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | # this method attempts to decide if an object is exactly the same as one | 
| 34 |  |  |  |  |  |  | # another. It does this by comparing the Perl built-in string representations | 
| 35 |  |  |  |  |  |  | # of a reference and displays the object's memory address. | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | sub isExactly { | 
| 38 | 2 |  |  | 2 |  | 4 | my ( $self, $candidate ) = @_; | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | # $candidate must also be a Comparable object, otherwise there is no way | 
| 41 |  |  |  |  |  |  | # they can be the same.  Along the same veins, we can check very quickly | 
| 42 |  |  |  |  |  |  | # to see if we are dealing with the same objects by testing the values | 
| 43 |  |  |  |  |  |  | # returned by ref(), for if they are not the same, then again, this fails. | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 2 | 50 |  |  |  | 18 | return 0 unless ref($self) eq ref($candidate); | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | # from now on this gets a little trickier...  First we need to test if the | 
| 48 |  |  |  |  |  |  | # objects are Printable, since this will prevent us from being able to get | 
| 49 |  |  |  |  |  |  | # a proper string representation of the object's memory address through | 
| 50 |  |  |  |  |  |  | # normal stringification, and so we will need to call its method | 
| 51 |  |  |  |  |  |  | # stringValue (see the Printable interface for more info) | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 2 | 50 |  |  |  | 14 | return ( $self->stringValue() eq $candidate->stringValue() ) | 
| 54 |  |  |  |  |  |  | if $self->does("TPrintable"); | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | # if the object is not Printable, that means that we can use the built in | 
| 57 |  |  |  |  |  |  | # Perl stringification routine then, so we do just that, if these strings | 
| 58 |  |  |  |  |  |  | # match then the memory address will match as well, and we will know we | 
| 59 |  |  |  |  |  |  | # have the exact same object. | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 2 |  |  |  |  | 14 | return ( "$self" eq "$candidate" ); | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | 1; | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | __END__ |