1 package Package::Stash;
4 # ABSTRACT: routines for manipulating stashes
9 #warn "loading Package::Stash";
10 $IMPLEMENTATION = $ENV{PACKAGE_STASH_IMPLEMENTATION}
11 if exists $ENV{PACKAGE_STASH_IMPLEMENTATION};
12 #warn "found $IMPLEMENTATION" if $IMPLEMENTATION;
14 if (!$IMPLEMENTATION) {
16 for my $impl ('XS', 'PP') {
17 if (eval "require Package::Stash::$impl; 1;") {
19 $IMPLEMENTATION = $impl;
25 if (!$IMPLEMENTATION) {
27 Carp::croak("Could not find a suitable Package::Stash implementation");
30 my $impl = "Package::Stash::$IMPLEMENTATION";
31 my $from = $impl->new($impl);
32 my $to = $impl->new(__PACKAGE__);
33 my $methods = $from->get_all_symbols('CODE');
34 for my $meth (keys %$methods) {
35 #warn "installing $meth";
36 $to->add_symbol("&$meth" => $methods->{$meth});
40 use Package::DeprecationManager -deprecations => {
41 'Package::Stash::add_package_symbol' => 0.14,
42 'Package::Stash::remove_package_glob' => 0.14,
43 'Package::Stash::has_package_symbol' => 0.14,
44 'Package::Stash::get_package_symbol' => 0.14,
45 'Package::Stash::get_or_add_package_symbol' => 0.14,
46 'Package::Stash::remove_package_symbol' => 0.14,
47 'Package::Stash::list_all_package_symbols' => 0.14,
50 sub add_package_symbol {
51 deprecated('add_package_symbol is deprecated, please use add_symbol');
52 shift->add_symbol(@_);
55 sub remove_package_glob {
56 deprecated('remove_package_glob is deprecated, please use remove_glob');
57 shift->remove_glob(@_);
60 sub has_package_symbol {
61 deprecated('has_package_symbol is deprecated, please use has_symbol');
62 shift->has_symbol(@_);
65 sub get_package_symbol {
66 deprecated('get_package_symbol is deprecated, please use get_symbol');
67 shift->get_symbol(@_);
70 sub get_or_add_package_symbol {
71 deprecated('get_or_add_package_symbol is deprecated, please use get_or_add_symbol');
72 shift->get_or_add_symbol(@_);
75 sub remove_package_symbol {
76 deprecated('remove_package_symbol is deprecated, please use remove_symbol');
77 shift->remove_symbol(@_);
80 sub list_all_package_symbols {
81 deprecated('list_all_package_symbols is deprecated, please use list_all_symbols');
82 shift->list_all_symbols(@_);
87 my $stash = Package::Stash->new('Foo');
88 $stash->add_symbol('%foo', {bar => 1});
90 $stash->has_symbol('$foo') # false
91 my $namespace = $stash->namespace;
92 *{ $namespace->{foo} }{HASH} # {bar => 1}
96 Manipulating stashes (Perl's symbol tables) is occasionally necessary, but
97 incredibly messy, and easy to get wrong. This module hides all of that behind a
100 NOTE: Most methods in this class require a variable specification that includes
101 a sigil. If this sigil is absent, it is assumed to represent the IO slot.
103 Due to limitations in the typeglob API available to perl code, and to typeglob
104 manipulation in perl being quite slow, this module provides two
105 implementations - one in pure perl, and one using XS. The XS implementation is
106 to be preferred for most usages; the pure perl one is provided for cases where
107 XS modules are not a possibility. The current implementation in use can be set
108 by setting C<$ENV{PACKAGE_STASH_IMPLEMENTATION}> or
109 C<$Package::Stash::IMPLEMENTATION> before loading Package::Stash (with the
110 environment variable taking precedence), otherwise, it will use the XS
111 implementation if possible, falling back to the pure perl one.
113 =method new $package_name
115 Creates a new C<Package::Stash> object, for the package given as the only
120 Returns the name of the package that this object represents.
124 Returns the raw stash itself.
126 =method add_symbol $variable $value %opts
128 Adds a new package symbol, for the symbol given as C<$variable>, and optionally
129 gives it an initial value of C<$value>. C<$variable> should be the name of
130 variable including the sigil, so
132 Package::Stash->new('Foo')->add_symbol('%foo')
134 will create C<%Foo::foo>.
136 Valid options (all optional) are C<filename>, C<first_line_num>, and
139 C<$opts{filename}>, C<$opts{first_line_num}>, and C<$opts{last_line_num}> can
140 be used to indicate where the symbol should be regarded as having been defined.
141 Currently these values are only used if the symbol is a subroutine ('C<&>'
142 sigil) and only if C<$^P & 0x10> is true, in which case the special C<%DB::sub>
143 hash is updated to record the values of C<filename>, C<first_line_num>, and
144 C<last_line_num> for the subroutine. If these are not passed, their values are
145 inferred (as much as possible) from C<caller> information.
147 This is especially useful for debuggers and profilers, which use C<%DB::sub> to
148 determine where the source code for a subroutine can be found. See
149 L<http://perldoc.perl.org/perldebguts.html#Debugger-Internals> for more
150 information about C<%DB::sub>.
152 =method remove_glob $name
154 Removes all package variables with the given name, regardless of sigil.
156 =method has_symbol $variable
158 Returns whether or not the given package variable (including sigil) exists.
160 =method get_symbol $variable
162 Returns the value of the given package variable (including sigil).
164 =method get_or_add_symbol $variable
166 Like C<get_symbol>, except that it will return an empty hashref or
167 arrayref if the variable doesn't exist.
169 =method remove_symbol $variable
171 Removes the package variable described by C<$variable> (which includes the
172 sigil); other variables with the same name but different sigils will be
175 =method list_all_symbols $type_filter
177 Returns a list of package variable names in the package, without sigils. If a
178 C<type_filter> is passed, it is used to select package variables of a given
179 type, where valid types are the slots of a typeglob ('SCALAR', 'CODE', 'HASH',
180 etc). Note that if the package contained any C<BEGIN> blocks, perl will leave
181 an empty typeglob in the C<BEGIN> slot, so this will show up if no filter is
182 used (and similarly for C<INIT>, C<END>, etc).
184 =method get_all_symbols $type_filter
186 Returns a hashref, keyed by the variable names in the package. If
187 C<$type_filter> is passed, the hash will contain every variable of that type in
188 the package as values, otherwise, it will contain the typeglobs corresponding
189 to the variable names (basically, a clone of the stash).
191 =head1 BUGS / CAVEATS
195 =item * GLOB and FORMAT variables are not (yet) accessible through this module.
197 =item * Also, see the BUGS section for the specific backends (L<Package::Stash::XS> and L<Package::Stash::PP>)
201 Please report any bugs through RT: email
202 C<bug-package-stash at rt.cpan.org>, or browse to
203 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
209 =item * L<Class::MOP::Package>
211 This module is a factoring out of code that used to live here
217 You can find this documentation for this module with the perldoc command.
219 perldoc Package::Stash
221 You can also look for information at:
225 =item * AnnoCPAN: Annotated CPAN documentation
227 L<http://annocpan.org/dist/Package-Stash>
231 L<http://cpanratings.perl.org/d/Package-Stash>
233 =item * RT: CPAN's request tracker
235 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
239 L<http://search.cpan.org/dist/Package-Stash>
245 Jesse Luehrs <doy at tozt dot net>
247 Based on code from L<Class::MOP::Package>, by Stevan Little and the Moose
256 get_or_add_package_symbol
257 remove_package_symbol
258 list_all_package_symbols