Add ppport.h and use it in Stash.xs - still need more work for 5.8.x
[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
f4979588 17=head1 SYNOPSIS
18
e94260da 19 my $stash = Package::Stash->new('Foo');
683542f5 20 $stash->add_package_symbol('%foo', {bar => 1});
21 # $Foo::foo{bar} == 1
22 $stash->has_package_symbol('$foo') # false
23 my $namespace = $stash->namespace;
24 *{ $namespace->{foo} }{HASH} # {bar => 1}
f4979588 25
26=head1 DESCRIPTION
27
683542f5 28Manipulating stashes (Perl's symbol tables) is occasionally necessary, but
29incredibly messy, and easy to get wrong. This module hides all of that behind a
30simple API.
31
56a29840 32NOTE: Most methods in this class require a variable specification that includes
33a sigil. If this sigil is absent, it is assumed to represent the IO slot.
34
988beb41 35=method new $package_name
683542f5 36
e94260da 37Creates a new C<Package::Stash> object, for the package given as the only
683542f5 38argument.
f4979588 39
988beb41 40=method name
683542f5 41
42Returns the name of the package that this object represents.
43
988beb41 44=method namespace
683542f5 45
46Returns the raw stash itself.
47
988beb41 48=method add_package_symbol $variable $value %opts
683542f5 49
50Adds a new package symbol, for the symbol given as C<$variable>, and optionally
51gives it an initial value of C<$value>. C<$variable> should be the name of
52variable including the sigil, so
53
e94260da 54 Package::Stash->new('Foo')->add_package_symbol('%foo')
683542f5 55
56will create C<%Foo::foo>.
57
c61010aa 58Valid options (all optional) are C<filename>, C<first_line_num>, and
59C<last_line_num>.
60
61C<$opts{filename}>, C<$opts{first_line_num}>, and C<$opts{last_line_num}> can
62be used to indicate where the symbol should be regarded as having been defined.
4ada57e0 63Currently these values are only used if the symbol is a subroutine ('C<&>'
c61010aa 64sigil) and only if C<$^P & 0x10> is true, in which case the special C<%DB::sub>
65hash is updated to record the values of C<filename>, C<first_line_num>, and
66C<last_line_num> for the subroutine. If these are not passed, their values are
67inferred (as much as possible) from C<caller> information.
4ada57e0 68
69This is especially useful for debuggers and profilers, which use C<%DB::sub> to
70determine where the source code for a subroutine can be found. See
71L<http://perldoc.perl.org/perldebguts.html#Debugger-Internals> for more
72information about C<%DB::sub>.
73
988beb41 74=method remove_package_glob $name
683542f5 75
76Removes all package variables with the given name, regardless of sigil.
77
988beb41 78=method has_package_symbol $variable
683542f5 79
80Returns whether or not the given package variable (including sigil) exists.
81
988beb41 82=method get_package_symbol $variable
683542f5 83
84Returns the value of the given package variable (including sigil).
85
988beb41 86=method get_or_add_package_symbol $variable
e55803fc 87
88Like C<get_package_symbol>, except that it will return an empty hashref or
89arrayref if the variable doesn't exist.
90
988beb41 91=method remove_package_symbol $variable
683542f5 92
93Removes the package variable described by C<$variable> (which includes the
94sigil); other variables with the same name but different sigils will be
95untouched.
96
988beb41 97=method list_all_package_symbols $type_filter
683542f5 98
99Returns a list of package variable names in the package, without sigils. If a
100C<type_filter> is passed, it is used to select package variables of a given
101type, where valid types are the slots of a typeglob ('SCALAR', 'CODE', 'HASH',
d1f721b3 102etc). Note that if the package contained any C<BEGIN> blocks, perl will leave
103an empty typeglob in the C<BEGIN> slot, so this will show up if no filter is
104used (and similarly for C<INIT>, C<END>, etc).
683542f5 105
0992f4ec 106=head1 BUGS
107
108No known bugs.
109
110Please report any bugs through RT: email
111C<bug-package-stash at rt.cpan.org>, or browse to
112L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
113
f4979588 114=head1 SEE ALSO
115
f4979588 116=over 4
117
988beb41 118=item * L<Class::MOP::Package>
f4979588 119
988beb41 120This module is a factoring out of code that used to live here
f4979588 121
122=back
123
0992f4ec 124=head1 SUPPORT
125
126You can find this documentation for this module with the perldoc command.
127
128 perldoc Package::Stash
129
130You can also look for information at:
131
132=over 4
133
134=item * AnnoCPAN: Annotated CPAN documentation
135
136L<http://annocpan.org/dist/Package-Stash>
137
138=item * CPAN Ratings
139
140L<http://cpanratings.perl.org/d/Package-Stash>
141
142=item * RT: CPAN's request tracker
143
144L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
145
146=item * Search CPAN
147
148L<http://search.cpan.org/dist/Package-Stash>
149
150=back
151
152=head1 AUTHOR
153
154Jesse Luehrs <doy at tozt dot net>
155
156Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
157Moose Cabal.
158
f4979588 159=cut
160
1611;