2 package Class::MOP::Package;
7 use Scalar::Util 'blessed', 'reftype';
11 use base 'Class::MOP::Object';
16 my ( $class, @args ) = @_;
18 unshift @args, "package" if @args % 2;
21 my $package_name = $options{package};
24 # we hand-construct the class
25 # until we can bootstrap it
26 if ( my $meta = Class::MOP::get_metaclass_by_name($package_name) ) {
29 my $meta = ( ref $class || $class )->_new({
30 'package' => $package_name,
33 Class::MOP::store_metaclass_by_name($package_name, $meta);
40 my ( $class, @args ) = @_;
42 unshift @args, "package" if @args % 2;
45 my $package_name = delete $options{package};
47 (defined $package_name && $package_name
48 && (!blessed $package_name || $package_name->isa('Class::MOP::Package')))
49 || confess "You must pass a package name or an existing Class::MOP::Package instance";
51 $package_name = $package_name->name
52 if blessed $package_name;
54 Class::MOP::remove_metaclass_by_name($package_name);
56 $class->initialize($package_name, %options); # call with first arg form for compat
62 return Class::MOP::Class->initialize($class)->new_object(@_)
63 if $class ne __PACKAGE__;
65 my $params = @_ == 1 ? $_[0] : {@_};
68 # Need to quote package to avoid a problem with PPI mis-parsing this
69 # as a package statement.
70 'package' => $params->{package},
73 # because of issues with the Perl API
74 # to the typeglob in some versions, we
75 # need to just always grab a new
76 # reference to the hash in the accessor.
77 # Ideally we could just store a ref and
78 # it would Just Work, but oh well :\
88 # all these attribute readers will be bootstrapped
89 # away in the Class::MOP bootstrap section
92 $_[0]->{_package_stash} ||= Package::Stash->new($_[0]->name)
95 $_[0]->_package_stash->namespace
100 # ... these functions have to touch the symbol table itself,.. yuk
102 sub add_package_symbol {
104 $self->_package_stash->add_symbol(@_);
107 sub remove_package_glob {
109 $self->_package_stash->remove_glob(@_);
112 # ... these functions deal with stuff on the namespace level
114 sub has_package_symbol {
116 $self->_package_stash->has_symbol(@_);
119 sub get_package_symbol {
121 $self->_package_stash->get_symbol(@_);
124 sub get_or_add_package_symbol {
126 $self->_package_stash->get_or_add_symbol(@_);
129 sub remove_package_symbol {
131 $self->_package_stash->remove_symbol(@_);
134 sub list_all_package_symbols {
136 $self->_package_stash->list_all_symbols(@_);
139 sub get_all_package_symbols {
141 $self->_package_stash->get_all_symbols(@_);
146 # ABSTRACT: Package Meta Object
154 The Package Protocol provides an abstraction of a Perl 5 package. A
155 package is basically namespace, and this module provides methods for
156 looking at and changing that namespace's symbol table.
162 =item B<< Class::MOP::Package->initialize($package_name) >>
164 This method creates a new C<Class::MOP::Package> instance which
165 represents specified package. If an existing metaclass object exists
166 for the package, that will be returned instead.
168 =item B<< Class::MOP::Package->reinitialize($package) >>
170 This method forcibly removes any existing metaclass for the package
171 before calling C<initialize>. In contrast to C<initialize>, you may
172 also pass an existing C<Class::MOP::Package> instance instead of just
173 a package name as C<$package>.
175 Do not call this unless you know what you are doing.
177 =item B<< $metapackage->name >>
179 This is returns the package's name, as passed to the constructor.
181 =item B<< $metapackage->namespace >>
183 This returns a hash reference to the package's symbol table. The keys
184 are symbol names and the values are typeglob references.
186 =item B<< $metapackage->add_package_symbol($variable_name, $initial_value) >>
188 This method accepts a variable name and an optional initial value. The
189 C<$variable_name> must contain a leading sigil.
191 This method creates the variable in the package's symbol table, and
192 sets it to the initial value if one was provided.
194 =item B<< $metapackage->get_package_symbol($variable_name) >>
196 Given a variable name, this method returns the variable as a reference
197 or undef if it does not exist. The C<$variable_name> must contain a
200 =item B<< $metapackage->get_or_add_package_symbol($variable_name) >>
202 Given a variable name, this method returns the variable as a reference.
203 If it does not exist, a default value will be generated if possible. The
204 C<$variable_name> must contain a leading sigil.
206 =item B<< $metapackage->has_package_symbol($variable_name) >>
208 Returns true if there is a package variable defined for
209 C<$variable_name>. The C<$variable_name> must contain a leading sigil.
211 =item B<< $metapackage->remove_package_symbol($variable_name) >>
213 This will remove the package variable specified C<$variable_name>. The
214 C<$variable_name> must contain a leading sigil.
216 =item B<< $metapackage->remove_package_glob($glob_name) >>
218 Given the name of a glob, this will remove that glob from the
219 package's symbol table. Glob names do not include a sigil. Removing
220 the glob removes all variables and subroutines with the specified
223 =item B<< $metapackage->list_all_package_symbols($type_filter) >>
225 This will list all the glob names associated with the current
226 package. These names do not have leading sigils.
228 You can provide an optional type filter, which should be one of
229 'SCALAR', 'ARRAY', 'HASH', or 'CODE'.
231 =item B<< $metapackage->get_all_package_symbols($type_filter) >>
233 This works much like C<list_all_package_symbols>, but it returns a
234 hash reference. The keys are glob names and the values are references
235 to the value for that name.
237 =item B<< Class::MOP::Package->meta >>
239 This will return a L<Class::MOP::Class> instance for this class.