=cut
+sub _valid_for_type {
+ my $self = shift;
+ my ($value, $type) = @_;
+ if ($type eq 'HASH' || $type eq 'ARRAY'
+ || $type eq 'IO' || $type eq 'CODE') {
+ return reftype($value) eq $type;
+ }
+ else {
+ my $ref = reftype($value);
+ return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE';
+ }
+}
+
sub add_package_symbol {
my ($self, $variable, $initial_value) = @_;
? @{$variable}{qw[name sigil type]}
: $self->_deconstruct_variable_name($variable);
+ if (@_ > 2) {
+ $self->_valid_for_type($initial_value, $type)
+ || confess "$initial_value is not of type $type";
+ }
+
my $pkg = $self->name;
no strict 'refs';
ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
}
+# check some errors
+
+dies_ok {
+ $foo_stash->add_package_symbol('@bar', {})
+} "can't initialize a slot with the wrong type of value";
+
+dies_ok {
+ $foo_stash->add_package_symbol('bar', [])
+} "can't initialize a slot with the wrong type of value";
+
+dies_ok {
+ $foo_stash->add_package_symbol('$bar', sub { })
+} "can't initialize a slot with the wrong type of value";
+
+{
+ package Bar;
+ open *foo, '<', $0;
+}
+
+dies_ok {
+ $foo_stash->add_package_symbol('$bar', *Bar::foo{IO})
+} "can't initialize a slot with the wrong type of value";
+
done_testing;