diff --git a/lib/Package/Stash/PP.pm b/lib/Package/Stash/PP.pm index fa8db40..d4763f9 100644 --- a/lib/Package/Stash/PP.pm +++ b/lib/Package/Stash/PP.pm @@ -250,6 +250,14 @@ sub has_symbol { if (reftype($entry_ref) eq 'GLOB') { if ($type eq 'SCALAR') { if (BROKEN_SCALAR_INITIALIZATION) { + { + my $package = $self->name; + + # Turning off warnings does not silence this, so intercept it instead. + local $SIG{__WARN__} = sub {1}; + return 1 if eval "package $package; my \$y = ${sigil}${name}; 1"; + } + return defined ${ *{$entry_ref}{$type} }; } else { diff --git a/t/pp_imported_scalars.t b/t/pp_imported_scalars.t new file mode 100644 index 0000000..2c69de8 --- /dev/null +++ b/t/pp_imported_scalars.t @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +BEGIN { $Package::Stash::IMPLEMENTATION = 'PP' } + +use Package::Stash; + +{ + package Foo::Test::Scalar; + use vars qw/$xyz/; + + sub xyz { 1 }; + sub abc { 1 }; +} + +my $ps = Package::Stash->new('Foo::Test::Scalar'); +Test::More::ok($ps->has_symbol('$xyz'), "Found imported scalar, even though it is undef."); +Test::More::ok(!$ps->has_symbol('$abc'), "did not find undeclared scalar"); + +done_testing;