Shlomi Fish avatar Shlomi Fish committed cd0c3b9

Patch to fix the overloading.

Thanks to Mons.

Comments (0)

Files changed (4)

 Revision history for Perl extension XML::LibXML
+    
+    - Apply a patch from Mons Anderson ( mons@cpan.org ) for fixing the 
+    overloading.
+        - t/62overload.t
+        - Thanks to Mons.
 
 1.98            Sun May 13 21:02:14 IDT 2012
     - Make sure parse_string() and load_xml() also accept references to
 #-------------------------------------------------------------------------#
 package XML::LibXML::Node;
 
+use overload
+    '""'   => sub { $_[0]->toString() },
+    'bool' => sub { 1 },
+    '0+'   => sub { Scalar::Util::refaddr($_[0]) },
+    fallback => 1,
+    ;
+
+
 sub CLONE_SKIP {
   return $XML::LibXML::__threads_shared ? 0 : 1;
 }
 
 use overload
     '%{}'  => 'getAttributeHash',
-    'bool' => sub { 1 },
     'eq' => '_isSameNodeLax', '==' => '_isSameNodeLax',
     'ne' => '_isNotSameNodeLax', '!=' => '_isNotSameNodeLax',
+    fallback => 1,
     ;
 
 sub _isNotSameNodeLax {
         }
         else
         {
-            *__destroy_tiecache = sub { delete $tiecache{ $_[0] } };
+            *__destroy_tiecache = sub { delete $tiecache{ 0+$_[0] } };
         }
     };
     sub getAttributeHash
     {
         my $self = shift;
-        if (!exists $tiecache{ $self }) {
+        if (!exists $tiecache{ 0+$self }) {
             tie my %attr, 'XML::LibXML::AttributeHash', $self, weaken => 1;
-            $tiecache{ $self } = \%attr;
+            $tiecache{ 0+$self } = \%attr;
         }
-        return $tiecache{ $self };
+        return $tiecache{ 0+$self };
     }
     sub DESTROY
     {
 t/60error_prev_chain.t
 t/60struct_error.t
 t/61error.t
+t/62overload.t
 t/71overloads.t
 t/72destruction.t
 t/80registryleak.t
+# -*- cperl -*-
+
+use strict;
+use warnings;
+
+use Test::More tests => 12;
+
+use XML::LibXML;
+
+my $e1 = XML::LibXML::Element->new('test1');
+$e1->setAttribute('attr' => 'value1');
+
+my $e2 = XML::LibXML::Element->new('test2');
+$e2->setAttribute('attr' => 'value2');
+
+my $h1 = \%{ $e1 };
+my $h2 = \%{ $e2 };
+
+isnt $h1,$h2, 'different references';
+
+is $h1->{attr}, 'value1', 'affr for el 1';
+is $h2->{attr}, 'value2', 'affr for el 2';
+
+is "$e1", '<test1 attr="value1"/>', 'stringify for el 1';
+is "$e2", '<test2 attr="value2"/>', 'stringify for el 2';
+
+cmp_ok 0+$e1, '>', 1, 'num for el 1';
+cmp_ok 0+$e2, '>', 1, 'num for el 2';
+
+isnt 0+$e1,0+$e2, 'num for e1 and e2 differs';
+
+my $e3 = $e1;
+
+ok $e3 eq $e1, 'eq';
+ok $e3 == $e1, '==';
+
+ok $e1 ne $e2, 'ne';
+ok $e1 != $e2, '!=';
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.