2 package Class::MOP::Package;
8 use Scalar::Util 'blessed';
11 our $VERSION = '0.77';
12 $VERSION = eval $VERSION;
13 our $AUTHORITY = 'cpan:STEVAN';
15 use base 'Class::MOP::Object';
20 my ( $class, @args ) = @_;
22 unshift @args, "package" if @args % 2;
25 my $package_name = $options{package};
28 # we hand-construct the class
29 # until we can bootstrap it
30 if ( my $meta = Class::MOP::get_metaclass_by_name($package_name) ) {
33 my $meta = ( ref $class || $class )->_new({
34 'package' => $package_name,
38 Class::MOP::store_metaclass_by_name($package_name, $meta);
45 my ( $class, @args ) = @_;
47 unshift @args, "package" if @args % 2;
50 my $package_name = delete $options{package};
52 (defined $package_name && $package_name && !blessed($package_name))
53 || confess "You must pass a package name and it cannot be blessed";
55 Class::MOP::remove_metaclass_by_name($package_name);
57 $class->initialize($package_name, %options); # call with first arg form for compat
62 my $options = @_ == 1 ? $_[0] : {@_};
65 # because of issues with the Perl API
66 # to the typeglob in some versions, we
67 # need to just always grab a new
68 # reference to the hash in the accessor.
69 # Ideally we could just store a ref and
70 # it would Just Work, but oh well :\
71 $options->{namespace} ||= \undef;
73 bless $options, $class;
79 # all these attribute readers will be bootstrapped
80 # away in the Class::MOP bootstrap section
82 sub name { $_[0]->{'package'} }
85 # because of issues with the Perl API
86 # to the typeglob in some versions, we
87 # need to just always grab a new
88 # reference to the hash here. Ideally
89 # we could just store a ref and it would
90 # Just Work, but oh well :\
92 \%{$_[0]->{'package'} . '::'}
105 sub _deconstruct_variable_name {
106 my ($self, $variable) = @_;
109 || confess "You must pass a variable name";
111 my $sigil = substr($variable, 0, 1, '');
114 || confess "The variable name must include a sigil";
116 (exists $SIGIL_MAP{$sigil})
117 || confess "I do not recognize that sigil '$sigil'";
119 return ($variable, $sigil, $SIGIL_MAP{$sigil});
125 # ... these functions have to touch the symbol table itself,.. yuk
127 sub add_package_symbol {
128 my ($self, $variable, $initial_value) = @_;
130 my ($name, $sigil, $type) = ref $variable eq 'HASH'
131 ? @{$variable}{qw[name sigil type]}
132 : $self->_deconstruct_variable_name($variable);
134 my $pkg = $self->{'package'};
137 no warnings 'redefine', 'misc';
138 *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
141 sub remove_package_glob {
142 my ($self, $name) = @_;
144 delete ${$self->name . '::'}{$name};
147 # ... these functions deal with stuff on the namespace level
149 sub has_package_symbol {
150 my ($self, $variable) = @_;
152 my ($name, $sigil, $type) = ref $variable eq 'HASH'
153 ? @{$variable}{qw[name sigil type]}
154 : $self->_deconstruct_variable_name($variable);
156 my $namespace = $self->namespace;
158 return 0 unless exists $namespace->{$name};
161 # For some really stupid reason
162 # a typeglob will have a default
163 # value of \undef in the SCALAR
164 # slot, so we need to work around
165 # this. Which of course means that
166 # if you put \undef in your scalar
167 # then this is broken.
169 if (ref($namespace->{$name}) eq 'SCALAR') {
170 return ($type eq 'CODE');
172 elsif ($type eq 'SCALAR') {
173 my $val = *{$namespace->{$name}}{$type};
174 return defined(${$val});
177 defined(*{$namespace->{$name}}{$type});
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;
190 $self->add_package_symbol($variable)
191 unless exists $namespace->{$name};
193 if (ref($namespace->{$name}) eq 'SCALAR') {
194 if ($type eq 'CODE') {
196 return \&{$self->name.'::'.$name};
203 return *{$namespace->{$name}}{$type};
207 sub remove_package_symbol {
208 my ($self, $variable) = @_;
210 my ($name, $sigil, $type) = ref $variable eq 'HASH'
211 ? @{$variable}{qw[name sigil type]}
212 : $self->_deconstruct_variable_name($variable);
215 # no doubt this is grossly inefficient and
216 # could be done much easier and faster in XS
218 my ($scalar_desc, $array_desc, $hash_desc, $code_desc) = (
219 { sigil => '$', type => 'SCALAR', name => $name },
220 { sigil => '@', type => 'ARRAY', name => $name },
221 { sigil => '%', type => 'HASH', name => $name },
222 { sigil => '&', type => 'CODE', name => $name },
225 my ($scalar, $array, $hash, $code);
226 if ($type eq 'SCALAR') {
227 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
228 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
229 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
231 elsif ($type eq 'ARRAY') {
232 $scalar = $self->get_package_symbol($scalar_desc) if $self->has_package_symbol($scalar_desc);
233 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
234 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
236 elsif ($type eq 'HASH') {
237 $scalar = $self->get_package_symbol($scalar_desc) if $self->has_package_symbol($scalar_desc);
238 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
239 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
241 elsif ($type eq 'CODE') {
242 $scalar = $self->get_package_symbol($scalar_desc) if $self->has_package_symbol($scalar_desc);
243 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
244 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
247 confess "This should never ever ever happen";
250 $self->remove_package_glob($name);
252 $self->add_package_symbol($scalar_desc => $scalar) if defined $scalar;
253 $self->add_package_symbol($array_desc => $array) if defined $array;
254 $self->add_package_symbol($hash_desc => $hash) if defined $hash;
255 $self->add_package_symbol($code_desc => $code) if defined $code;
258 sub list_all_package_symbols {
259 my ($self, $type_filter) = @_;
261 my $namespace = $self->namespace;
262 return keys %{$namespace} unless defined $type_filter;
265 # or we can filter based on
266 # type (SCALAR|ARRAY|HASH|CODE)
267 if ( $type_filter eq 'CODE' ) {
269 (ref($namespace->{$_})
270 ? (ref($namespace->{$_}) eq 'SCALAR')
271 : (ref(\$namespace->{$_}) eq 'GLOB'
272 && defined(*{$namespace->{$_}}{CODE})));
273 } keys %{$namespace};
275 return grep { *{$namespace->{$_}}{$type_filter} } keys %{$namespace};
279 sub get_all_package_symbols {
280 my ($self, $type_filter) = @_;
282 die "Cannot call get_all_package_symbols as a class method"
285 my $namespace = $self->namespace;
287 return $namespace unless defined $type_filter;
290 # for some reason this nasty impl is orders of magnitude faster than a clean version
291 if ( $type_filter eq 'CODE' ) {
295 (ref($namespace->{$_})
296 ? ( $_ => \&{$pkg ||= $self->name . "::$_"} )
297 : ( ref \$namespace->{$_} eq 'GLOB' # don't use {CODE} unless it's really a glob to prevent stringification of stubs
298 && (*{$namespace->{$_}}{CODE}) # the extra parents prevent breakage on 5.8.2
299 ? ( $_ => *{$namespace->{$_}}{CODE} )
301 my $sym = B::svref_2object(\$namespace->{$_});
302 my $svt = ref $sym if $sym;
303 ($sym && ($svt eq 'B::PV' || $svt eq 'B::IV'))
304 ? ($_ => ($pkg ||= $self->name)->can($_))
309 $_ => *{$namespace->{$_}}{$type_filter}
311 !ref($namespace->{$_}) && *{$namespace->{$_}}{$type_filter}
326 Class::MOP::Package - Package Meta Object
330 This is an abstraction of a Perl 5 package, it is a superclass of
331 L<Class::MOP::Class> and provides all of the symbol table
332 introspection methods.
336 B<Class::MOP::Package> is a subclass of L<Class::MOP::Object>
344 Returns a metaclass for this package.
346 =item B<initialize ($package_name)>
348 This will initialize a Class::MOP::Package instance which represents
349 the package of C<$package_name>.
351 =item B<reinitialize ($package_name, %options)>
353 This removes the old metaclass, and creates a new one in it's place.
354 Do B<not> use this unless you really know what you are doing, it could
355 very easily make a very large mess of your program.
359 This is a read-only attribute which returns the package name for the
364 This returns a HASH reference to the symbol table. The keys of the
365 HASH are the symbol names, and the values are typeglob references.
367 =item B<add_package_symbol ($variable_name, ?$initial_value)>
369 Given a C<$variable_name>, which must contain a leading sigil, this
370 method will create that variable within the package which houses the
371 class. It also takes an optional C<$initial_value>, which must be a
372 reference of the same type as the sigil of the C<$variable_name>
375 =item B<get_package_symbol ($variable_name)>
377 This will return a reference to the package variable in
380 =item B<has_package_symbol ($variable_name)>
382 Returns true (C<1>) if there is a package variable defined for
383 C<$variable_name>, and false (C<0>) otherwise.
385 =item B<remove_package_symbol ($variable_name)>
387 This will attempt to remove the package variable at C<$variable_name>.
389 =item B<remove_package_glob ($glob_name)>
391 This will attempt to remove the entire typeglob associated with
392 C<$glob_name> from the package.
394 =item B<list_all_package_symbols (?$type_filter)>
396 This will list all the glob names associated with the current package.
397 By inspecting the globs returned you can discern all the variables in
400 By passing a C<$type_filter>, you can limit the list to only those
401 which match the filter (either SCALAR, ARRAY, HASH or CODE).
403 =item B<get_all_package_symbols (?$type_filter)>
405 Works exactly like C<list_all_package_symbols> but returns a HASH of
406 name => thing mapping instead of just an ARRAY of names.
412 Stevan Little E<lt>stevan@iinteractive.comE<gt>
414 =head1 COPYRIGHT AND LICENSE
416 Copyright 2006-2008 by Infinity Interactive, Inc.
418 L<http://www.iinteractive.com>
420 This library is free software; you can redistribute it and/or modify
421 it under the same terms as Perl itself.