actually, only do the weird special casing on broken perl versions
[gitmo/Package-Stash.git] / lib / Package / Stash.pm
index b990f55..6c361c1 100644 (file)
@@ -5,6 +5,10 @@ use warnings;
 
 use Carp qw(confess);
 use Scalar::Util qw(reftype);
+use Symbol;
+# before 5.12, assigning to the ISA glob would make it lose its magical ->isa
+# powers
+use constant BROKEN_ISA_ASSIGNMENT => ($] < 5.012);
 
 =head1 SYNOPSIS
 
@@ -229,21 +233,43 @@ sub get_package_symbol {
 
     my $namespace = $self->namespace;
 
-    if ($opts{vivify} && !exists $namespace->{$name}) {
-        if ($type eq 'ARRAY') {
-            $self->add_package_symbol(
-                $variable,
-                # setting our own arrayref manually loses the magicalness or
-                # something
-                $name eq 'ISA' ? () : ([])
-            );
-        }
-        elsif ($type eq 'HASH') {
-            $self->add_package_symbol($variable, {});
+    if (!exists $namespace->{$name}) {
+        if ($opts{vivify}) {
+            if ($type eq 'ARRAY') {
+                if (BROKEN_ISA_ASSIGNMENT) {
+                    $self->add_package_symbol(
+                        $variable,
+                        $name eq 'ISA' ? () : ([])
+                    );
+                }
+                else {
+                    $self->add_package_symbol($variable, []);
+                }
+            }
+            elsif ($type eq 'HASH') {
+                $self->add_package_symbol($variable, {});
+            }
+            elsif ($type eq 'SCALAR') {
+                $self->add_package_symbol($variable);
+            }
+            elsif ($type eq 'IO') {
+                $self->add_package_symbol($variable, Symbol::geniosym);
+            }
+            elsif ($type eq 'CODE') {
+                confess "Don't know how to vivify CODE variables";
+            }
+            else {
+                confess "Unknown type $type in vivication";
+            }
         }
         else {
-            # FIXME
-            $self->add_package_symbol($variable)
+            if ($type eq 'CODE') {
+                # this effectively "de-vivifies" the code slot. if we don't do
+                # this, referencing the coderef at the end of this function
+                # will cause perl to auto-vivify a stub coderef in the slot,
+                # which isn't what we want
+                $self->add_package_symbol($variable);
+            }
         }
     }