1Package::Stash(3) User Contributed Perl Documentation Package::Stash(3)
2
3
4
6 Package::Stash - routines for manipulating stashes
7
9 version 0.34
10
12 my $stash = Package::Stash->new('Foo');
13 $stash->add_symbol('%foo', {bar => 1});
14 # $Foo::foo{bar} == 1
15 $stash->has_symbol('$foo') # false
16 my $namespace = $stash->namespace;
17 *{ $namespace->{foo} }{HASH} # {bar => 1}
18
20 Manipulating stashes (Perl's symbol tables) is occasionally necessary,
21 but incredibly messy, and easy to get wrong. This module hides all of
22 that behind a simple API.
23
24 NOTE: Most methods in this class require a variable specification that
25 includes a sigil. If this sigil is absent, it is assumed to represent
26 the IO slot.
27
28 Due to limitations in the typeglob API available to perl code, and to
29 typeglob manipulation in perl being quite slow, this module provides
30 two implementations - one in pure perl, and one using XS. The XS
31 implementation is to be preferred for most usages; the pure perl one is
32 provided for cases where XS modules are not a possibility. The current
33 implementation in use can be set by setting
34 $ENV{PACKAGE_STASH_IMPLEMENTATION} or $Package::Stash::IMPLEMENTATION
35 before loading Package::Stash (with the environment variable taking
36 precedence), otherwise, it will use the XS implementation if possible,
37 falling back to the pure perl one.
38
40 new $package_name
41 Creates a new "Package::Stash" object, for the package given as the
42 only argument.
43
44 name
45 Returns the name of the package that this object represents.
46
47 namespace
48 Returns the raw stash itself.
49
50 add_symbol $variable $value %opts
51 Adds a new package symbol, for the symbol given as $variable, and
52 optionally gives it an initial value of $value. $variable should be the
53 name of variable including the sigil, so
54
55 Package::Stash->new('Foo')->add_symbol('%foo')
56
57 will create %Foo::foo.
58
59 Valid options (all optional) are "filename", "first_line_num", and
60 "last_line_num".
61
62 $opts{filename}, $opts{first_line_num}, and $opts{last_line_num} can be
63 used to indicate where the symbol should be regarded as having been
64 defined. Currently these values are only used if the symbol is a
65 subroutine ('"&"' sigil) and only if "$^P & 0x10" is true, in which
66 case the special %DB::sub hash is updated to record the values of
67 "filename", "first_line_num", and "last_line_num" for the subroutine.
68 If these are not passed, their values are inferred (as much as
69 possible) from "caller" information.
70
71 This is especially useful for debuggers and profilers, which use
72 %DB::sub to determine where the source code for a subroutine can be
73 found. See
74 <http://perldoc.perl.org/perldebguts.html#Debugger-Internals> for more
75 information about %DB::sub.
76
77 remove_glob $name
78 Removes all package variables with the given name, regardless of sigil.
79
80 has_symbol $variable
81 Returns whether or not the given package variable (including sigil)
82 exists.
83
84 get_symbol $variable
85 Returns the value of the given package variable (including sigil).
86
87 get_or_add_symbol $variable
88 Like "get_symbol", except that it will return an empty hashref or
89 arrayref if the variable doesn't exist.
90
91 remove_symbol $variable
92 Removes the package variable described by $variable (which includes the
93 sigil); other variables with the same name but different sigils will be
94 untouched.
95
96 list_all_symbols $type_filter
97 Returns a list of package variable names in the package, without
98 sigils. If a "type_filter" is passed, it is used to select package
99 variables of a given type, where valid types are the slots of a
100 typeglob ('SCALAR', 'CODE', 'HASH', etc). Note that if the package
101 contained any "BEGIN" blocks, perl will leave an empty typeglob in the
102 "BEGIN" slot, so this will show up if no filter is used (and similarly
103 for "INIT", "END", etc).
104
105 get_all_symbols $type_filter
106 Returns a hashref, keyed by the variable names in the package. If
107 $type_filter is passed, the hash will contain every variable of that
108 type in the package as values, otherwise, it will contain the typeglobs
109 corresponding to the variable names (basically, a clone of the stash).
110
112 · Prior to perl 5.10, scalar slots are only considered to exist if
113 they are defined
114
115 This is due to a shortcoming within perl itself. See "Making
116 References" in perlref point 7 for more information.
117
118 · GLOB and FORMAT variables are not (yet) accessible through this
119 module.
120
121 · Also, see the BUGS section for the specific backends
122 (Package::Stash::XS and Package::Stash::PP)
123
124 Please report any bugs through RT: email "bug-package-stash at
125 rt.cpan.org", or browse to
126 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
127
129 · Class::MOP::Package
130
131 This module is a factoring out of code that used to live here
132
134 You can find this documentation for this module with the perldoc
135 command.
136
137 perldoc Package::Stash
138
139 You can also look for information at:
140
141 · AnnoCPAN: Annotated CPAN documentation
142
143 <http://annocpan.org/dist/Package-Stash>
144
145 · CPAN Ratings
146
147 <http://cpanratings.perl.org/d/Package-Stash>
148
149 · RT: CPAN's request tracker
150
151 <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
152
153 · Search CPAN
154
155 <http://search.cpan.org/dist/Package-Stash>
156
158 Jesse Luehrs <doy at tozt dot net>
159
160 Based on code from Class::MOP::Package, by Stevan Little and the Moose
161 Cabal.
162
164 Jesse Luehrs <doy at tozt dot net>
165
167 This software is copyright (c) 2013 by Jesse Luehrs.
168
169 This is free software; you can redistribute it and/or modify it under
170 the same terms as the Perl 5 programming language system itself.
171
172
173
174perl v5.16.3 2013-01-04 Package::Stash(3)