okay, working implementation selection
[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 {
56 deprecated('add_package_symbol is deprecated, please use add_symbol');
57 shift->add_symbol(@_);
58}
59
60sub remove_package_glob {
61 deprecated('remove_package_glob is deprecated, please use remove_glob');
62 shift->remove_glob(@_);
63}
64
65sub has_package_symbol {
66 deprecated('has_package_symbol is deprecated, please use has_symbol');
67 shift->has_symbol(@_);
68}
69
70sub get_package_symbol {
71 deprecated('get_package_symbol is deprecated, please use get_symbol');
72 shift->get_symbol(@_);
73}
74
75sub 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(@_);
78}
79
80sub remove_package_symbol {
81 deprecated('remove_package_symbol is deprecated, please use remove_symbol');
82 shift->remove_symbol(@_);
83}
84
85sub list_all_package_symbols {
86 deprecated('list_all_package_symbols is deprecated, please use list_all_symbols');
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
200=item * GLOB and FORMAT variables are not (yet) accessible through this module.
201
202=item * Also, see the BUGS section for the specific backends (L<Package::Stash::XS> and L<Package::Stash::PP>)
203
204=back
205
206Please report any bugs through RT: email
207C<bug-package-stash at rt.cpan.org>, or browse to
208L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
209
210=head1 SEE ALSO
211
212=over 4
213
214=item * L<Class::MOP::Package>
215
216This module is a factoring out of code that used to live here
217
218=back
219
220=head1 SUPPORT
221
222You can find this documentation for this module with the perldoc command.
223
224 perldoc Package::Stash
225
226You can also look for information at:
227
228=over 4
229
230=item * AnnoCPAN: Annotated CPAN documentation
231
232L<http://annocpan.org/dist/Package-Stash>
233
234=item * CPAN Ratings
235
236L<http://cpanratings.perl.org/d/Package-Stash>
237
238=item * RT: CPAN's request tracker
239
240L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
241
242=item * Search CPAN
243
244L<http://search.cpan.org/dist/Package-Stash>
245
246=back
247
248=head1 AUTHOR
249
250Jesse Luehrs <doy at tozt dot net>
251
252Based on code from L<Class::MOP::Package>, by Stevan Little and the Moose
253Cabal.
254
255=begin Pod::Coverage
256
257add_package_symbol
258remove_package_glob
259has_package_symbol
260get_package_symbol
261get_or_add_package_symbol
262remove_package_symbol
263list_all_package_symbols
264
265=end Pod::Coverage
266
267=cut
268
2691;