1 package Package::Stash;
4 # ABSTRACT: routines for manipulating stashes
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
13 exists $Package::Stash::{VERSION}
14 ? ${ $Package::Stash::{VERSION} } : (),
19 my $stash = Package::Stash->new('Foo');
20 $stash->add_package_symbol('%foo', {bar => 1});
22 $stash->has_package_symbol('$foo') # false
23 my $namespace = $stash->namespace;
24 *{ $namespace->{foo} }{HASH} # {bar => 1}
28 Manipulating stashes (Perl's symbol tables) is occasionally necessary, but
29 incredibly messy, and easy to get wrong. This module hides all of that behind a
32 NOTE: Most methods in this class require a variable specification that includes
33 a sigil. If this sigil is absent, it is assumed to represent the IO slot.
35 =method new $package_name
37 Creates a new C<Package::Stash> object, for the package given as the only
42 Returns the name of the package that this object represents.
46 Returns the raw stash itself.
48 =method add_package_symbol $variable $value %opts
50 Adds a new package symbol, for the symbol given as C<$variable>, and optionally
51 gives it an initial value of C<$value>. C<$variable> should be the name of
52 variable including the sigil, so
54 Package::Stash->new('Foo')->add_package_symbol('%foo')
56 will create C<%Foo::foo>.
58 Valid options (all optional) are C<filename>, C<first_line_num>, and
61 C<$opts{filename}>, C<$opts{first_line_num}>, and C<$opts{last_line_num}> can
62 be used to indicate where the symbol should be regarded as having been defined.
63 Currently these values are only used if the symbol is a subroutine ('C<&>'
64 sigil) and only if C<$^P & 0x10> is true, in which case the special C<%DB::sub>
65 hash is updated to record the values of C<filename>, C<first_line_num>, and
66 C<last_line_num> for the subroutine. If these are not passed, their values are
67 inferred (as much as possible) from C<caller> information.
69 This is especially useful for debuggers and profilers, which use C<%DB::sub> to
70 determine where the source code for a subroutine can be found. See
71 L<http://perldoc.perl.org/perldebguts.html#Debugger-Internals> for more
72 information about C<%DB::sub>.
74 =method remove_package_glob $name
76 Removes all package variables with the given name, regardless of sigil.
78 =method has_package_symbol $variable
80 Returns whether or not the given package variable (including sigil) exists.
82 =method get_package_symbol $variable
84 Returns the value of the given package variable (including sigil).
86 =method get_or_add_package_symbol $variable
88 Like C<get_package_symbol>, except that it will return an empty hashref or
89 arrayref if the variable doesn't exist.
93 sub get_or_add_package_symbol {
95 $self->get_package_symbol(@_, vivify => 1);
98 =method remove_package_symbol $variable
100 Removes the package variable described by C<$variable> (which includes the
101 sigil); other variables with the same name but different sigils will be
104 =method list_all_package_symbols $type_filter
106 Returns a list of package variable names in the package, without sigils. If a
107 C<type_filter> is passed, it is used to select package variables of a given
108 type, where valid types are the slots of a typeglob ('SCALAR', 'CODE', 'HASH',
109 etc). Note that if the package contained any C<BEGIN> blocks, perl will leave
110 an empty typeglob in the C<BEGIN> slot, so this will show up if no filter is
111 used (and similarly for C<INIT>, C<END>, etc).
117 Please report any bugs through RT: email
118 C<bug-package-stash at rt.cpan.org>, or browse to
119 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
125 =item * L<Class::MOP::Package>
127 This module is a factoring out of code that used to live here
133 You can find this documentation for this module with the perldoc command.
135 perldoc Package::Stash
137 You can also look for information at:
141 =item * AnnoCPAN: Annotated CPAN documentation
143 L<http://annocpan.org/dist/Package-Stash>
147 L<http://cpanratings.perl.org/d/Package-Stash>
149 =item * RT: CPAN's request tracker
151 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
155 L<http://search.cpan.org/dist/Package-Stash>
161 Jesse Luehrs <doy at tozt dot net>
163 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the