summaryrefslogtreecommitdiffstats
path: root/lib/Stash/Manip.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Stash/Manip.pm')
-rw-r--r--lib/Stash/Manip.pm18
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/Stash/Manip.pm b/lib/Stash/Manip.pm
index 944eab3..40e9567 100644
--- a/lib/Stash/Manip.pm
+++ b/lib/Stash/Manip.pm
@@ -110,6 +110,19 @@ will create C<%Foo::foo>.
=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) = @_;
@@ -117,6 +130,11 @@ sub add_package_symbol {
? @{$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';