From 7486ccf34361fccfb1599d244961ddbd3d8bc5a7 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 22 Sep 2010 13:10:28 -0500 Subject: fix coderef vivification --- lib/Package/Stash.pm | 56 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 24 deletions(-) (limited to 'lib') diff --git a/lib/Package/Stash.pm b/lib/Package/Stash.pm index 77236d0..f708bbc 100644 --- a/lib/Package/Stash.pm +++ b/lib/Package/Stash.pm @@ -230,32 +230,40 @@ 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, {}); - } - elsif ($type eq 'SCALAR') { - $self->add_package_symbol($variable); - } - elsif ($type eq 'IO') { - $self->add_package_symbol($variable, Symbol::geniosym); - } - elsif ($type eq 'CODE') { - # ignoring this case for now, since i don't know what would - # be useful to do here (and subs in the stash autovivify in weird - # ways too) - #$self->add_package_symbol($variable, sub {}); + if (!exists $namespace->{$name}) { + if ($opts{vivify}) { + 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, {}); + } + 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 { - confess "Unknown type $type in vivication"; + 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); + } } } -- cgit v1.2.3-54-g00ecf