2 package Class::MOP::Package;
7 use Scalar::Util 'blessed', 'reftype';
11 our $VERSION = '1.09';
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,
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
52 && (!blessed $package_name || $package_name->isa('Class::MOP::Package')))
53 || confess "You must pass a package name or an existing Class::MOP::Package instance";
55 $package_name = $package_name->name
56 if blessed $package_name;
58 Class::MOP::remove_metaclass_by_name($package_name);
60 $class->initialize($package_name, %options); # call with first arg form for compat
66 return Class::MOP::Class->initialize($class)->new_object(@_)
67 if $class ne __PACKAGE__;
69 my $params = @_ == 1 ? $_[0] : {@_};
72 package => $params->{package},
75 # because of issues with the Perl API
76 # to the typeglob in some versions, we
77 # need to just always grab a new
78 # reference to the hash in the accessor.
79 # Ideally we could just store a ref and
80 # it would Just Work, but oh well :\
90 # all these attribute readers will be bootstrapped
91 # away in the Class::MOP bootstrap section
94 $_[0]->{_package_stash} ||= Package::Stash->new($_[0]->name)
97 $_[0]->_package_stash->namespace
102 # ... these functions have to touch the symbol table itself,.. yuk
104 sub add_package_symbol {
106 $self->_package_stash->add_package_symbol(@_);
109 sub remove_package_glob {
111 $self->_package_stash->remove_package_glob(@_);
114 # ... these functions deal with stuff on the namespace level
116 sub has_package_symbol {
118 $self->_package_stash->has_package_symbol(@_);
121 sub get_package_symbol {
123 $self->_package_stash->get_package_symbol(@_);
126 sub get_or_add_package_symbol {
128 $self->_package_stash->get_or_add_package_symbol(@_);
131 sub remove_package_symbol {
133 $self->_package_stash->remove_package_symbol(@_);
136 sub list_all_package_symbols {
138 $self->_package_stash->list_all_package_symbols(@_);
149 Class::MOP::Package - Package Meta Object
153 The Package Protocol provides an abstraction of a Perl 5 package. A
154 package is basically namespace, and this module provides methods for
155 looking at and changing that namespace's symbol table.
161 =item B<< Class::MOP::Package->initialize($package_name) >>
163 This method creates a new C<Class::MOP::Package> instance which
164 represents specified package. If an existing metaclass object exists
165 for the package, that will be returned instead.
167 =item B<< Class::MOP::Package->reinitialize($package) >>
169 This method forcibly removes any existing metaclass for the package
170 before calling C<initialize>. In contrast to C<initialize>, you may
171 also pass an existing C<Class::MOP::Package> instance instead of just
172 a package name as C<$package>.
174 Do not call this unless you know what you are doing.
176 =item B<< $metapackage->name >>
178 This is returns the package's name, as passed to the constructor.
180 =item B<< $metapackage->namespace >>
182 This returns a hash reference to the package's symbol table. The keys
183 are symbol names and the values are typeglob references.
185 =item B<< $metapackage->add_package_symbol($variable_name, $initial_value) >>
187 This method accepts a variable name and an optional initial value. The
188 C<$variable_name> must contain a leading sigil.
190 This method creates the variable in the package's symbol table, and
191 sets it to the initial value if one was provided.
193 =item B<< $metapackage->get_package_symbol($variable_name) >>
195 Given a variable name, this method returns the variable as a reference
196 or undef if it does not exist. The C<$variable_name> must contain a
199 =item B<< $metapackage->get_or_add_package_symbol($variable_name) >>
201 Given a variable name, this method returns the variable as a reference.
202 If it does not exist, a default value will be generated if possible. The
203 C<$variable_name> must contain a leading sigil.
205 =item B<< $metapackage->has_package_symbol($variable_name) >>
207 Returns true if there is a package variable defined for
208 C<$variable_name>. The C<$variable_name> must contain a leading sigil.
210 =item B<< $metapackage->remove_package_symbol($variable_name) >>
212 This will remove the package variable specified C<$variable_name>. The
213 C<$variable_name> must contain a leading sigil.
215 =item B<< $metapackage->remove_package_glob($glob_name) >>
217 Given the name of a glob, this will remove that glob from the
218 package's symbol table. Glob names do not include a sigil. Removing
219 the glob removes all variables and subroutines with the specified
222 =item B<< $metapackage->list_all_package_symbols($type_filter) >>
224 This will list all the glob names associated with the current
225 package. These names do not have leading sigils.
227 You can provide an optional type filter, which should be one of
228 'SCALAR', 'ARRAY', 'HASH', or 'CODE'.
230 =item B<< $metapackage->get_all_package_symbols($type_filter) >>
232 This works much like C<list_all_package_symbols>, but it returns a
233 hash reference. The keys are glob names and the values are references
234 to the value for that name.
236 =item B<< Class::MOP::Package->meta >>
238 This will return a L<Class::MOP::Class> instance for this class.
244 Stevan Little E<lt>stevan@iinteractive.comE<gt>
246 =head1 COPYRIGHT AND LICENSE
248 Copyright 2006-2010 by Infinity Interactive, Inc.
250 L<http://www.iinteractive.com>
252 This library is free software; you can redistribute it and/or modify
253 it under the same terms as Perl itself.