10 package My::Package::Stash;
14 use base 'Package::Stash';
20 __PACKAGE__->meta->add_attribute(
22 reader => 'namespace',
29 $class->meta->new_object(__INSTANCE__ => $class->SUPER::new(@_));
33 my ($self, $variable, $initial_value) = @_;
35 (my $name = $variable) =~ s/^[\$\@\%\&]//;
38 *{$glob} = $initial_value if defined $initial_value;
39 $self->namespace->{$name} = *{$glob};
44 package My::Meta::Package;
49 use base 'Class::MOP::Package';
52 $_[0]->{_package_stash} ||= My::Package::Stash->new($_[0]->name);
56 # No actually package Foo exists :)
57 my $meta = My::Meta::Package->initialize('Foo');
59 isa_ok($meta, 'My::Meta::Package');
60 isa_ok($meta, 'Class::MOP::Package');
62 ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
63 ok(!$meta->has_package_symbol('%foo'), '... the meta agrees');
66 $meta->add_package_symbol('%foo' => { one => 1 });
67 }, undef, '... the %foo symbol is created succcessfully' );
69 ok(!defined($Foo::{foo}), '... the %foo slot has not been created in the actual Foo package');
70 ok($meta->has_package_symbol('%foo'), '... the meta agrees');
72 my $foo = $meta->get_package_symbol('%foo');
73 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
77 is($foo, $meta->get_package_symbol('%foo'), '... our %foo is the same as the metas');
79 ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
82 $meta->add_package_symbol('@bar' => [ 1, 2, 3 ]);
83 }, undef, '... created @Foo::bar successfully' );
85 ok(!defined($Foo::{bar}), '... the @bar slot has still not been created');
87 ok(!defined($Foo::{baz}), '... the %baz slot has not been created yet');
90 $meta->add_package_symbol('%baz');
91 }, undef, '... created %Foo::baz successfully' );
93 ok(!defined($Foo::{baz}), '... the %baz slot has still not been created');