2 package Class::MOP::Package;
7 use Scalar::Util 'blessed';
10 our $VERSION = '0.65';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Class::MOP::Object';
18 my ( $class, @args ) = @_;
20 unshift @args, "package" if @args % 2;
23 my $package_name = $options{package};
26 # we hand-construct the class
27 # until we can bootstrap it
28 if ( my $meta = Class::MOP::get_metaclass_by_name($package_name) ) {
31 my $meta = ( ref $class || $class )->_new({
32 'package' => $package_name,
35 Class::MOP::store_metaclass_by_name($package_name, $meta);
42 my ( $class, @args ) = @_;
44 unshift @args, "package" if @args % 2;
47 my $package_name = $options{package};
49 (defined $package_name && $package_name && !blessed($package_name))
50 || confess "You must pass a package name and it cannot be blessed";
52 Class::MOP::remove_metaclass_by_name($package_name);
54 $class->initialize('package' => $package_name, @_);
59 my $options = @_ == 1 ? $_[0] : {@_};
62 # because of issues with the Perl API
63 # to the typeglob in some versions, we
64 # need to just always grab a new
65 # reference to the hash in the accessor.
66 # Ideally we could just store a ref and
67 # it would Just Work, but oh well :\
68 $options->{namespace} ||= \undef;
70 bless $options, $class;
76 # all these attribute readers will be bootstrapped
77 # away in the Class::MOP bootstrap section
79 sub name { $_[0]->{'package'} }
82 # because of issues with the Perl API
83 # to the typeglob in some versions, we
84 # need to just always grab a new
85 # reference to the hash here. Ideally
86 # we could just store a ref and it would
87 # Just Work, but oh well :\
89 \%{$_[0]->{'package'} . '::'}
102 sub _deconstruct_variable_name {
103 my ($self, $variable) = @_;
106 || confess "You must pass a variable name";
108 my $sigil = substr($variable, 0, 1, '');
111 || confess "The variable name must include a sigil";
113 (exists $SIGIL_MAP{$sigil})
114 || confess "I do not recognize that sigil '$sigil'";
116 return ($variable, $sigil, $SIGIL_MAP{$sigil});
122 # ... these functions have to touch the symbol table itself,.. yuk
124 sub add_package_symbol {
125 my ($self, $variable, $initial_value) = @_;
127 my ($name, $sigil, $type) = ref $variable eq 'HASH'
128 ? @{$variable}{qw[name sigil type]}
129 : $self->_deconstruct_variable_name($variable);
131 my $pkg = $self->{'package'};
134 no warnings 'redefine', 'misc';
135 *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
138 sub remove_package_glob {
139 my ($self, $name) = @_;
141 delete ${$self->name . '::'}{$name};
144 # ... these functions deal with stuff on the namespace level
146 sub has_package_symbol {
147 my ($self, $variable) = @_;
149 my ($name, $sigil, $type) = ref $variable eq 'HASH'
150 ? @{$variable}{qw[name sigil type]}
151 : $self->_deconstruct_variable_name($variable);
153 my $namespace = $self->namespace;
155 return 0 unless exists $namespace->{$name};
158 # For some really stupid reason
159 # a typeglob will have a default
160 # value of \undef in the SCALAR
161 # slot, so we need to work around
162 # this. Which of course means that
163 # if you put \undef in your scalar
164 # then this is broken.
166 if (ref($namespace->{$name}) eq 'SCALAR') {
167 return ($type eq 'CODE');
169 elsif ($type eq 'SCALAR') {
170 my $val = *{$namespace->{$name}}{$type};
171 return defined(${$val});
174 defined(*{$namespace->{$name}}{$type});
178 sub get_package_symbol {
179 my ($self, $variable) = @_;
181 my ($name, $sigil, $type) = ref $variable eq 'HASH'
182 ? @{$variable}{qw[name sigil type]}
183 : $self->_deconstruct_variable_name($variable);
185 my $namespace = $self->namespace;
187 $self->add_package_symbol($variable)
188 unless exists $namespace->{$name};
190 if (ref($namespace->{$name}) eq 'SCALAR') {
191 if ($type eq 'CODE') {
193 return \&{$self->name.'::'.$name};
200 return *{$namespace->{$name}}{$type};
204 sub remove_package_symbol {
205 my ($self, $variable) = @_;
207 my ($name, $sigil, $type) = ref $variable eq 'HASH'
208 ? @{$variable}{qw[name sigil type]}
209 : $self->_deconstruct_variable_name($variable);
212 # no doubt this is grossly inefficient and
213 # could be done much easier and faster in XS
215 my ($scalar_desc, $array_desc, $hash_desc, $code_desc) = (
216 { sigil => '$', type => 'SCALAR', name => $name },
217 { sigil => '@', type => 'ARRAY', name => $name },
218 { sigil => '%', type => 'HASH', name => $name },
219 { sigil => '&', type => 'CODE', name => $name },
222 my ($scalar, $array, $hash, $code);
223 if ($type eq 'SCALAR') {
224 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
225 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
226 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
228 elsif ($type eq 'ARRAY') {
229 $scalar = $self->get_package_symbol($scalar_desc) if $self->has_package_symbol($scalar_desc);
230 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
231 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
233 elsif ($type eq 'HASH') {
234 $scalar = $self->get_package_symbol($scalar_desc) if $self->has_package_symbol($scalar_desc);
235 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
236 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
238 elsif ($type eq 'CODE') {
239 $scalar = $self->get_package_symbol($scalar_desc) if $self->has_package_symbol($scalar_desc);
240 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
241 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
244 confess "This should never ever ever happen";
247 $self->remove_package_glob($name);
249 $self->add_package_symbol($scalar_desc => $scalar) if defined $scalar;
250 $self->add_package_symbol($array_desc => $array) if defined $array;
251 $self->add_package_symbol($hash_desc => $hash) if defined $hash;
252 $self->add_package_symbol($code_desc => $code) if defined $code;
255 sub list_all_package_symbols {
256 my ($self, $type_filter) = @_;
258 my $namespace = $self->namespace;
259 return keys %{$namespace} unless defined $type_filter;
262 # or we can filter based on
263 # type (SCALAR|ARRAY|HASH|CODE)
264 if ( $type_filter eq 'CODE' ) {
266 (ref($namespace->{$_})
267 ? (ref($namespace->{$_}) eq 'SCALAR')
268 : (ref(\$namespace->{$_}) eq 'GLOB'
269 && defined(*{$namespace->{$_}}{CODE})));
270 } keys %{$namespace};
272 return grep { *{$namespace->{$_}}{$type_filter} } keys %{$namespace};
276 sub get_all_package_symbols {
277 my ($self, $type_filter) = @_;
278 my $namespace = $self->namespace;
280 return %$namespace unless defined $type_filter;
282 # for some reason this nasty impl is orders of magnitude aster than a clean version
283 if ( $type_filter eq 'CODE' ) {
287 (ref($namespace->{$_})
288 ? ( $_ => \&{$pkg ||= $self->name . "::$_"} )
289 : ( *{$namespace->{$_}}{CODE}
290 ? ( $_ => *{$namespace->{$_}}{$type_filter} )
295 $_ => *{$namespace->{$_}}{$type_filter}
297 !ref($namespace->{$_}) && *{$namespace->{$_}}{$type_filter}
310 Class::MOP::Package - Package Meta Object
314 This is an abstraction of a Perl 5 package, it is a superclass of
315 L<Class::MOP::Class> and provides all of the symbol table
316 introspection methods.
324 Returns a metaclass for this package.
326 =item B<initialize ($package_name)>
328 This will initialize a Class::MOP::Package instance which represents
329 the package of C<$package_name>.
331 =item B<reinitialize ($package_name, %options)>
333 This removes the old metaclass, and creates a new one in it's place.
334 Do B<not> use this unless you really know what you are doing, it could
335 very easily make a very large mess of your program.
339 This is a read-only attribute which returns the package name for the
344 This returns a HASH reference to the symbol table. The keys of the
345 HASH are the symbol names, and the values are typeglob references.
347 =item B<add_package_symbol ($variable_name, ?$initial_value)>
349 Given a C<$variable_name>, which must contain a leading sigil, this
350 method will create that variable within the package which houses the
351 class. It also takes an optional C<$initial_value>, which must be a
352 reference of the same type as the sigil of the C<$variable_name>
355 =item B<get_package_symbol ($variable_name)>
357 This will return a reference to the package variable in
360 =item B<has_package_symbol ($variable_name)>
362 Returns true (C<1>) if there is a package variable defined for
363 C<$variable_name>, and false (C<0>) otherwise.
365 =item B<remove_package_symbol ($variable_name)>
367 This will attempt to remove the package variable at C<$variable_name>.
369 =item B<remove_package_glob ($glob_name)>
371 This will attempt to remove the entire typeglob associated with
372 C<$glob_name> from the package.
374 =item B<list_all_package_symbols (?$type_filter)>
376 This will list all the glob names associated with the current package.
377 By inspecting the globs returned you can discern all the variables in
380 By passing a C<$type_filter>, you can limit the list to only those
381 which match the filter (either SCALAR, ARRAY, HASH or CODE).
383 =item B<get_all_package_symbols (?$type_filter)>
385 Works exactly like C<list_all_package_symbols> but returns a HASH of
386 name => thing mapping instead of just an ARRAY of names.
392 Stevan Little E<lt>stevan@iinteractive.comE<gt>
394 =head1 COPYRIGHT AND LICENSE
396 Copyright 2006-2008 by Infinity Interactive, Inc.
398 L<http://www.iinteractive.com>
400 This library is free software; you can redistribute it and/or modify
401 it under the same terms as Perl itself.