2 package Class::MOP::Package;
7 use Scalar::Util 'blessed';
10 our $VERSION = '0.89';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Class::MOP::Object';
19 my ( $class, @args ) = @_;
21 unshift @args, "package" if @args % 2;
24 my $package_name = $options{package};
27 # we hand-construct the class
28 # until we can bootstrap it
29 if ( my $meta = Class::MOP::get_metaclass_by_name($package_name) ) {
32 my $meta = ( ref $class || $class )->_new({
33 'package' => $package_name,
37 Class::MOP::store_metaclass_by_name($package_name, $meta);
44 my ( $class, @args ) = @_;
46 unshift @args, "package" if @args % 2;
49 my $package_name = delete $options{package};
51 (defined $package_name && $package_name && !blessed($package_name))
52 || confess "You must pass a package name and it cannot be blessed";
54 Class::MOP::remove_metaclass_by_name($package_name);
56 $class->initialize($package_name, %options); # call with first arg form for compat
61 return Class::MOP::Class->initialize($class)->new_object(@_)
62 if $class ne __PACKAGE__;
64 my $params = @_ == 1 ? $_[0] : {@_};
67 package => $params->{package},
70 # because of issues with the Perl API
71 # to the typeglob in some versions, we
72 # need to just always grab a new
73 # reference to the hash in the accessor.
74 # Ideally we could just store a ref and
75 # it would Just Work, but oh well :\
85 # all these attribute readers will be bootstrapped
86 # away in the Class::MOP bootstrap section
90 # because of issues with the Perl API
91 # to the typeglob in some versions, we
92 # need to just always grab a new
93 # reference to the hash here. Ideally
94 # we could just store a ref and it would
95 # Just Work, but oh well :\
97 \%{$_[0]->{'package'} . '::'}
110 sub _deconstruct_variable_name {
111 my ($self, $variable) = @_;
114 || confess "You must pass a variable name";
116 my $sigil = substr($variable, 0, 1, '');
119 || confess "The variable name must include a sigil";
121 (exists $SIGIL_MAP{$sigil})
122 || confess "I do not recognize that sigil '$sigil'";
124 return ($variable, $sigil, $SIGIL_MAP{$sigil});
130 # ... these functions have to touch the symbol table itself,.. yuk
132 sub add_package_symbol {
133 my ($self, $variable, $initial_value) = @_;
135 my ($name, $sigil, $type) = ref $variable eq 'HASH'
136 ? @{$variable}{qw[name sigil type]}
137 : $self->_deconstruct_variable_name($variable);
139 my $pkg = $self->{'package'};
142 no warnings 'redefine', 'misc', 'prototype';
143 *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
146 sub remove_package_glob {
147 my ($self, $name) = @_;
149 delete ${$self->name . '::'}{$name};
152 # ... these functions deal with stuff on the namespace level
154 sub has_package_symbol {
155 my ($self, $variable) = @_;
157 my ($name, $sigil, $type) = ref $variable eq 'HASH'
158 ? @{$variable}{qw[name sigil type]}
159 : $self->_deconstruct_variable_name($variable);
161 my $namespace = $self->namespace;
163 return 0 unless exists $namespace->{$name};
165 my $entry_ref = \$namespace->{$name};
166 if (ref($entry_ref) eq 'GLOB') {
167 if ($type eq 'SCALAR') {
168 return defined(${ *{$entry_ref}{SCALAR} });
171 return defined(*{$entry_ref}{$type});
175 # a symbol table entry can be -1 (stub), string (stub with prototype),
176 # or reference (constant)
177 return $type eq 'CODE';
181 sub get_package_symbol {
182 my ($self, $variable) = @_;
184 my ($name, $sigil, $type) = ref $variable eq 'HASH'
185 ? @{$variable}{qw[name sigil type]}
186 : $self->_deconstruct_variable_name($variable);
188 my $namespace = $self->namespace;
191 $self->add_package_symbol($variable)
192 unless exists $namespace->{$name};
194 my $entry_ref = \$namespace->{$name};
196 if (ref($entry_ref) eq 'GLOB') {
197 return *{$entry_ref}{$type};
202 return \&{$self->name . '::' . $name};
210 sub remove_package_symbol {
211 my ($self, $variable) = @_;
213 my ($name, $sigil, $type) = ref $variable eq 'HASH'
214 ? @{$variable}{qw[name sigil type]}
215 : $self->_deconstruct_variable_name($variable);
218 # no doubt this is grossly inefficient and
219 # could be done much easier and faster in XS
221 my ($scalar_desc, $array_desc, $hash_desc, $code_desc) = (
222 { sigil => '$', type => 'SCALAR', name => $name },
223 { sigil => '@', type => 'ARRAY', name => $name },
224 { sigil => '%', type => 'HASH', name => $name },
225 { sigil => '&', type => 'CODE', name => $name },
228 my ($scalar, $array, $hash, $code);
229 if ($type eq 'SCALAR') {
230 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
231 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
232 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
234 elsif ($type eq 'ARRAY') {
235 $scalar = $self->get_package_symbol($scalar_desc) if $self->has_package_symbol($scalar_desc);
236 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
237 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
239 elsif ($type eq 'HASH') {
240 $scalar = $self->get_package_symbol($scalar_desc) if $self->has_package_symbol($scalar_desc);
241 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
242 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
244 elsif ($type eq 'CODE') {
245 $scalar = $self->get_package_symbol($scalar_desc) if $self->has_package_symbol($scalar_desc);
246 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
247 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
250 confess "This should never ever ever happen";
253 $self->remove_package_glob($name);
255 $self->add_package_symbol($scalar_desc => $scalar) if defined $scalar;
256 $self->add_package_symbol($array_desc => $array) if defined $array;
257 $self->add_package_symbol($hash_desc => $hash) if defined $hash;
258 $self->add_package_symbol($code_desc => $code) if defined $code;
261 sub list_all_package_symbols {
262 my ($self, $type_filter) = @_;
264 my $namespace = $self->namespace;
265 return keys %{$namespace} unless defined $type_filter;
268 # or we can filter based on
269 # type (SCALAR|ARRAY|HASH|CODE)
270 if ( $type_filter eq 'CODE' ) {
272 (ref($namespace->{$_})
273 ? (ref($namespace->{$_}) eq 'SCALAR')
274 : (ref(\$namespace->{$_}) eq 'GLOB'
275 && defined(*{$namespace->{$_}}{CODE})));
276 } keys %{$namespace};
278 return grep { *{$namespace->{$_}}{$type_filter} } keys %{$namespace};
290 Class::MOP::Package - Package Meta Object
294 The Package Protocol provides an abstraction of a Perl 5 package. A
295 package is basically namespace, and this module provides methods for
296 looking at and changing that namespace's symbol table.
302 =item B<< Class::MOP::Package->initialize($package_name) >>
304 This method creates a new C<Class::MOP::Package> instance which
305 represents specified package. If an existing metaclass object exists
306 for the package, that will be returned instead.
308 =item B<< Class::MOP::Package->reinitialize($package_name) >>
310 This method forcibly removes any existing metaclass for the package
311 before calling C<initialize>
313 Do not call this unless you know what you are doing.
315 =item B<< $metapackage->name >>
317 This is returns the package's name, as passed to the constructor.
319 =item B<< $metapackage->namespace >>
321 This returns a hash reference to the package's symbol table. The keys
322 are symbol names and the values are typeglob references.
324 =item B<< $metapackage->add_package_symbol($variable_name, $initial_value) >>
326 This method accepts a variable name and an optional initial value. The
327 C<$variable_name> must contain a leading sigil.
329 This method creates the variable in the package's symbol table, and
330 sets it to the initial value if one was provided.
332 =item B<< $metapackage->get_package_symbol($variable_name) >>
334 Given a variable name, this method returns the variable as a reference
335 or undef if it does not exist. The C<$variable_name> must contain a
338 =item B<< $metapackage->has_package_symbol($variable_name) >>
340 Returns true if there is a package variable defined for
341 C<$variable_name>. The C<$variable_name> must contain a leading sigil.
343 =item B<< $metapackage->remove_package_symbol($variable_name) >>
345 This will remove the package variable specified C<$variable_name>. The
346 C<$variable_name> must contain a leading sigil.
348 =item B<< $metapackage->remove_package_glob($glob_name) >>
350 Given the name of a glob, this will remove that glob from the
351 package's symbol table. Glob names do not include a sigil. Removing
352 the glob removes all variables and subroutines with the specified
355 =item B<< $metapackage->list_all_package_symbols($type_filter) >>
357 This will list all the glob names associated with the current
358 package. These names do not have leading sigils.
360 You can provide an optional type filter, which should be one of
361 'SCALAR', 'ARRAY', 'HASH', or 'CODE'.
363 =item B<< $metapackage->get_all_package_symbols($type_filter) >>
365 This works much like C<list_all_package_symbols>, but it returns a
366 hash reference. The keys are glob names and the values are references
367 to the value for that name.
369 =item B<< Class::MOP::Package->meta >>
371 This will return a L<Class::MOP::Class> instance for this class.
377 Stevan Little E<lt>stevan@iinteractive.comE<gt>
379 =head1 COPYRIGHT AND LICENSE
381 Copyright 2006-2009 by Infinity Interactive, Inc.
383 L<http://www.iinteractive.com>
385 This library is free software; you can redistribute it and/or modify
386 it under the same terms as Perl itself.