document get_all_symbols
[gitmo/Package-Stash-XS.git] / lib / Package / Stash.pm
CommitLineData
e94260da 1package Package::Stash;
f10f6217 2use strict;
3use warnings;
988beb41 4# ABSTRACT: routines for manipulating stashes
f10f6217 5
59017825 6use XSLoader;
7XSLoader::load(
8 __PACKAGE__,
9 # we need to be careful not to touch $VERSION at compile time, otherwise
10 # DynaLoader will assume it's set and check against it, which will cause
11 # fail when being run in the checkout without dzil having set the actual
12 # $VERSION
13 exists $Package::Stash::{VERSION}
14 ? ${ $Package::Stash::{VERSION} } : (),
15);
16
15c104e2 17use Package::DeprecationManager -deprecations => {
18 'Package::Stash::add_package_symbol' => 0.14,
19 'Package::Stash::remove_package_glob' => 0.14,
20 'Package::Stash::has_package_symbol' => 0.14,
21 'Package::Stash::get_package_symbol' => 0.14,
22 'Package::Stash::get_or_add_package_symbol' => 0.14,
23 'Package::Stash::remove_package_symbol' => 0.14,
24 'Package::Stash::list_all_package_symbols' => 0.14,
25};
26
27sub add_package_symbol {
28 deprecated('add_package_symbol is deprecated, please use add_symbol');
29 shift->add_symbol(@_);
30}
31
32sub remove_package_glob {
33 deprecated('remove_package_glob is deprecated, please use remove_glob');
34 shift->remove_glob(@_);
35}
36
37sub has_package_symbol {
38 deprecated('has_package_symbol is deprecated, please use has_symbol');
39 shift->has_symbol(@_);
40}
41
42sub get_package_symbol {
43 deprecated('get_package_symbol is deprecated, please use get_symbol');
44 shift->get_symbol(@_);
45}
46
47sub get_or_add_package_symbol {
48 deprecated('get_or_add_package_symbol is deprecated, please use get_or_add_symbol');
49 shift->get_or_add_symbol(@_);
50}
51
52sub remove_package_symbol {
53 deprecated('remove_package_symbol is deprecated, please use remove_symbol');
54 shift->remove_symbol(@_);
55}
56
57sub list_all_package_symbols {
58 deprecated('list_all_package_symbols is deprecated, please use list_all_symbols');
59 shift->list_all_symbols(@_);
60}
61
f4979588 62=head1 SYNOPSIS
63
e94260da 64 my $stash = Package::Stash->new('Foo');
15c104e2 65 $stash->add_symbol('%foo', {bar => 1});
683542f5 66 # $Foo::foo{bar} == 1
15c104e2 67 $stash->has_symbol('$foo') # false
683542f5 68 my $namespace = $stash->namespace;
69 *{ $namespace->{foo} }{HASH} # {bar => 1}
f4979588 70
71=head1 DESCRIPTION
72
683542f5 73Manipulating stashes (Perl's symbol tables) is occasionally necessary, but
74incredibly messy, and easy to get wrong. This module hides all of that behind a
75simple API.
76
56a29840 77NOTE: Most methods in this class require a variable specification that includes
78a sigil. If this sigil is absent, it is assumed to represent the IO slot.
79
988beb41 80=method new $package_name
683542f5 81
e94260da 82Creates a new C<Package::Stash> object, for the package given as the only
683542f5 83argument.
f4979588 84
988beb41 85=method name
683542f5 86
87Returns the name of the package that this object represents.
88
988beb41 89=method namespace
683542f5 90
91Returns the raw stash itself.
92
15c104e2 93=method add_symbol $variable $value %opts
683542f5 94
95Adds a new package symbol, for the symbol given as C<$variable>, and optionally
96gives it an initial value of C<$value>. C<$variable> should be the name of
97variable including the sigil, so
98
15c104e2 99 Package::Stash->new('Foo')->add_symbol('%foo')
683542f5 100
101will create C<%Foo::foo>.
102
c61010aa 103Valid options (all optional) are C<filename>, C<first_line_num>, and
104C<last_line_num>.
105
106C<$opts{filename}>, C<$opts{first_line_num}>, and C<$opts{last_line_num}> can
107be used to indicate where the symbol should be regarded as having been defined.
4ada57e0 108Currently these values are only used if the symbol is a subroutine ('C<&>'
c61010aa 109sigil) and only if C<$^P & 0x10> is true, in which case the special C<%DB::sub>
110hash is updated to record the values of C<filename>, C<first_line_num>, and
111C<last_line_num> for the subroutine. If these are not passed, their values are
112inferred (as much as possible) from C<caller> information.
4ada57e0 113
114This is especially useful for debuggers and profilers, which use C<%DB::sub> to
115determine where the source code for a subroutine can be found. See
116L<http://perldoc.perl.org/perldebguts.html#Debugger-Internals> for more
117information about C<%DB::sub>.
118
15c104e2 119=method remove_glob $name
683542f5 120
121Removes all package variables with the given name, regardless of sigil.
122
15c104e2 123=method has_symbol $variable
683542f5 124
125Returns whether or not the given package variable (including sigil) exists.
126
15c104e2 127=method get_symbol $variable
683542f5 128
129Returns the value of the given package variable (including sigil).
130
15c104e2 131=method get_or_add_symbol $variable
e55803fc 132
15c104e2 133Like C<get_symbol>, except that it will return an empty hashref or
e55803fc 134arrayref if the variable doesn't exist.
135
15c104e2 136=method remove_symbol $variable
683542f5 137
138Removes the package variable described by C<$variable> (which includes the
139sigil); other variables with the same name but different sigils will be
140untouched.
141
15c104e2 142=method list_all_symbols $type_filter
683542f5 143
144Returns a list of package variable names in the package, without sigils. If a
145C<type_filter> is passed, it is used to select package variables of a given
146type, where valid types are the slots of a typeglob ('SCALAR', 'CODE', 'HASH',
d1f721b3 147etc). Note that if the package contained any C<BEGIN> blocks, perl will leave
148an empty typeglob in the C<BEGIN> slot, so this will show up if no filter is
149used (and similarly for C<INIT>, C<END>, etc).
683542f5 150
1156452d 151=method get_all_symbols $type_filter
152
153Returns a hashref, keyed by the variable names in the package. If
154C<$type_filter> is passed, the hash will contain every variable of that type in
155the package as values, otherwise, it will contain the typeglobs corresponding
156to the variable names (basically, a clone of the stash).
157
0992f4ec 158=head1 BUGS
159
160No known bugs.
161
162Please report any bugs through RT: email
163C<bug-package-stash at rt.cpan.org>, or browse to
164L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
165
f4979588 166=head1 SEE ALSO
167
f4979588 168=over 4
169
988beb41 170=item * L<Class::MOP::Package>
f4979588 171
988beb41 172This module is a factoring out of code that used to live here
f4979588 173
174=back
175
0992f4ec 176=head1 SUPPORT
177
178You can find this documentation for this module with the perldoc command.
179
180 perldoc Package::Stash
181
182You can also look for information at:
183
184=over 4
185
186=item * AnnoCPAN: Annotated CPAN documentation
187
188L<http://annocpan.org/dist/Package-Stash>
189
190=item * CPAN Ratings
191
192L<http://cpanratings.perl.org/d/Package-Stash>
193
194=item * RT: CPAN's request tracker
195
196L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
197
198=item * Search CPAN
199
200L<http://search.cpan.org/dist/Package-Stash>
201
202=back
203
204=head1 AUTHOR
205
206Jesse Luehrs <doy at tozt dot net>
207
208Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
209Moose Cabal.
210
f4979588 211=cut
212
2131;