redeprecate these
[gitmo/Package-Stash.git] / lib / Package / Stash.pm
CommitLineData
e4afde02 1package Package::Stash;
2use strict;
3use warnings;
4# ABSTRACT: routines for manipulating stashes
5
6our $IMPLEMENTATION;
7
8BEGIN {
e4afde02 9 $IMPLEMENTATION = $ENV{PACKAGE_STASH_IMPLEMENTATION}
10 if exists $ENV{PACKAGE_STASH_IMPLEMENTATION};
e4afde02 11
a5129e53 12 my $err;
13 if ($IMPLEMENTATION) {
14 if (!eval "require Package::Stash::$IMPLEMENTATION; 1") {
15 require Carp;
16 Carp::croak("Could not load Package::Stash::$IMPLEMENTATION: $@");
17 }
18 }
19 else {
e4afde02 20 for my $impl ('XS', 'PP') {
21 if (eval "require Package::Stash::$impl; 1;") {
e4afde02 22 $IMPLEMENTATION = $impl;
23 last;
24 }
a5129e53 25 else {
26 $err .= $@;
27 }
e4afde02 28 }
29 }
30
31 if (!$IMPLEMENTATION) {
32 require Carp;
a5129e53 33 Carp::croak("Could not find a suitable Package::Stash implementation: $err");
e4afde02 34 }
35
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) {
e4afde02 41 $to->add_symbol("&$meth" => $methods->{$meth});
42 }
43}
44
45use 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,
53};
54
55sub add_package_symbol {
86cf2011 56 deprecated('add_package_symbol is deprecated, please use add_symbol');
e4afde02 57 shift->add_symbol(@_);
58}
59
60sub remove_package_glob {
86cf2011 61 deprecated('remove_package_glob is deprecated, please use remove_glob');
e4afde02 62 shift->remove_glob(@_);
63}
64
65sub has_package_symbol {
86cf2011 66 deprecated('has_package_symbol is deprecated, please use has_symbol');
e4afde02 67 shift->has_symbol(@_);
68}
69
70sub get_package_symbol {
86cf2011 71 deprecated('get_package_symbol is deprecated, please use get_symbol');
e4afde02 72 shift->get_symbol(@_);
73}
74
75sub get_or_add_package_symbol {
86cf2011 76 deprecated('get_or_add_package_symbol is deprecated, please use get_or_add_symbol');
e4afde02 77 shift->get_or_add_symbol(@_);
78}
79
80sub remove_package_symbol {
86cf2011 81 deprecated('remove_package_symbol is deprecated, please use remove_symbol');
e4afde02 82 shift->remove_symbol(@_);
83}
84
85sub list_all_package_symbols {
86cf2011 86 deprecated('list_all_package_symbols is deprecated, please use list_all_symbols');
e4afde02 87 shift->list_all_symbols(@_);
88}
89
90=head1 SYNOPSIS
91
92 my $stash = Package::Stash->new('Foo');
93 $stash->add_symbol('%foo', {bar => 1});
94 # $Foo::foo{bar} == 1
95 $stash->has_symbol('$foo') # false
96 my $namespace = $stash->namespace;
97 *{ $namespace->{foo} }{HASH} # {bar => 1}
98
99=head1 DESCRIPTION
100
101Manipulating stashes (Perl's symbol tables) is occasionally necessary, but
102incredibly messy, and easy to get wrong. This module hides all of that behind a
103simple API.
104
105NOTE: Most methods in this class require a variable specification that includes
106a sigil. If this sigil is absent, it is assumed to represent the IO slot.
107
108Due to limitations in the typeglob API available to perl code, and to typeglob
109manipulation in perl being quite slow, this module provides two
110implementations - one in pure perl, and one using XS. The XS implementation is
111to be preferred for most usages; the pure perl one is provided for cases where
112XS modules are not a possibility. The current implementation in use can be set
113by setting C<$ENV{PACKAGE_STASH_IMPLEMENTATION}> or
114C<$Package::Stash::IMPLEMENTATION> before loading Package::Stash (with the
115environment variable taking precedence), otherwise, it will use the XS
116implementation if possible, falling back to the pure perl one.
117
118=method new $package_name
119
120Creates a new C<Package::Stash> object, for the package given as the only
121argument.
122
123=method name
124
125Returns the name of the package that this object represents.
126
127=method namespace
128
129Returns the raw stash itself.
130
131=method add_symbol $variable $value %opts
132
133Adds a new package symbol, for the symbol given as C<$variable>, and optionally
134gives it an initial value of C<$value>. C<$variable> should be the name of
135variable including the sigil, so
136
137 Package::Stash->new('Foo')->add_symbol('%foo')
138
139will create C<%Foo::foo>.
140
141Valid options (all optional) are C<filename>, C<first_line_num>, and
142C<last_line_num>.
143
144C<$opts{filename}>, C<$opts{first_line_num}>, and C<$opts{last_line_num}> can
145be used to indicate where the symbol should be regarded as having been defined.
146Currently these values are only used if the symbol is a subroutine ('C<&>'
147sigil) and only if C<$^P & 0x10> is true, in which case the special C<%DB::sub>
148hash is updated to record the values of C<filename>, C<first_line_num>, and
149C<last_line_num> for the subroutine. If these are not passed, their values are
150inferred (as much as possible) from C<caller> information.
151
152This is especially useful for debuggers and profilers, which use C<%DB::sub> to
153determine where the source code for a subroutine can be found. See
154L<http://perldoc.perl.org/perldebguts.html#Debugger-Internals> for more
155information about C<%DB::sub>.
156
157=method remove_glob $name
158
159Removes all package variables with the given name, regardless of sigil.
160
161=method has_symbol $variable
162
163Returns whether or not the given package variable (including sigil) exists.
164
165=method get_symbol $variable
166
167Returns the value of the given package variable (including sigil).
168
169=method get_or_add_symbol $variable
170
171Like C<get_symbol>, except that it will return an empty hashref or
172arrayref if the variable doesn't exist.
173
174=method remove_symbol $variable
175
176Removes the package variable described by C<$variable> (which includes the
177sigil); other variables with the same name but different sigils will be
178untouched.
179
180=method list_all_symbols $type_filter
181
182Returns a list of package variable names in the package, without sigils. If a
183C<type_filter> is passed, it is used to select package variables of a given
184type, where valid types are the slots of a typeglob ('SCALAR', 'CODE', 'HASH',
185etc). Note that if the package contained any C<BEGIN> blocks, perl will leave
186an empty typeglob in the C<BEGIN> slot, so this will show up if no filter is
187used (and similarly for C<INIT>, C<END>, etc).
188
189=method get_all_symbols $type_filter
190
191Returns a hashref, keyed by the variable names in the package. If
192C<$type_filter> is passed, the hash will contain every variable of that type in
193the package as values, otherwise, it will contain the typeglobs corresponding
194to the variable names (basically, a clone of the stash).
195
196=head1 BUGS / CAVEATS
197
198=over 4
199
7ef54f40 200=item * Prior to perl 5.10, scalar slots are only considered to exist if they are defined
201
202This is due to a shortcoming within perl itself. See
203L<perlref/Making References> point 7 for more information.
204
e4afde02 205=item * GLOB and FORMAT variables are not (yet) accessible through this module.
206
207=item * Also, see the BUGS section for the specific backends (L<Package::Stash::XS> and L<Package::Stash::PP>)
208
209=back
210
211Please report any bugs through RT: email
212C<bug-package-stash at rt.cpan.org>, or browse to
213L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
214
215=head1 SEE ALSO
216
217=over 4
218
219=item * L<Class::MOP::Package>
220
221This module is a factoring out of code that used to live here
222
223=back
224
225=head1 SUPPORT
226
227You can find this documentation for this module with the perldoc command.
228
229 perldoc Package::Stash
230
231You can also look for information at:
232
233=over 4
234
235=item * AnnoCPAN: Annotated CPAN documentation
236
237L<http://annocpan.org/dist/Package-Stash>
238
239=item * CPAN Ratings
240
241L<http://cpanratings.perl.org/d/Package-Stash>
242
243=item * RT: CPAN's request tracker
244
245L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
246
247=item * Search CPAN
248
249L<http://search.cpan.org/dist/Package-Stash>
250
251=back
252
253=head1 AUTHOR
254
255Jesse Luehrs <doy at tozt dot net>
256
257Based on code from L<Class::MOP::Package>, by Stevan Little and the Moose
258Cabal.
259
260=begin Pod::Coverage
261
262add_package_symbol
263remove_package_glob
264has_package_symbol
265get_package_symbol
266get_or_add_package_symbol
267remove_package_symbol
268list_all_package_symbols
269
270=end Pod::Coverage
271
272=cut
273
2741;