9 package My::Package::Stash;
13 use base 'Package::Stash';
19 my $self = $class->SUPER::new(@_);
20 $self->{namespace} = {};
25 my ($self, $variable, $initial_value) = @_;
27 (my $name = $variable) =~ s/^[\$\@\%\&]//;
30 *{$glob} = $initial_value if defined $initial_value;
31 $self->namespace->{$name} = *{$glob};
35 # No actually package Foo exists :)
36 my $foo_stash = My::Package::Stash->new('Foo');
38 isa_ok($foo_stash, 'My::Package::Stash');
39 isa_ok($foo_stash, 'Package::Stash');
41 ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
42 ok(!$foo_stash->has_symbol('%foo'), '... the foo_stash agrees');
45 $foo_stash->add_symbol('%foo' => { one => 1 });
46 }, undef, '... the %foo symbol is created succcessfully');
48 ok(!defined($Foo::{foo}), '... the %foo slot has not been created in the actual Foo package');
49 ok($foo_stash->has_symbol('%foo'), '... the foo_stash agrees');
51 my $foo = $foo_stash->get_symbol('%foo');
52 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
56 is($foo, $foo_stash->get_symbol('%foo'), '... our %foo is the same as the foo_stashs');
58 ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
61 $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]);
62 }, undef, '... created @Foo::bar successfully');
64 ok(!defined($Foo::{bar}), '... the @bar slot has still not been created');
66 ok(!defined($Foo::{baz}), '... the %baz slot has not been created yet');
69 $foo_stash->add_symbol('%baz');
70 }, undef, '... created %Foo::baz successfully');
72 ok(!defined($Foo::{baz}), '... the %baz slot has still not been created');