2 package Class::MOP::Package;
7 use Scalar::Util 'blessed';
10 our $VERSION = '0.06';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Class::MOP::Object';
18 require Class::MOP::Class;
19 Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
26 my $package_name = shift;
27 # we hand-construct the class
28 # until we can bootstrap it
31 '$!package' => $package_name,
33 # because of issues with the Perl API
34 # to the typeglob in some versions, we
35 # need to just always grab a new
36 # reference to the hash in the accessor.
37 # Ideally we could just store a ref and
38 # it would Just Work, but oh well :\
39 '%!namespace' => \undef,
46 # all these attribute readers will be bootstrapped
47 # away in the Class::MOP bootstrap section
49 sub name { $_[0]->{'$!package'} }
52 # because of issues with the Perl API
53 # to the typeglob in some versions, we
54 # need to just always grab a new
55 # reference to the hash here. Ideally
56 # we could just store a ref and it would
57 # Just Work, but oh well :\
59 \%{$_[0]->name . '::'}
72 sub _deconstruct_variable_name {
73 my ($self, $variable) = @_;
76 || confess "You must pass a variable name";
78 my ($sigil, $name) = ($variable =~ /^(.)(.*)$/);
81 || confess "The variable name must include a sigil";
83 (exists $SIGIL_MAP{$sigil})
84 || confess "I do not recognize that sigil '$sigil'";
86 return ($name, $sigil, $SIGIL_MAP{$sigil});
92 # ... these functions have to touch the symbol table itself,.. yuk
94 sub add_package_symbol {
95 my ($self, $variable, $initial_value) = @_;
97 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
100 no warnings 'redefine', 'misc';
101 *{$self->name . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
104 sub remove_package_glob {
105 my ($self, $name) = @_;
107 delete ${$self->name . '::'}{$name};
110 # ... these functions deal with stuff on the namespace level
112 sub has_package_symbol {
113 my ($self, $variable) = @_;
115 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
117 return 0 unless exists $self->namespace->{$name};
120 # For some really stupid reason
121 # a typeglob will have a default
122 # value of \undef in the SCALAR
123 # slot, so we need to work around
124 # this. Which of course means that
125 # if you put \undef in your scalar
126 # then this is broken.
128 if (ref($self->namespace->{$name}) eq 'SCALAR') {
129 return ($type eq 'CODE' ? 1 : 0);
131 elsif ($type eq 'SCALAR') {
132 my $val = *{$self->namespace->{$name}}{$type};
133 return defined(${$val}) ? 1 : 0;
136 defined(*{$self->namespace->{$name}}{$type}) ? 1 : 0;
140 sub get_package_symbol {
141 my ($self, $variable) = @_;
143 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
145 $self->add_package_symbol($variable)
146 unless exists $self->namespace->{$name};
148 if (ref($self->namespace->{$name}) eq 'SCALAR') {
149 if ($type eq 'CODE') {
151 return \&{$self->name.'::'.$name};
158 return *{$self->namespace->{$name}}{$type};
162 sub remove_package_symbol {
163 my ($self, $variable) = @_;
165 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
168 # no doubt this is grossly inefficient and
169 # could be done much easier and faster in XS
171 my ($scalar, $array, $hash, $code);
172 if ($type eq 'SCALAR') {
173 $array = $self->get_package_symbol('@' . $name) if $self->has_package_symbol('@' . $name);
174 $hash = $self->get_package_symbol('%' . $name) if $self->has_package_symbol('%' . $name);
175 $code = $self->get_package_symbol('&' . $name) if $self->has_package_symbol('&' . $name);
177 elsif ($type eq 'ARRAY') {
178 $scalar = $self->get_package_symbol('$' . $name) if $self->has_package_symbol('$' . $name);
179 $hash = $self->get_package_symbol('%' . $name) if $self->has_package_symbol('%' . $name);
180 $code = $self->get_package_symbol('&' . $name) if $self->has_package_symbol('&' . $name);
182 elsif ($type eq 'HASH') {
183 $scalar = $self->get_package_symbol('$' . $name) if $self->has_package_symbol('$' . $name);
184 $array = $self->get_package_symbol('@' . $name) if $self->has_package_symbol('@' . $name);
185 $code = $self->get_package_symbol('&' . $name) if $self->has_package_symbol('&' . $name);
187 elsif ($type eq 'CODE') {
188 $scalar = $self->get_package_symbol('$' . $name) if $self->has_package_symbol('$' . $name);
189 $array = $self->get_package_symbol('@' . $name) if $self->has_package_symbol('@' . $name);
190 $hash = $self->get_package_symbol('%' . $name) if $self->has_package_symbol('%' . $name);
193 confess "This should never ever ever happen";
196 $self->remove_package_glob($name);
198 $self->add_package_symbol(('$' . $name) => $scalar) if defined $scalar;
199 $self->add_package_symbol(('@' . $name) => $array) if defined $array;
200 $self->add_package_symbol(('%' . $name) => $hash) if defined $hash;
201 $self->add_package_symbol(('&' . $name) => $code) if defined $code;
204 sub list_all_package_symbols {
205 my ($self, $type_filter) = @_;
206 return keys %{$self->namespace} unless defined $type_filter;
208 # or we can filter based on
209 # type (SCALAR|ARRAY|HASH|CODE)
210 my $namespace = $self->namespace;
212 (ref($namespace->{$_})
213 ? (ref($namespace->{$_}) eq 'SCALAR' && $type_filter eq 'CODE')
214 : (ref(\$namespace->{$_}) eq 'GLOB'
215 && defined(*{$namespace->{$_}}{$type_filter})));
216 } keys %{$namespace};
227 Class::MOP::Package - Package Meta Object
239 =item B<initialize ($package_name)>
243 This is a read-only attribute which returns the package name for the
248 This returns a HASH reference to the symbol table. The keys of the
249 HASH are the symbol names, and the values are typeglob references.
251 =item B<add_package_symbol ($variable_name, ?$initial_value)>
253 Given a C<$variable_name>, which must contain a leading sigil, this
254 method will create that variable within the package which houses the
255 class. It also takes an optional C<$initial_value>, which must be a
256 reference of the same type as the sigil of the C<$variable_name>
259 =item B<get_package_symbol ($variable_name)>
261 This will return a reference to the package variable in
264 =item B<has_package_symbol ($variable_name)>
266 Returns true (C<1>) if there is a package variable defined for
267 C<$variable_name>, and false (C<0>) otherwise.
269 =item B<remove_package_symbol ($variable_name)>
271 This will attempt to remove the package variable at C<$variable_name>.
273 =item B<remove_package_glob ($glob_name)>
275 This will attempt to remove the entire typeglob associated with
276 C<$glob_name> from the package.
278 =item B<list_all_package_symbols (?$type_filter)>
280 This will list all the glob names associated with the current package.
281 By inspecting the globs returned you can discern all the variables in
284 By passing a C<$type_filter>, you can limit the list to only those
285 which match the filter (either SCALAR, ARRAY, HASH or CODE).
291 Stevan Little E<lt>stevan@iinteractive.comE<gt>
293 =head1 COPYRIGHT AND LICENSE
295 Copyright 2006, 2007 by Infinity Interactive, Inc.
297 L<http://www.iinteractive.com>
299 This library is free software; you can redistribute it and/or modify
300 it under the same terms as Perl itself.