9 package My::Package::Stash;
13 use base 'Package::Stash';
19 my $self = $class->SUPER::new(@_);
20 $self->{namespace} = {};
24 sub namespace { shift->{namespace} }
27 my ($self, $variable, $initial_value) = @_;
29 (my $name = $variable) =~ s/^[\$\@\%\&]//;
32 *{$glob} = $initial_value if defined $initial_value;
33 $self->namespace->{$name} = *{$glob};
37 # No actually package Foo exists :)
38 my $foo_stash = My::Package::Stash->new('Foo');
40 isa_ok($foo_stash, 'My::Package::Stash');
41 isa_ok($foo_stash, 'Package::Stash');
43 ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
44 ok(!$foo_stash->has_symbol('%foo'), '... the foo_stash agrees');
47 $foo_stash->add_symbol('%foo' => { one => 1 });
48 }, undef, '... the %foo symbol is created succcessfully');
50 ok(!defined($Foo::{foo}), '... the %foo slot has not been created in the actual Foo package');
51 ok($foo_stash->has_symbol('%foo'), '... the foo_stash agrees');
53 my $foo = $foo_stash->get_symbol('%foo');
54 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
58 is($foo, $foo_stash->get_symbol('%foo'), '... our %foo is the same as the foo_stashs');
60 ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
63 $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]);
64 }, undef, '... created @Foo::bar successfully');
66 ok(!defined($Foo::{bar}), '... the @bar slot has still not been created');
68 ok(!defined($Foo::{baz}), '... the %baz slot has not been created yet');
71 $foo_stash->add_symbol('%baz');
72 }, undef, '... created %Foo::baz successfully');
74 ok(!defined($Foo::{baz}), '... the %baz slot has still not been created');