Commits

Toby Inkster committed 50d3eac

Remove dep on Hash::FieldHash

Comments (0)

Files changed (4)

     - Overload hash dereferencing on XML::LibXML::Elements, to provide
     access to the element's attributes.
         - See XML::LibXML::AttributeHash for details.
-        - Requires Hash::FieldHash 0.09 or above.
         - Thanks to Toby Inkster.
     - Pull some commits from Toby Inkster to add more convenient methods
     to XML::LibXML::NodeList such as sort, map, grep, etc.
     # need to use a fieldhash. Hash::FieldHash requires at least
     # Perl 5.8, but XML-LibXML already dropped support for older
     # Perls since XML-LibXML-1.77.
-    use Hash::FieldHash qw();
+    #
+    # If Hash::FieldHash isn't available we can sort of do the
+    # same thing by relying upon the stringification of non-scalar
+    # hash keys, and performing a bit of cleanup in DESTROY.
+    #
     my %tiecache;
-    BEGIN {
-        Hash::FieldHash::fieldhash(%tiecache);
+    BEGIN
+    {
+        if (eval { require Hash::FieldHash 0.09; 1 })
+        {
+            Hash::FieldHash::fieldhashes(\%tiecache);
+            *__destroy_tiecache = sub {};
+        }
+        else
+        {
+            *__destroy_tiecache = sub { delete $tiecache{ $_[0] } };
+        }
     };
-    sub getAttributeHash {
+    sub getAttributeHash
+    {
         my $self = shift;
         if (!exists $tiecache{ $self }) {
             tie my %attr, 'XML::LibXML::AttributeHash', $self, weaken => 1;
         }
         return $tiecache{ $self };
     }
+    sub DESTROY
+    {
+        my ($self) = @_;
+        $self->__destroy_tiecache;
+        $self->SUPER::DESTROY;
+    }
 }
 
 sub setNamespace {
     'PREREQ_PM' => {
         'base' => 0,
         'ExtUtils::MakeMaker' => '6.56',
-        'Hash::FieldHash' => '0.09',
+        #'Hash::FieldHash' => '0.09',
         'strict' => 0,
         'Test::More' => 0,
         'vars' => 0,

t/72destruction.t

 
 my $is_destroyed;
 BEGIN {
-	no warnings 'once';
+	no warnings 'once', 'redefine';
+	my $old = \&XML::LibXML::Element::DESTROY;
 	*XML::LibXML::Element::DESTROY = sub {
 		# warn sprintf("DESTROY %s", $_[0]->toString);
 		$is_destroyed++;
+		$old->(@_);
 	};
 }