8 package My::Package::Stash;
12 use base 'Package::Stash';
18 my $self = $class->SUPER::new(@_);
19 $self->{namespace} = {};
23 sub add_package_symbol {
24 my ($self, $variable, $initial_value) = @_;
26 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
29 *{$glob} = $initial_value if defined $initial_value;
30 $self->namespace->{$name} = *{$glob};
34 # No actually package Foo exists :)
35 my $foo_stash = My::Package::Stash->new('Foo');
37 isa_ok($foo_stash, 'My::Package::Stash');
38 isa_ok($foo_stash, 'Package::Stash');
40 ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
41 ok(!$foo_stash->has_package_symbol('%foo'), '... the foo_stash agrees');
44 $foo_stash->add_package_symbol('%foo' => { one => 1 });
45 }, '... the %foo symbol is created succcessfully');
47 ok(!defined($Foo::{foo}), '... the %foo slot has not been created in the actual Foo package');
48 ok($foo_stash->has_package_symbol('%foo'), '... the foo_stash agrees');
50 my $foo = $foo_stash->get_package_symbol('%foo');
51 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
55 is($foo, $foo_stash->get_package_symbol('%foo'), '... our %foo is the same as the foo_stashs');
57 ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
60 $foo_stash->add_package_symbol('@bar' => [ 1, 2, 3 ]);
61 }, '... created @Foo::bar successfully');
63 ok(!defined($Foo::{bar}), '... the @bar slot has still not been created');
65 ok(!defined($Foo::{baz}), '... the %baz slot has not been created yet');
68 $foo_stash->add_package_symbol('%baz');
69 }, '... created %Foo::baz successfully');
71 ok(!defined($Foo::{baz}), '... the %baz slot has still not been created');