use Scalar::Util 'blessed';
use Carp 'confess';
+use Symbol 'gensym';
our $VERSION = '0.02';
my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
+
no strict 'refs';
- no warnings 'misc', 'redefine';
+ no warnings 'redefine', 'misc';
*{$self->name . '::' . $name} = $initial_value;
}
my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
- return 0 unless exists $self->namespace->{$name};
+ return 0 unless exists $self->namespace->{$name};
defined *{$self->namespace->{$name}}{$type} ? 1 : 0;
}
my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
- return *{$self->namespace->{$name}}{$type}
- if exists $self->namespace->{$name};
- $self->add_package_symbol($variable);
+ $self->add_package_symbol($variable)
+ unless exists $self->namespace->{$name};
+ return *{$self->namespace->{$name}}{$type};
}
sub remove_package_symbol {
my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
+ no strict 'refs';
if ($type eq 'SCALAR') {
- undef ${$self->namespace->{$name}};
+ undef ${$self->name . '::' . $name};
}
elsif ($type eq 'ARRAY') {
- undef @{$self->namespace->{$name}};
+ undef @{$self->name . '::' . $name};
}
elsif ($type eq 'HASH') {
- undef %{$self->namespace->{$name}};
+ undef %{$self->name . '::' . $name};
}
elsif ($type eq 'CODE') {
# FIXME:
# this is crap, it is probably much
# easier to write this in XS.
my ($scalar, @array, %hash);
- $scalar = ${$self->namespace->{$name}} if defined *{$self->namespace->{$name}}{SCALAR};
- @array = @{$self->namespace->{$name}} if defined *{$self->namespace->{$name}}{ARRAY};
- %hash = %{$self->namespace->{$name}} if defined *{$self->namespace->{$name}}{HASH};
- {
- no strict 'refs';
- delete ${$self->name . '::'}{$name};
- }
- ${$self->namespace->{$name}} = $scalar if defined $scalar;
- @{$self->namespace->{$name}} = @array if scalar @array;
- %{$self->namespace->{$name}} = %hash if keys %hash;
+ $scalar = ${$self->name . '::' . $name} if defined *{$self->namespace->{$name}}{SCALAR};
+ @array = @{$self->name . '::' . $name} if defined *{$self->namespace->{$name}}{ARRAY};
+ %hash = %{$self->name . '::' . $name} if defined *{$self->namespace->{$name}}{HASH};
+
+ delete ${$self->name . '::'}{$name};
+
+ ${$self->name . '::' . $name} = $scalar if defined $scalar;
+ @{$self->name . '::' . $name} = @array if scalar @array;
+ %{$self->name . '::' . $name} = %hash if keys %hash;
}
else {
confess "This should never ever ever happen";
use strict;
use warnings;
-use Test::More tests => 34;
+use Test::More tests => 43;
use Test::Exception;
BEGIN {
Foo->meta->add_package_symbol('%foo' => { one => 1 });
} '... created %Foo::foo successfully';
+ok(!Foo->meta->has_package_symbol('$foo'), '... SCALAR shouldnt have been created too');
+
ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
ok(Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
{
no strict 'refs';
+ ok(defined(*{"Foo::foo"}{HASH}), '... the %foo (HASH) slot was created successfully');
+
+ ok(!defined(*{"Foo::foo"}{SCALAR}), '... but the $foo slot was not created');
+ ok(!Foo->meta->has_package_symbol('$foo'), '... and the meta agrees');
+
+ ok(!defined(*{"Foo::foo"}{ARRAY}), '... but the @foo slot was not created');
+ ok(!Foo->meta->has_package_symbol('@foo'), '... and the meta agrees');
+
+ ok(!defined(*{"Foo::foo"}{CODE}), '... but the &foo slot was not created');
+ ok(!Foo->meta->has_package_symbol('&foo'), '... and the meta agrees');
+}
+
+{
+ no strict 'refs';
ok(exists ${'Foo::foo'}{one}, '... our %foo was initialized correctly');
is(${'Foo::foo'}{one}, 1, '... our %foo was initialized correctly');
}
ok(Foo->meta->has_package_symbol('%foo'), '... the %foo slot was removed successfully');
+{
+ no strict 'refs';
+ ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
+}
+
# check some errors
dies_ok {
dies_ok {
Foo->meta->has_package_symbol('bar');
} '... no sigil for bar';
-
-
-#dies_ok {
-# Foo->meta->get_package_symbol('@.....bar');
-#} '... could not fetch variable';