Commit | Line | Data |
e94260da |
1 | package Package::Stash; |
f10f6217 |
2 | use strict; |
3 | use warnings; |
4 | |
5 | use Carp qw(confess); |
6 | use Scalar::Util qw(reftype); |
f4979588 |
7 | |
8 | =head1 NAME |
9 | |
e94260da |
10 | Package::Stash - routines for manipulating stashes |
f4979588 |
11 | |
12 | =head1 SYNOPSIS |
13 | |
e94260da |
14 | my $stash = Package::Stash->new('Foo'); |
683542f5 |
15 | $stash->add_package_symbol('%foo', {bar => 1}); |
16 | # $Foo::foo{bar} == 1 |
17 | $stash->has_package_symbol('$foo') # false |
18 | my $namespace = $stash->namespace; |
19 | *{ $namespace->{foo} }{HASH} # {bar => 1} |
f4979588 |
20 | |
21 | =head1 DESCRIPTION |
22 | |
683542f5 |
23 | Manipulating stashes (Perl's symbol tables) is occasionally necessary, but |
24 | incredibly messy, and easy to get wrong. This module hides all of that behind a |
25 | simple API. |
26 | |
56a29840 |
27 | NOTE: Most methods in this class require a variable specification that includes |
28 | a sigil. If this sigil is absent, it is assumed to represent the IO slot. |
29 | |
683542f5 |
30 | =head1 METHODS |
31 | |
32 | =cut |
33 | |
34 | =head2 new $package_name |
35 | |
e94260da |
36 | Creates a new C<Package::Stash> object, for the package given as the only |
683542f5 |
37 | argument. |
f4979588 |
38 | |
39 | =cut |
40 | |
f10f6217 |
41 | sub new { |
42 | my $class = shift; |
43 | my ($namespace) = @_; |
60146e1c |
44 | return bless { 'package' => $namespace }, $class; |
f10f6217 |
45 | } |
46 | |
683542f5 |
47 | =head2 name |
48 | |
49 | Returns the name of the package that this object represents. |
50 | |
51 | =cut |
52 | |
f10f6217 |
53 | sub name { |
54 | return $_[0]->{package}; |
55 | } |
56 | |
683542f5 |
57 | =head2 namespace |
58 | |
59 | Returns the raw stash itself. |
60 | |
61 | =cut |
62 | |
f10f6217 |
63 | sub namespace { |
64 | # NOTE: |
65 | # because of issues with the Perl API |
66 | # to the typeglob in some versions, we |
67 | # need to just always grab a new |
68 | # reference to the hash here. Ideally |
69 | # we could just store a ref and it would |
70 | # Just Work, but oh well :\ |
71 | no strict 'refs'; |
72 | return \%{$_[0]->name . '::'}; |
73 | } |
74 | |
75 | { |
76 | my %SIGIL_MAP = ( |
77 | '$' => 'SCALAR', |
78 | '@' => 'ARRAY', |
79 | '%' => 'HASH', |
80 | '&' => 'CODE', |
56a29840 |
81 | '' => 'IO', |
f10f6217 |
82 | ); |
83 | |
84 | sub _deconstruct_variable_name { |
85 | my ($self, $variable) = @_; |
86 | |
56a29840 |
87 | (defined $variable && length $variable) |
f10f6217 |
88 | || confess "You must pass a variable name"; |
89 | |
90 | my $sigil = substr($variable, 0, 1, ''); |
91 | |
56a29840 |
92 | if (exists $SIGIL_MAP{$sigil}) { |
93 | return ($variable, $sigil, $SIGIL_MAP{$sigil}); |
94 | } |
95 | else { |
96 | return ("${sigil}${variable}", '', $SIGIL_MAP{''}); |
97 | } |
f10f6217 |
98 | } |
99 | } |
100 | |
4ada57e0 |
101 | =head2 add_package_symbol $variable $value $filename $firstlinenum $lastlinenum |
683542f5 |
102 | |
103 | Adds a new package symbol, for the symbol given as C<$variable>, and optionally |
104 | gives it an initial value of C<$value>. C<$variable> should be the name of |
105 | variable including the sigil, so |
106 | |
e94260da |
107 | Package::Stash->new('Foo')->add_package_symbol('%foo') |
683542f5 |
108 | |
109 | will create C<%Foo::foo>. |
110 | |
4ada57e0 |
111 | The optional $filename, $firstlinenum, and $lastlinenum arguments can be used |
112 | to indicate where the symbol should be regarded as having been defined. |
113 | Currently these values are only used if the symbol is a subroutine ('C<&>' |
114 | sigil) and only if C<$^P & 0x10> is true. In which case the special |
115 | C<%DB::sub> hash is updated to record the values of $filename, $firstlinenum, |
116 | and $lastlinenum for the subroutine. |
117 | |
118 | This is especially useful for debuggers and profilers, which use C<%DB::sub> to |
119 | determine where the source code for a subroutine can be found. See |
120 | L<http://perldoc.perl.org/perldebguts.html#Debugger-Internals> for more |
121 | information about C<%DB::sub>. |
122 | |
683542f5 |
123 | =cut |
124 | |
3634ce60 |
125 | sub _valid_for_type { |
126 | my $self = shift; |
127 | my ($value, $type) = @_; |
128 | if ($type eq 'HASH' || $type eq 'ARRAY' |
129 | || $type eq 'IO' || $type eq 'CODE') { |
130 | return reftype($value) eq $type; |
131 | } |
132 | else { |
133 | my $ref = reftype($value); |
134 | return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE'; |
135 | } |
136 | } |
137 | |
f10f6217 |
138 | sub add_package_symbol { |
640de369 |
139 | my ($self, $variable, $initial_value, %opts) = @_; |
f10f6217 |
140 | |
141 | my ($name, $sigil, $type) = ref $variable eq 'HASH' |
142 | ? @{$variable}{qw[name sigil type]} |
143 | : $self->_deconstruct_variable_name($variable); |
144 | |
4ada57e0 |
145 | my $pkg = $self->name; |
146 | |
3634ce60 |
147 | if (@_ > 2) { |
148 | $self->_valid_for_type($initial_value, $type) |
149 | || confess "$initial_value is not of type $type"; |
3634ce60 |
150 | |
4ada57e0 |
151 | # cheap fail-fast check for PERLDBf_SUBLINE and '&' |
152 | if ($^P and $^P & 0x10 && $sigil eq '&') { |
640de369 |
153 | my $filename = $opts{filename}; |
154 | my $first_line_num = $opts{first_line_num}; |
4ada57e0 |
155 | |
640de369 |
156 | (undef, $filename, $first_line_num) = caller |
4ada57e0 |
157 | if not defined $filename; |
640de369 |
158 | |
159 | my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0); |
4ada57e0 |
160 | |
161 | # http://perldoc.perl.org/perldebguts.html#Debugger-Internals |
640de369 |
162 | $DB::sub{$pkg . '::' . $name} = "$filename:$first_line_num-$last_line_num"; |
4ada57e0 |
163 | } |
164 | } |
f10f6217 |
165 | |
166 | no strict 'refs'; |
167 | no warnings 'redefine', 'misc', 'prototype'; |
168 | *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value; |
169 | } |
170 | |
683542f5 |
171 | =head2 remove_package_glob $name |
172 | |
173 | Removes all package variables with the given name, regardless of sigil. |
174 | |
175 | =cut |
176 | |
f10f6217 |
177 | sub remove_package_glob { |
178 | my ($self, $name) = @_; |
179 | no strict 'refs'; |
180 | delete ${$self->name . '::'}{$name}; |
181 | } |
182 | |
183 | # ... these functions deal with stuff on the namespace level |
184 | |
683542f5 |
185 | =head2 has_package_symbol $variable |
186 | |
187 | Returns whether or not the given package variable (including sigil) exists. |
188 | |
189 | =cut |
190 | |
f10f6217 |
191 | sub has_package_symbol { |
192 | my ($self, $variable) = @_; |
193 | |
194 | my ($name, $sigil, $type) = ref $variable eq 'HASH' |
195 | ? @{$variable}{qw[name sigil type]} |
196 | : $self->_deconstruct_variable_name($variable); |
197 | |
198 | my $namespace = $self->namespace; |
199 | |
200 | return unless exists $namespace->{$name}; |
201 | |
202 | my $entry_ref = \$namespace->{$name}; |
203 | if (reftype($entry_ref) eq 'GLOB') { |
204 | if ( $type eq 'SCALAR' ) { |
205 | return defined ${ *{$entry_ref}{SCALAR} }; |
206 | } |
207 | else { |
208 | return defined *{$entry_ref}{$type}; |
209 | } |
210 | } |
211 | else { |
212 | # a symbol table entry can be -1 (stub), string (stub with prototype), |
213 | # or reference (constant) |
214 | return $type eq 'CODE'; |
215 | } |
216 | } |
217 | |
683542f5 |
218 | =head2 get_package_symbol $variable |
219 | |
220 | Returns the value of the given package variable (including sigil). |
221 | |
222 | =cut |
223 | |
f10f6217 |
224 | sub get_package_symbol { |
225 | my ($self, $variable) = @_; |
226 | |
227 | my ($name, $sigil, $type) = ref $variable eq 'HASH' |
228 | ? @{$variable}{qw[name sigil type]} |
229 | : $self->_deconstruct_variable_name($variable); |
230 | |
231 | my $namespace = $self->namespace; |
232 | |
30d1a098 |
233 | if (!exists $namespace->{$name}) { |
b746972e |
234 | # assigning to the result of this function like |
235 | # @{$stash->get_package_symbol('@ISA')} = @new_ISA |
236 | # makes the result not visible until the variable is explicitly |
237 | # accessed... in the case of @ISA, this might never happen |
238 | # for instance, assigning like that and then calling $obj->isa |
239 | # will fail. see t/005-isa.t |
240 | if ($type eq 'ARRAY' && $name ne 'ISA') { |
a2a2164a |
241 | $self->add_package_symbol($variable, []); |
242 | } |
243 | elsif ($type eq 'HASH') { |
244 | $self->add_package_symbol($variable, {}); |
245 | } |
246 | else { |
247 | # FIXME |
248 | $self->add_package_symbol($variable) |
249 | } |
30d1a098 |
250 | } |
f10f6217 |
251 | |
252 | my $entry_ref = \$namespace->{$name}; |
253 | |
254 | if (ref($entry_ref) eq 'GLOB') { |
255 | return *{$entry_ref}{$type}; |
256 | } |
257 | else { |
258 | if ($type eq 'CODE') { |
259 | no strict 'refs'; |
260 | return \&{ $self->name . '::' . $name }; |
261 | } |
262 | else { |
263 | return undef; |
264 | } |
265 | } |
266 | } |
267 | |
683542f5 |
268 | =head2 remove_package_symbol $variable |
269 | |
270 | Removes the package variable described by C<$variable> (which includes the |
271 | sigil); other variables with the same name but different sigils will be |
272 | untouched. |
273 | |
274 | =cut |
275 | |
f10f6217 |
276 | sub remove_package_symbol { |
277 | my ($self, $variable) = @_; |
278 | |
279 | my ($name, $sigil, $type) = ref $variable eq 'HASH' |
280 | ? @{$variable}{qw[name sigil type]} |
281 | : $self->_deconstruct_variable_name($variable); |
282 | |
283 | # FIXME: |
284 | # no doubt this is grossly inefficient and |
285 | # could be done much easier and faster in XS |
286 | |
b1a00d0e |
287 | my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = ( |
f10f6217 |
288 | { sigil => '$', type => 'SCALAR', name => $name }, |
289 | { sigil => '@', type => 'ARRAY', name => $name }, |
290 | { sigil => '%', type => 'HASH', name => $name }, |
291 | { sigil => '&', type => 'CODE', name => $name }, |
b1a00d0e |
292 | { sigil => '', type => 'IO', name => $name }, |
f10f6217 |
293 | ); |
294 | |
b1a00d0e |
295 | my ($scalar, $array, $hash, $code, $io); |
f10f6217 |
296 | if ($type eq 'SCALAR') { |
297 | $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc); |
298 | $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc); |
299 | $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc); |
b1a00d0e |
300 | $io = $self->get_package_symbol($io_desc) if $self->has_package_symbol($io_desc); |
f10f6217 |
301 | } |
302 | elsif ($type eq 'ARRAY') { |
42fa5cfc |
303 | $scalar = $self->get_package_symbol($scalar_desc); |
f10f6217 |
304 | $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc); |
305 | $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc); |
b1a00d0e |
306 | $io = $self->get_package_symbol($io_desc) if $self->has_package_symbol($io_desc); |
f10f6217 |
307 | } |
308 | elsif ($type eq 'HASH') { |
42fa5cfc |
309 | $scalar = $self->get_package_symbol($scalar_desc); |
f10f6217 |
310 | $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc); |
311 | $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc); |
b1a00d0e |
312 | $io = $self->get_package_symbol($io_desc) if $self->has_package_symbol($io_desc); |
f10f6217 |
313 | } |
314 | elsif ($type eq 'CODE') { |
42fa5cfc |
315 | $scalar = $self->get_package_symbol($scalar_desc); |
f10f6217 |
316 | $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc); |
317 | $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc); |
b1a00d0e |
318 | $io = $self->get_package_symbol($io_desc) if $self->has_package_symbol($io_desc); |
319 | } |
320 | elsif ($type eq 'IO') { |
42fa5cfc |
321 | $scalar = $self->get_package_symbol($scalar_desc); |
b1a00d0e |
322 | $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc); |
323 | $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc); |
324 | $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc); |
f10f6217 |
325 | } |
326 | else { |
327 | confess "This should never ever ever happen"; |
328 | } |
329 | |
330 | $self->remove_package_glob($name); |
331 | |
42fa5cfc |
332 | $self->add_package_symbol($scalar_desc => $scalar); |
f10f6217 |
333 | $self->add_package_symbol($array_desc => $array) if defined $array; |
334 | $self->add_package_symbol($hash_desc => $hash) if defined $hash; |
335 | $self->add_package_symbol($code_desc => $code) if defined $code; |
b1a00d0e |
336 | $self->add_package_symbol($io_desc => $io) if defined $io; |
f10f6217 |
337 | } |
338 | |
683542f5 |
339 | =head2 list_all_package_symbols $type_filter |
340 | |
341 | Returns a list of package variable names in the package, without sigils. If a |
342 | C<type_filter> is passed, it is used to select package variables of a given |
343 | type, where valid types are the slots of a typeglob ('SCALAR', 'CODE', 'HASH', |
344 | etc). |
345 | |
346 | =cut |
347 | |
f10f6217 |
348 | sub list_all_package_symbols { |
349 | my ($self, $type_filter) = @_; |
350 | |
351 | my $namespace = $self->namespace; |
352 | return keys %{$namespace} unless defined $type_filter; |
353 | |
354 | # NOTE: |
355 | # or we can filter based on |
356 | # type (SCALAR|ARRAY|HASH|CODE) |
357 | if ($type_filter eq 'CODE') { |
358 | return grep { |
359 | (ref($namespace->{$_}) |
360 | ? (ref($namespace->{$_}) eq 'SCALAR') |
361 | : (ref(\$namespace->{$_}) eq 'GLOB' |
362 | && defined(*{$namespace->{$_}}{CODE}))); |
363 | } keys %{$namespace}; |
364 | } else { |
365 | return grep { *{$namespace->{$_}}{$type_filter} } keys %{$namespace}; |
366 | } |
367 | } |
f4979588 |
368 | |
369 | =head1 BUGS |
370 | |
371 | No known bugs. |
372 | |
373 | Please report any bugs through RT: email |
e94260da |
374 | C<bug-package-stash at rt.cpan.org>, or browse to |
375 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>. |
f4979588 |
376 | |
377 | =head1 SEE ALSO |
378 | |
683542f5 |
379 | L<Class::MOP::Package> - this module is a factoring out of code that used to |
380 | live here |
f4979588 |
381 | |
382 | =head1 SUPPORT |
383 | |
384 | You can find this documentation for this module with the perldoc command. |
385 | |
e94260da |
386 | perldoc Package::Stash |
f4979588 |
387 | |
388 | You can also look for information at: |
389 | |
390 | =over 4 |
391 | |
392 | =item * AnnoCPAN: Annotated CPAN documentation |
393 | |
e94260da |
394 | L<http://annocpan.org/dist/Package-Stash> |
f4979588 |
395 | |
396 | =item * CPAN Ratings |
397 | |
e94260da |
398 | L<http://cpanratings.perl.org/d/Package-Stash> |
f4979588 |
399 | |
400 | =item * RT: CPAN's request tracker |
401 | |
e94260da |
402 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash> |
f4979588 |
403 | |
404 | =item * Search CPAN |
405 | |
e94260da |
406 | L<http://search.cpan.org/dist/Package-Stash> |
f4979588 |
407 | |
408 | =back |
409 | |
410 | =head1 AUTHOR |
411 | |
412 | Jesse Luehrs <doy at tozt dot net> |
413 | |
683542f5 |
414 | Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the |
415 | Moose Cabal. |
416 | |
f4979588 |
417 | =head1 COPYRIGHT AND LICENSE |
418 | |
419 | This software is copyright (c) 2010 by Jesse Luehrs. |
420 | |
421 | This is free software; you can redistribute it and/or modify it under |
422 | the same terms as perl itself. |
423 | |
424 | =cut |
425 | |
426 | 1; |