- added tests and docs for this feature
* Class::MOP::Class
- - improved &get_package_variable
+ - improved &get_package_symbol
- &version and &superclasses now use it
- methods are now blessed into Class::MOP::Method
whenever possible
sub superclasses {
my $self = shift;
- $self->add_package_variable('@SUPERS' => [])
- unless $self->has_package_variable('@SUPERS');
+ $self->add_package_symbol('@SUPERS' => [])
+ unless $self->has_package_symbol('@SUPERS');
if (@_) {
my @supers = @_;
- @{$self->get_package_variable('@SUPERS')} = @supers;
+ @{$self->get_package_symbol('@SUPERS')} = @supers;
}
- @{$self->get_package_variable('@SUPERS')};
+ @{$self->get_package_symbol('@SUPERS')};
}
sub class_precedence_list {
sub get_slot_value {
my ($self, $instance, $slot_name) = @_;
- $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance};
+ $self->{meta}->get_package_symbol('%' . $slot_name)->{refaddr $instance};
}
sub set_slot_value {
my ($self, $instance, $slot_name, $value) = @_;
- $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} = $value;
+ $self->{meta}->get_package_symbol('%' . $slot_name)->{refaddr $instance} = $value;
}
sub initialize_slot {
my ($self, $instance, $slot_name) = @_;
- $self->{meta}->add_package_variable(('%' . $slot_name) => {})
- unless $self->{meta}->has_package_variable('%' . $slot_name);
- $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} = undef;
+ $self->{meta}->add_package_symbol(('%' . $slot_name) => {})
+ unless $self->{meta}->has_package_symbol('%' . $slot_name);
+ $self->{meta}->get_package_symbol('%' . $slot_name)->{refaddr $instance} = undef;
}
sub is_slot_initialized {
my ($self, $instance, $slot_name) = @_;
- return 0 unless $self->{meta}->has_package_variable('%' . $slot_name);
- return exists $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} ? 1 : 0;
+ return 0 unless $self->{meta}->has_package_symbol('%' . $slot_name);
+ return exists $self->{meta}->get_package_symbol('%' . $slot_name)->{refaddr $instance} ? 1 : 0;
}
1;
=over 4
-=item B<add_package_variable ($variable_name, ?$initial_value)>
+=item B<add_package_symbol ($variable_name, ?$initial_value)>
Given a C<$variable_name>, which must contain a leading sigil, this
method will create that variable within the package which houses the
reference of the same type as the sigil of the C<$variable_name>
implies.
-=item B<get_package_variable ($variable_name)>
+=item B<get_package_symbol ($variable_name)>
This will return a reference to the package variable in
C<$variable_name>.
-=item B<has_package_variable ($variable_name)>
+=item B<has_package_symbol ($variable_name)>
Returns true (C<1>) if there is a package variable defined for
C<$variable_name>, and false (C<0>) otherwise.
-=item B<remove_package_variable ($variable_name)>
+=item B<remove_package_symbol ($variable_name)>
This will attempt to remove the package variable at C<$variable_name>.
sub add_attribute { confess 'Cannot call method "add_attribute" on an immutable instance' }
sub remove_attribute { confess 'Cannot call method "remove_attribute" on an immutable instance' }
-sub add_package_variable { confess 'Cannot call method "add_package_variable" on an immutable instance' }
-sub remove_package_variable { confess 'Cannot call method "remove_package_variable" on an immutable instance' }
+sub add_package_symbol { confess 'Cannot call method "add_package_symbol" on an immutable instance' }
+sub remove_package_symbol { confess 'Cannot call method "remove_package_symbol" on an immutable instance' }
# NOTE:
# superclasses is an accessor, so
=item B<add_method>
-=item B<add_package_variable>
+=item B<add_package_symbol>
=item B<alias_method>
=item B<remove_method>
-=item B<remove_package_variable>
+=item B<remove_package_symbol>
=item B<superclasses>
sub version {
my $self = shift;
- ${$self->get_package_variable('$VERSION')};
+ ${$self->get_package_symbol('$VERSION')};
}
1;
'&' => 'CODE',
);
-sub add_package_variable {
+sub add_package_symbol {
my ($self, $variable, $initial_value) = @_;
(defined $variable)
*{$self->name . '::' . $name} = $initial_value;
}
-sub has_package_variable {
+sub has_package_symbol {
my ($self, $variable) = @_;
(defined $variable)
|| confess "You must pass a variable name";
}
-sub get_package_variable {
+sub get_package_symbol {
my ($self, $variable) = @_;
(defined $variable)
|| confess "You must pass a variable name";
}
-sub remove_package_variable {
+sub remove_package_symbol {
my ($self, $variable) = @_;
(defined $variable)
}
}
-
1;
__END__
=item B<name>
-=item B<add_package_variable>
+=item B<add_package_symbol>
-=item B<get_package_variable>
+=item B<get_package_symbol>
-=item B<has_package_variable>
+=item B<has_package_symbol>
-=item B<remove_package_variable>
+=item B<remove_package_symbol>
=back
name
- add_package_variable get_package_variable has_package_variable remove_package_variable
+ add_package_symbol get_package_symbol has_package_symbol remove_package_symbol
);
my @class_mop_module_methods = qw(
is($class_mop_class_meta->name, 'Class::MOP::Class', '... Class::MOP::Class->name');
is($class_mop_class_meta->version, $Class::MOP::Class::VERSION, '... Class::MOP::Class->version');
-ok($class_mop_class_meta->has_package_variable('$VERSION'), '... Class::MOP::Class->has_package_variable($VERSION)');
-is(${$class_mop_class_meta->get_package_variable('$VERSION')},
+ok($class_mop_class_meta->has_package_symbol('$VERSION'), '... Class::MOP::Class->has_package_symbol($VERSION)');
+is(${$class_mop_class_meta->get_package_symbol('$VERSION')},
$Class::MOP::Class::VERSION,
- '... Class::MOP::Class->get_package_variable($VERSION)');
+ '... Class::MOP::Class->get_package_symbol($VERSION)');
is_deeply(
[ $class_mop_class_meta->superclasses ],
}
ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
-ok(!Foo->meta->has_package_variable('%foo'), '... the meta agrees');
+ok(!Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
lives_ok {
- Foo->meta->add_package_variable('%foo' => { one => 1 });
+ Foo->meta->add_package_symbol('%foo' => { one => 1 });
} '... created %Foo::foo successfully';
ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
-ok(Foo->meta->has_package_variable('%foo'), '... the meta agrees');
+ok(Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
{
no strict 'refs';
is(${'Foo::foo'}{one}, 1, '... our %foo was initialized correctly');
}
-my $foo = Foo->meta->get_package_variable('%foo');
+my $foo = Foo->meta->get_package_symbol('%foo');
is_deeply({ one => 1 }, $foo, '... got the right package variable back');
$foo->{two} = 2;
{
no strict 'refs';
- is(\%{'Foo::foo'}, Foo->meta->get_package_variable('%foo'), '... our %foo is the same as the metas');
+ is(\%{'Foo::foo'}, Foo->meta->get_package_symbol('%foo'), '... our %foo is the same as the metas');
ok(exists ${'Foo::foo'}{two}, '... our %foo was updated correctly');
is(${'Foo::foo'}{two}, 2, '... our %foo was updated correctly');
ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
lives_ok {
- Foo->meta->add_package_variable('@bar' => [ 1, 2, 3 ]);
+ Foo->meta->add_package_symbol('@bar' => [ 1, 2, 3 ]);
} '... created @Foo::bar successfully';
ok(defined($Foo::{bar}), '... the @bar slot was created successfully');
ok(!defined($Foo::{baz}), '... the %baz slot has not been created yet');
lives_ok {
- Foo->meta->add_package_variable('%baz');
+ Foo->meta->add_package_symbol('%baz');
} '... created %Foo::baz successfully';
ok(defined($Foo::{baz}), '... the %baz slot was created successfully');
ok(!defined($Foo::{bling}), '... the @bling slot has not been created yet');
lives_ok {
- Foo->meta->add_package_variable('@bling');
+ Foo->meta->add_package_symbol('@bling');
} '... created @Foo::bling successfully';
ok(defined($Foo::{bling}), '... the @bling slot was created successfully');
}
lives_ok {
- Foo->meta->remove_package_variable('%foo');
+ Foo->meta->remove_package_symbol('%foo');
} '... removed %Foo::foo successfully';
-ok(Foo->meta->has_package_variable('%foo'), '... the %foo slot was removed successfully');
+ok(Foo->meta->has_package_symbol('%foo'), '... the %foo slot was removed successfully');
# check some errors
dies_ok {
- Foo->meta->add_package_variable('bar');
+ Foo->meta->add_package_symbol('bar');
} '... no sigil for bar';
dies_ok {
- Foo->meta->remove_package_variable('bar');
+ Foo->meta->remove_package_symbol('bar');
} '... no sigil for bar';
dies_ok {
- Foo->meta->get_package_variable('bar');
+ Foo->meta->get_package_symbol('bar');
} '... no sigil for bar';
dies_ok {
- Foo->meta->has_package_variable('bar');
+ Foo->meta->has_package_symbol('bar');
} '... no sigil for bar';
#dies_ok {
-# Foo->meta->get_package_variable('@.....bar');
+# Foo->meta->get_package_symbol('@.....bar');
#} '... could not fetch variable';
{
dies_ok {
- Class::MOP::Class->add_package_variable();
- } '... add_package_variable dies as expected';
+ Class::MOP::Class->add_package_symbol();
+ } '... add_package_symbol dies as expected';
dies_ok {
- Class::MOP::Class->add_package_variable('');
- } '... add_package_variable dies as expected';
+ Class::MOP::Class->add_package_symbol('');
+ } '... add_package_symbol dies as expected';
dies_ok {
- Class::MOP::Class->add_package_variable('foo');
- } '... add_package_variable dies as expected';
+ Class::MOP::Class->add_package_symbol('foo');
+ } '... add_package_symbol dies as expected';
dies_ok {
- Class::MOP::Class->add_package_variable('&foo');
- } '... add_package_variable dies as expected';
+ Class::MOP::Class->add_package_symbol('&foo');
+ } '... add_package_symbol dies as expected';
# throws_ok {
-# Class::MOP::Class->meta->add_package_variable('@-');
+# Class::MOP::Class->meta->add_package_symbol('@-');
# } qr/^Could not create package variable \(\@\-\) because/,
-# '... add_package_variable dies as expected';
+# '... add_package_symbol dies as expected';
}
{
dies_ok {
- Class::MOP::Class->has_package_variable();
- } '... has_package_variable dies as expected';
+ Class::MOP::Class->has_package_symbol();
+ } '... has_package_symbol dies as expected';
dies_ok {
- Class::MOP::Class->has_package_variable('');
- } '... has_package_variable dies as expected';
+ Class::MOP::Class->has_package_symbol('');
+ } '... has_package_symbol dies as expected';
dies_ok {
- Class::MOP::Class->has_package_variable('foo');
- } '... has_package_variable dies as expected';
+ Class::MOP::Class->has_package_symbol('foo');
+ } '... has_package_symbol dies as expected';
dies_ok {
- Class::MOP::Class->has_package_variable('&foo');
- } '... has_package_variable dies as expected';
+ Class::MOP::Class->has_package_symbol('&foo');
+ } '... has_package_symbol dies as expected';
}
{
dies_ok {
- Class::MOP::Class->get_package_variable();
- } '... get_package_variable dies as expected';
+ Class::MOP::Class->get_package_symbol();
+ } '... get_package_symbol dies as expected';
dies_ok {
- Class::MOP::Class->get_package_variable('');
- } '... get_package_variable dies as expected';
+ Class::MOP::Class->get_package_symbol('');
+ } '... get_package_symbol dies as expected';
dies_ok {
- Class::MOP::Class->get_package_variable('foo');
- } '... get_package_variable dies as expected';
+ Class::MOP::Class->get_package_symbol('foo');
+ } '... get_package_symbol dies as expected';
dies_ok {
- Class::MOP::Class->get_package_variable('&foo');
- } '... get_package_variable dies as expected';
+ Class::MOP::Class->get_package_symbol('&foo');
+ } '... get_package_symbol dies as expected';
}
{
dies_ok {
- Class::MOP::Class->remove_package_variable();
- } '... remove_package_variable dies as expected';
+ Class::MOP::Class->remove_package_symbol();
+ } '... remove_package_symbol dies as expected';
dies_ok {
- Class::MOP::Class->remove_package_variable('');
- } '... remove_package_variable dies as expected';
+ Class::MOP::Class->remove_package_symbol('');
+ } '... remove_package_symbol dies as expected';
dies_ok {
- Class::MOP::Class->remove_package_variable('foo');
- } '... remove_package_variable dies as expected';
+ Class::MOP::Class->remove_package_symbol('foo');
+ } '... remove_package_symbol dies as expected';
dies_ok {
- Class::MOP::Class->remove_package_variable('&foo');
- } '... remove_package_variable dies as expected';
+ Class::MOP::Class->remove_package_symbol('&foo');
+ } '... remove_package_symbol dies as expected';
}
dies_ok { $meta->add_attribute() } '... exception thrown as expected';
dies_ok { $meta->remove_attribute() } '... exception thrown as expected';
- dies_ok { $meta->add_package_variable() } '... exception thrown as expected';
- dies_ok { $meta->remove_package_variable() } '... exception thrown as expected';
+ dies_ok { $meta->add_package_symbol() } '... exception thrown as expected';
+ dies_ok { $meta->remove_package_symbol() } '... exception thrown as expected';
my @supers;
lives_ok {
dies_ok { $meta->add_attribute() } '... exception thrown as expected';
dies_ok { $meta->remove_attribute() } '... exception thrown as expected';
- dies_ok { $meta->add_package_variable() } '... exception thrown as expected';
- dies_ok { $meta->remove_package_variable() } '... exception thrown as expected';
+ dies_ok { $meta->add_package_symbol() } '... exception thrown as expected';
+ dies_ok { $meta->remove_package_symbol() } '... exception thrown as expected';
my @supers;
lives_ok {
dies_ok { $meta->add_attribute() } '... exception thrown as expected';
dies_ok { $meta->remove_attribute() } '... exception thrown as expected';
- dies_ok { $meta->add_package_variable() } '... exception thrown as expected';
- dies_ok { $meta->remove_package_variable() } '... exception thrown as expected';
+ dies_ok { $meta->add_package_symbol() } '... exception thrown as expected';
+ dies_ok { $meta->remove_package_symbol() } '... exception thrown as expected';
my @supers;
lives_ok {
}
ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
-ok(!Foo->meta->has_package_variable('%foo'), '... the meta agrees');
+ok(!Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
lives_ok {
- Foo->meta->add_package_variable('%foo' => { one => 1 });
+ Foo->meta->add_package_symbol('%foo' => { one => 1 });
} '... created %Foo::foo successfully';
ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
-ok(Foo->meta->has_package_variable('%foo'), '... the meta agrees');
+ok(Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
{
no strict 'refs';
is(${'Foo::foo'}{one}, 1, '... our %foo was initialized correctly');
}
-my $foo = Foo->meta->get_package_variable('%foo');
+my $foo = Foo->meta->get_package_symbol('%foo');
is_deeply({ one => 1 }, $foo, '... got the right package variable back');
$foo->{two} = 2;
{
no strict 'refs';
- is(\%{'Foo::foo'}, Foo->meta->get_package_variable('%foo'), '... our %foo is the same as the metas');
+ is(\%{'Foo::foo'}, Foo->meta->get_package_symbol('%foo'), '... our %foo is the same as the metas');
ok(exists ${'Foo::foo'}{two}, '... our %foo was updated correctly');
is(${'Foo::foo'}{two}, 2, '... our %foo was updated correctly');
ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
lives_ok {
- Foo->meta->add_package_variable('@bar' => [ 1, 2, 3 ]);
+ Foo->meta->add_package_symbol('@bar' => [ 1, 2, 3 ]);
} '... created @Foo::bar successfully';
ok(defined($Foo::{bar}), '... the @bar slot was created successfully');
ok(!defined($Foo::{baz}), '... the %baz slot has not been created yet');
lives_ok {
- Foo->meta->add_package_variable('%baz');
+ Foo->meta->add_package_symbol('%baz');
} '... created %Foo::baz successfully';
ok(defined($Foo::{baz}), '... the %baz slot was created successfully');
ok(!defined($Foo::{bling}), '... the @bling slot has not been created yet');
lives_ok {
- Foo->meta->add_package_variable('@bling');
+ Foo->meta->add_package_symbol('@bling');
} '... created @Foo::bling successfully';
ok(defined($Foo::{bling}), '... the @bling slot was created successfully');
}
lives_ok {
- Foo->meta->remove_package_variable('%foo');
+ Foo->meta->remove_package_symbol('%foo');
} '... removed %Foo::foo successfully';
-ok(Foo->meta->has_package_variable('%foo'), '... the %foo slot was removed successfully');
+ok(Foo->meta->has_package_symbol('%foo'), '... the %foo slot was removed successfully');
# check some errors
dies_ok {
- Foo->meta->add_package_variable('bar');
+ Foo->meta->add_package_symbol('bar');
} '... no sigil for bar';
dies_ok {
- Foo->meta->remove_package_variable('bar');
+ Foo->meta->remove_package_symbol('bar');
} '... no sigil for bar';
dies_ok {
- Foo->meta->get_package_variable('bar');
+ Foo->meta->get_package_symbol('bar');
} '... no sigil for bar';
dies_ok {
- Foo->meta->has_package_variable('bar');
+ Foo->meta->has_package_symbol('bar');
} '... no sigil for bar';
#dies_ok {
-# Foo->meta->get_package_variable('@.....bar');
+# Foo->meta->get_package_symbol('@.....bar');
#} '... could not fetch variable';