1 package Package::Stash;
4 # ABSTRACT: routines for manipulating stashes
9 $IMPLEMENTATION = $ENV{PACKAGE_STASH_IMPLEMENTATION}
10 if exists $ENV{PACKAGE_STASH_IMPLEMENTATION};
13 if ($IMPLEMENTATION) {
14 if (!eval "require Package::Stash::$IMPLEMENTATION; 1") {
16 Carp::croak("Could not load Package::Stash::$IMPLEMENTATION: $@");
20 for my $impl ('XS', 'PP') {
21 if (eval "require Package::Stash::$impl; 1;") {
22 $IMPLEMENTATION = $impl;
31 if (!$IMPLEMENTATION) {
33 Carp::croak("Could not find a suitable Package::Stash implementation: $err");
36 my $impl = "Package::Stash::$IMPLEMENTATION";
37 my $from = $impl->new($impl);
38 my $to = $impl->new(__PACKAGE__);
39 my $methods = $from->get_all_symbols('CODE');
40 for my $meth (keys %$methods) {
41 $to->add_symbol("&$meth" => $methods->{$meth});
45 use Package::DeprecationManager -deprecations => {
46 'Package::Stash::add_package_symbol' => 0.14,
47 'Package::Stash::remove_package_glob' => 0.14,
48 'Package::Stash::has_package_symbol' => 0.14,
49 'Package::Stash::get_package_symbol' => 0.14,
50 'Package::Stash::get_or_add_package_symbol' => 0.14,
51 'Package::Stash::remove_package_symbol' => 0.14,
52 'Package::Stash::list_all_package_symbols' => 0.14,
55 sub add_package_symbol {
56 deprecated('add_package_symbol is deprecated, please use add_symbol');
57 shift->add_symbol(@_);
60 sub remove_package_glob {
61 deprecated('remove_package_glob is deprecated, please use remove_glob');
62 shift->remove_glob(@_);
65 sub has_package_symbol {
66 deprecated('has_package_symbol is deprecated, please use has_symbol');
67 shift->has_symbol(@_);
70 sub get_package_symbol {
71 deprecated('get_package_symbol is deprecated, please use get_symbol');
72 shift->get_symbol(@_);
75 sub get_or_add_package_symbol {
76 deprecated('get_or_add_package_symbol is deprecated, please use get_or_add_symbol');
77 shift->get_or_add_symbol(@_);
80 sub remove_package_symbol {
81 deprecated('remove_package_symbol is deprecated, please use remove_symbol');
82 shift->remove_symbol(@_);
85 sub list_all_package_symbols {
86 deprecated('list_all_package_symbols is deprecated, please use list_all_symbols');
87 shift->list_all_symbols(@_);
92 my $stash = Package::Stash->new('Foo');
93 $stash->add_symbol('%foo', {bar => 1});
95 $stash->has_symbol('$foo') # false
96 my $namespace = $stash->namespace;
97 *{ $namespace->{foo} }{HASH} # {bar => 1}
101 Manipulating stashes (Perl's symbol tables) is occasionally necessary, but
102 incredibly messy, and easy to get wrong. This module hides all of that behind a
105 NOTE: Most methods in this class require a variable specification that includes
106 a sigil. If this sigil is absent, it is assumed to represent the IO slot.
108 Due to limitations in the typeglob API available to perl code, and to typeglob
109 manipulation in perl being quite slow, this module provides two
110 implementations - one in pure perl, and one using XS. The XS implementation is
111 to be preferred for most usages; the pure perl one is provided for cases where
112 XS modules are not a possibility. The current implementation in use can be set
113 by setting C<$ENV{PACKAGE_STASH_IMPLEMENTATION}> or
114 C<$Package::Stash::IMPLEMENTATION> before loading Package::Stash (with the
115 environment variable taking precedence), otherwise, it will use the XS
116 implementation if possible, falling back to the pure perl one.
118 =method new $package_name
120 Creates a new C<Package::Stash> object, for the package given as the only
125 Returns the name of the package that this object represents.
129 Returns the raw stash itself.
131 =method add_symbol $variable $value %opts
133 Adds a new package symbol, for the symbol given as C<$variable>, and optionally
134 gives it an initial value of C<$value>. C<$variable> should be the name of
135 variable including the sigil, so
137 Package::Stash->new('Foo')->add_symbol('%foo')
139 will create C<%Foo::foo>.
141 Valid options (all optional) are C<filename>, C<first_line_num>, and
144 C<$opts{filename}>, C<$opts{first_line_num}>, and C<$opts{last_line_num}> can
145 be used to indicate where the symbol should be regarded as having been defined.
146 Currently these values are only used if the symbol is a subroutine ('C<&>'
147 sigil) and only if C<$^P & 0x10> is true, in which case the special C<%DB::sub>
148 hash is updated to record the values of C<filename>, C<first_line_num>, and
149 C<last_line_num> for the subroutine. If these are not passed, their values are
150 inferred (as much as possible) from C<caller> information.
152 This is especially useful for debuggers and profilers, which use C<%DB::sub> to
153 determine where the source code for a subroutine can be found. See
154 L<http://perldoc.perl.org/perldebguts.html#Debugger-Internals> for more
155 information about C<%DB::sub>.
157 =method remove_glob $name
159 Removes all package variables with the given name, regardless of sigil.
161 =method has_symbol $variable
163 Returns whether or not the given package variable (including sigil) exists.
165 =method get_symbol $variable
167 Returns the value of the given package variable (including sigil).
169 =method get_or_add_symbol $variable
171 Like C<get_symbol>, except that it will return an empty hashref or
172 arrayref if the variable doesn't exist.
174 =method remove_symbol $variable
176 Removes the package variable described by C<$variable> (which includes the
177 sigil); other variables with the same name but different sigils will be
180 =method list_all_symbols $type_filter
182 Returns a list of package variable names in the package, without sigils. If a
183 C<type_filter> is passed, it is used to select package variables of a given
184 type, where valid types are the slots of a typeglob ('SCALAR', 'CODE', 'HASH',
185 etc). Note that if the package contained any C<BEGIN> blocks, perl will leave
186 an empty typeglob in the C<BEGIN> slot, so this will show up if no filter is
187 used (and similarly for C<INIT>, C<END>, etc).
189 =method get_all_symbols $type_filter
191 Returns a hashref, keyed by the variable names in the package. If
192 C<$type_filter> is passed, the hash will contain every variable of that type in
193 the package as values, otherwise, it will contain the typeglobs corresponding
194 to the variable names (basically, a clone of the stash).
196 =head1 BUGS / CAVEATS
200 =item * Prior to perl 5.10, scalar slots are only considered to exist if they are defined
202 This is due to a shortcoming within perl itself. See
203 L<perlref/Making References> point 7 for more information.
205 =item * GLOB and FORMAT variables are not (yet) accessible through this module.
207 =item * Also, see the BUGS section for the specific backends (L<Package::Stash::XS> and L<Package::Stash::PP>)
211 Please report any bugs through RT: email
212 C<bug-package-stash at rt.cpan.org>, or browse to
213 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
219 =item * L<Class::MOP::Package>
221 This module is a factoring out of code that used to live here
227 You can find this documentation for this module with the perldoc command.
229 perldoc Package::Stash
231 You can also look for information at:
235 =item * AnnoCPAN: Annotated CPAN documentation
237 L<http://annocpan.org/dist/Package-Stash>
241 L<http://cpanratings.perl.org/d/Package-Stash>
243 =item * RT: CPAN's request tracker
245 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
249 L<http://search.cpan.org/dist/Package-Stash>
255 Jesse Luehrs <doy at tozt dot net>
257 Based on code from L<Class::MOP::Package>, by Stevan Little and the Moose
266 get_or_add_package_symbol
267 remove_package_symbol
268 list_all_package_symbols