2 package Class::MOP::Package;
7 use Scalar::Util 'blessed';
10 our $VERSION = '0.02';
15 require Class::MOP::Class;
16 Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
23 my $package_name = shift;
24 # we hand-construct the class
25 # until we can bootstrap it
28 '$:package' => $package_name,
29 '%:namespace' => \%{$package_name . '::'},
36 # all these attribute readers will be bootstrapped
37 # away in the Class::MOP bootstrap section
39 sub name { $_[0]->{'$:package'} }
40 sub namespace { $_[0]->{'%:namespace'} }
52 sub _deconstruct_variable_name {
53 my ($self, $variable) = @_;
56 || confess "You must pass a variable name";
58 my ($sigil, $name) = ($variable =~ /^(.)(.*)$/);
61 || confess "The variable name must include a sigil";
63 (exists $SIGIL_MAP{$sigil})
64 || confess "I do not recognize that sigil '$sigil'";
66 return ($name, $sigil, $SIGIL_MAP{$sigil});
72 sub add_package_symbol {
73 my ($self, $variable, $initial_value) = @_;
75 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
78 no warnings 'misc', 'redefine';
79 *{$self->name . '::' . $name} = $initial_value;
82 sub has_package_symbol {
83 my ($self, $variable) = @_;
85 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
87 return 0 unless exists $self->namespace->{$name};
88 defined *{$self->namespace->{$name}}{$type} ? 1 : 0;
91 sub get_package_symbol {
92 my ($self, $variable) = @_;
94 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
96 return *{$self->namespace->{$name}}{$type}
97 if exists $self->namespace->{$name};
98 $self->add_package_symbol($variable);
101 sub remove_package_symbol {
102 my ($self, $variable) = @_;
104 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
106 if ($type eq 'SCALAR') {
107 undef ${$self->namespace->{$name}};
109 elsif ($type eq 'ARRAY') {
110 undef @{$self->namespace->{$name}};
112 elsif ($type eq 'HASH') {
113 undef %{$self->namespace->{$name}};
115 elsif ($type eq 'CODE') {
117 # this is crap, it is probably much
118 # easier to write this in XS.
119 my ($scalar, @array, %hash);
120 $scalar = ${$self->namespace->{$name}} if defined *{$self->namespace->{$name}}{SCALAR};
121 @array = @{$self->namespace->{$name}} if defined *{$self->namespace->{$name}}{ARRAY};
122 %hash = %{$self->namespace->{$name}} if defined *{$self->namespace->{$name}}{HASH};
125 delete ${$self->name . '::'}{$name};
127 ${$self->namespace->{$name}} = $scalar if defined $scalar;
128 @{$self->namespace->{$name}} = @array if scalar @array;
129 %{$self->namespace->{$name}} = %hash if keys %hash;
132 confess "This should never ever ever happen";
136 sub list_all_package_symbols {
138 return keys %{$self->namespace};
149 Class::MOP::Package - Package Meta Object
167 =item B<add_package_symbol>
169 =item B<get_package_symbol>
171 =item B<has_package_symbol>
173 =item B<remove_package_symbol>
175 =item B<list_all_package_symbols>
181 Stevan Little E<lt>stevan@iinteractive.comE<gt>
183 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
185 =head1 COPYRIGHT AND LICENSE
187 Copyright 2006 by Infinity Interactive, Inc.
189 L<http://www.iinteractive.com>
191 This library is free software; you can redistribute it and/or modify
192 it under the same terms as Perl itself.