| 1 | package perl5i::2::equal; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | no if $] >= 5.018000, warnings => 'experimental::smartmatch'; | ||
| 6 | |||
| 7 | use perl5i::2::autobox; | ||
| 8 | |||
| 9 | sub are_equal { | ||
| 10 | my ($r1, $r2) = @_; | ||
| 11 | |||
| 12 | # given two scalars, decide whether they are identical or not, | ||
| 13 | # recursing over deep data structures. Since it uses recursion, | ||
| 14 | # traversal is done depth-first. | ||
| 15 | # Warning: complex if-then-else decision tree ahead. It's ordered on | ||
| 16 | # my perceived and anecdotical take on the frequency of occurrence | ||
| 17 | # of each reftype: most popular on top, most rare on the bottom. | ||
| 18 | # This way we return as early as possible. | ||
| 19 | |||
| 20 | # undef eq undef | ||
| 21 | return 1 if !defined $r1 and !defined $r2; | ||
| 22 | |||
| 23 | # One is defined, one isn't | ||
| 24 | return if defined $r1 xor defined $r2; | ||
| 25 | |||
| 26 | my( $ref1, $ref2 ) = (ref $r1, ref $r2); | ||
| 27 | |||
| 28 | if( !$ref1 and !$ref2 ) { | ||
| 29 | my $is_num1 = $r1->is_number; | ||
| 30 | my $is_num2 = $r2->is_number; | ||
| 31 | if( $is_num1 xor $is_num2 ) { | ||
| 32 | # One's looks like a number, the other doesn't. | ||
| 33 | # Can't be equal. | ||
| 34 | return 0; | ||
| 35 | } | ||
| 36 | elsif( $is_num1 ) { | ||
| 37 | # They're both numbers | ||
| 38 | return $r1 == $r2; | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | # They're both strings | ||
| 42 | return $r1 eq $r2; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | elsif( $ref1 eq $ref2 ) { | ||
| 46 | if ( $ref1 ~~ [qw(Regexp GLOB CODE)] ) { | ||
| 47 | return $r1 eq $r2; | ||
| 48 | } | ||
| 49 | elsif ( $ref1 eq 'ARRAY' ) { | ||
| 50 | return _equal_arrays( $r1, $r2 ); | ||
| 51 | } | ||
| 52 | elsif ( $ref1 eq 'HASH' ) { | ||
| 53 | return _equal_hashes( $r1, $r2 ); | ||
| 54 | } | ||
| 55 | elsif ( $ref1 ~~ [qw(SCALAR REF)] ) { | ||
| 56 | return are_equal($$r1, $$r2); | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | # Must be an object | ||
| 60 | return _equal_objects( $r1, $r2 ); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | elsif( $ref1 and $ref2 ) { | ||
| 64 | # They're both refs, but not of the same type | ||
| 65 | my $is_overloaded1 = overload::Overloaded($r1); | ||
| 66 | my $is_overloaded2 = overload::Overloaded($r2); | ||
| 67 | |||
| 68 | if( $is_overloaded1 and $is_overloaded2 ) { | ||
| 69 | # Two overloaded objects | ||
| 70 | return _equal_overload( $r1, $r2 ); | ||
| 71 | } | ||
| 72 | else { | ||
| 73 | # One's an overloaded object, the other is not or | ||
| 74 | # Two plain refs different type or | ||
| 75 | # non-overloaded objects of different type. | ||
| 76 | return 0; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | else { | ||
| 80 | # One is a ref, one is not | ||
| 81 | my $is_overloaded = $ref1 ? overload::Overloaded($r1) | ||
| 82 | : overload::Overloaded($r2); | ||
| 83 | |||
| 84 | if( $is_overloaded ) { | ||
| 85 | # One's an overloaded object, one's a plain scalar | ||
| 86 | return $ref1 ? _equal_overload_vs_scalar($r1, $r2) | ||
| 87 | : _equal_overload_vs_scalar($r2, $r1); | ||
| 88 | } | ||
| 89 | else { | ||
| 90 | # One's a plain ref or object, one's a plain scalar | ||
| 91 | return 0; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _equal_arrays { | ||
| 97 | my ($r1, $r2) = @_; | ||
| 98 | # They can only be equal if they have the same nÂș of elements. | ||
| 99 | return if @$r1 != @$r2; | ||
| 100 | |||
| 101 | foreach my $i (0 .. @$r1 - 1) { | ||
| 102 | return unless are_equal($r1->[$i], $r2->[$i]); | ||
| 103 | } | ||
| 104 | |||
| 105 | return 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub _equal_hashes { | ||
| 109 | my ($r1, $r2) = @_; | ||
| 110 | # Hashes can't be equal unless their keys are equal. | ||
| 111 | return unless ( %$r1 ~~ %$r2 ); | ||
| 112 | |||
| 113 | # Compare the equality of the values for each key. | ||
| 114 | foreach my $key (keys %$r1) { | ||
| 115 | return unless are_equal( $r1->{$key}, $r2->{$key} ); | ||
| 116 | } | ||
| 117 | |||
| 118 | return 1; | ||
| 119 | } | ||
| 120 | |||
| 121 | # Returns the code which will run when the object is used as a string | ||
| 122 | require overload; | ||
| 123 | sub _overload_type { | ||
| 124 | return unless ref $_[0]; | ||
| 125 | my $str = overload::Method($_[0], q[""]); | ||
| 126 | my $num = overload::Method($_[0], "0+"); | ||
| 127 | return "both" if $str and $num; | ||
| 128 | return "" if !$str and !$num; | ||
| 129 | return "str" if $str; | ||
| 130 | return "num" if $num; | ||
| 131 | } | ||
| 132 | |||
| 133 | # Two objects, possibly different classes, both overloaded. | ||
| 134 | sub _equal_overload { | ||
| 135 | my($obj1, $obj2) = @_; | ||
| 136 | |||
| 137 | my $type1 = _overload_type($obj1); | ||
| 138 | my $type2 = _overload_type($obj2); | ||
| 139 | |||
| 140 | # One of them is not overloaded | ||
| 141 | return if !$type1 or !$type2; | ||
| 142 | |||
| 143 | if( $type1 eq 'both' and $type2 eq 'both' ) { | ||
| 144 | return $obj1 == $obj2 || $obj1 eq $obj2; | ||
| 145 | } | ||
| 146 | elsif( | ||
| 147 | ($type1 eq 'num' and $type2 eq 'str') or | ||
| 148 | ($type1 eq 'str' and $type2 eq 'num') | ||
| 149 | ) | ||
| 150 | { | ||
| 151 | # They're not both numbers, not both strings, and not both both | ||
| 152 | # Must be str vs num. | ||
| 153 | return $type1 eq 'num' ? $obj1+0 eq "$obj2" | ||
| 154 | : $obj2+0 eq "$obj1"; | ||
| 155 | } | ||
| 156 | elsif( 'num' ~~ [$type1, $type2] ) { | ||
| 157 | return $obj1 == $obj2; | ||
| 158 | } | ||
| 159 | elsif( 'str' ~~ [$type1, $type2] ) { | ||
| 160 | return $obj1 eq $obj2; | ||
| 161 | } | ||
| 162 | else { | ||
| 163 | die "Should never be reached"; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | # Two objects, same class | ||
| 168 | sub _equal_objects { | ||
| 169 | my($r1, $r2) = @_; | ||
| 170 | |||
| 171 | # No need to check both, they're the same class | ||
| 172 | my $is_overloaded = overload::Overloaded($r1); | ||
| 173 | |||
| 174 | if( !$is_overloaded ) { | ||
| 175 | # Neither are overloaded, they're the same class, are they the same object? | ||
| 176 | return $r1 eq $r2; | ||
| 177 | } | ||
| 178 | else { | ||
| 179 | return _equal_overload( $r1, $r2 ); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | # One overloaded object, one plain scalar | ||
| 184 | # STRING != OBJ | ||
| 185 | # NUMBER != OBJ | ||
| 186 | # STRING eq OBJeq | ||
| 187 | # STRING eq OBJboth | ||
| 188 | # STRING != OBJ== (using == will throw a warning) | ||
| 189 | # NUMBER == OBJ== | ||
| 190 | # NUMBER eq OBJeq | ||
| 191 | # NUMBER == OBJboth | ||
| 192 | sub _equal_overload_vs_scalar { | ||
| 193 | my($obj, $scalar) = @_; | ||
| 194 | |||
| 195 | my $type = _overload_type($obj); | ||
| 196 | return unless $type; | ||
| 197 | |||
| 198 | if( $scalar->is_number ) { | ||
| 199 | if( $type eq 'str' ) { | ||
| 200 | $obj eq $scalar; | ||
| 201 | } | ||
| 202 | else { | ||
| 203 | $obj == $scalar; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | if( $type eq 'num' ) { | ||
| 208 | # Can't reliably compare | ||
| 209 | return; | ||
| 210 | } | ||
| 211 | else { | ||
| 212 | $obj eq $scalar; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | 1; |