Commits

Toby Inkster  committed dffc77b

Cope with Perl installlations that don't have Scalar::Util::weaken

  • Participants
  • Parent commits 70b12fb

Comments (0)

Files changed (2)

     use Hash::FieldHash qw();
     use Scalar::Util qw();
     my %tiecache;
-    BEGIN { Hash::FieldHash::fieldhash(%tiecache) };
+    BEGIN {
+        Hash::FieldHash::fieldhash(%tiecache);
+        *__HAS_WEAKEN  = defined(&Scalar::Util::weaken)
+            ? sub () { 1 }
+            : sub () { 0 };
+    };
     sub getAttributeHash {
         my $self = shift;
         if (!exists $tiecache{ $self }) {
             tie my %attr, 'XML::LibXML::AttributeHash', $self, weaken => 1;
             $tiecache{ $self } = \%attr;
-            Scalar::Util::weaken($tiecache{ $self });
+            if ( __HAS_WEAKEN ) {
+                Scalar::Util::weaken($tiecache{ $self });
+            }
         }
         return $tiecache{ $self };
     }

File lib/XML/LibXML/AttributeHash.pm

 use vars qw($VERSION);
 $VERSION = "1.90"; # VERSION TEMPLATE: DO NOT CHANGE
 
+BEGIN {
+    *__HAS_WEAKEN  = defined(&Scalar::Util::weaken)
+        ? sub () { 1 }
+        : sub () { 0 };
+};
+
 sub element {
     return $_[0][0];
 }
 
 sub TIEHASH {
     my ($class, $element, %args) = @_;
-    if ($args{weaken}) {
-        Scalar::Util::weaken($element);
+    my $self = bless [$element, undef, \%args], $class;
+    if (__HAS_WEAKEN and $args{weaken}) {
+        Scalar::Util::weaken( $self->[0] );
     }
-    bless [$element, undef, \%args], $class;
+    return $self;
 }
 
 sub STORE {
 
 Currently only one argument is supported, the boolean "weaken" which (if
 true) indicates that the tied object's reference to the element should be
-a weak reference. This is used by XML::LibXML::Element's overloading.
+a weak reference. This is used by XML::LibXML::Element's overloading. The
+"weaken" argument is ignored if you don't have a working Scalar::Util::weaken.