Commit | Line | Data |
2905fb35 |
1 | package Package::Stash::PP; |
f10f6217 |
2 | use strict; |
3 | use warnings; |
2905fb35 |
4 | # ABSTRACT: pure perl implementation of the Package::Stash API |
f10f6217 |
5 | |
6 | use Carp qw(confess); |
2905fb35 |
7 | use Scalar::Util qw(blessed reftype); |
dc378b60 |
8 | use Symbol; |
9 | # before 5.12, assigning to the ISA glob would make it lose its magical ->isa |
10 | # powers |
11 | use constant BROKEN_ISA_ASSIGNMENT => ($] < 5.012); |
f4979588 |
12 | |
f4979588 |
13 | =head1 SYNOPSIS |
14 | |
2905fb35 |
15 | use Package::Stash; |
f4979588 |
16 | |
17 | =head1 DESCRIPTION |
18 | |
2905fb35 |
19 | This is a backend for L<Package::Stash> implemented in pure perl, for those without a compiler or who would like to use this inline in scripts. |
f4979588 |
20 | |
21 | =cut |
22 | |
f10f6217 |
23 | sub new { |
24 | my $class = shift; |
e55970bc |
25 | my ($package) = @_; |
26 | my $namespace; |
27 | { |
28 | no strict 'refs'; |
29 | # supposedly this caused a bug in earlier perls, but I can't reproduce |
30 | # it, so re-enabling the caching |
31 | $namespace = \%{$package . '::'}; |
32 | } |
33 | return bless { |
34 | 'package' => $package, |
35 | 'namespace' => $namespace, |
36 | }, $class; |
f10f6217 |
37 | } |
38 | |
39 | sub name { |
2905fb35 |
40 | confess "Can't call name as a class method" |
41 | unless blessed($_[0]); |
f10f6217 |
42 | return $_[0]->{package}; |
43 | } |
44 | |
45 | sub namespace { |
2905fb35 |
46 | confess "Can't call namespace as a class method" |
47 | unless blessed($_[0]); |
e55970bc |
48 | return $_[0]->{namespace}; |
f10f6217 |
49 | } |
50 | |
51 | { |
52 | my %SIGIL_MAP = ( |
53 | '$' => 'SCALAR', |
54 | '@' => 'ARRAY', |
55 | '%' => 'HASH', |
56 | '&' => 'CODE', |
56a29840 |
57 | '' => 'IO', |
f10f6217 |
58 | ); |
59 | |
60 | sub _deconstruct_variable_name { |
61 | my ($self, $variable) = @_; |
62 | |
56a29840 |
63 | (defined $variable && length $variable) |
f10f6217 |
64 | || confess "You must pass a variable name"; |
65 | |
66 | my $sigil = substr($variable, 0, 1, ''); |
67 | |
56a29840 |
68 | if (exists $SIGIL_MAP{$sigil}) { |
69 | return ($variable, $sigil, $SIGIL_MAP{$sigil}); |
70 | } |
71 | else { |
72 | return ("${sigil}${variable}", '', $SIGIL_MAP{''}); |
73 | } |
f10f6217 |
74 | } |
75 | } |
76 | |
3634ce60 |
77 | sub _valid_for_type { |
78 | my $self = shift; |
79 | my ($value, $type) = @_; |
80 | if ($type eq 'HASH' || $type eq 'ARRAY' |
81 | || $type eq 'IO' || $type eq 'CODE') { |
82 | return reftype($value) eq $type; |
83 | } |
84 | else { |
85 | my $ref = reftype($value); |
86 | return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE'; |
87 | } |
88 | } |
89 | |
2905fb35 |
90 | sub add_symbol { |
640de369 |
91 | my ($self, $variable, $initial_value, %opts) = @_; |
f10f6217 |
92 | |
93 | my ($name, $sigil, $type) = ref $variable eq 'HASH' |
94 | ? @{$variable}{qw[name sigil type]} |
95 | : $self->_deconstruct_variable_name($variable); |
96 | |
4ada57e0 |
97 | my $pkg = $self->name; |
98 | |
3634ce60 |
99 | if (@_ > 2) { |
100 | $self->_valid_for_type($initial_value, $type) |
101 | || confess "$initial_value is not of type $type"; |
3634ce60 |
102 | |
4ada57e0 |
103 | # cheap fail-fast check for PERLDBf_SUBLINE and '&' |
104 | if ($^P and $^P & 0x10 && $sigil eq '&') { |
640de369 |
105 | my $filename = $opts{filename}; |
106 | my $first_line_num = $opts{first_line_num}; |
4ada57e0 |
107 | |
640de369 |
108 | (undef, $filename, $first_line_num) = caller |
4ada57e0 |
109 | if not defined $filename; |
640de369 |
110 | |
111 | my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0); |
4ada57e0 |
112 | |
113 | # http://perldoc.perl.org/perldebguts.html#Debugger-Internals |
640de369 |
114 | $DB::sub{$pkg . '::' . $name} = "$filename:$first_line_num-$last_line_num"; |
4ada57e0 |
115 | } |
116 | } |
f10f6217 |
117 | |
118 | no strict 'refs'; |
119 | no warnings 'redefine', 'misc', 'prototype'; |
120 | *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value; |
121 | } |
122 | |
2905fb35 |
123 | sub remove_glob { |
f10f6217 |
124 | my ($self, $name) = @_; |
125 | no strict 'refs'; |
126 | delete ${$self->name . '::'}{$name}; |
127 | } |
128 | |
2905fb35 |
129 | sub has_symbol { |
f10f6217 |
130 | my ($self, $variable) = @_; |
131 | |
132 | my ($name, $sigil, $type) = ref $variable eq 'HASH' |
133 | ? @{$variable}{qw[name sigil type]} |
134 | : $self->_deconstruct_variable_name($variable); |
135 | |
136 | my $namespace = $self->namespace; |
137 | |
138 | return unless exists $namespace->{$name}; |
139 | |
140 | my $entry_ref = \$namespace->{$name}; |
141 | if (reftype($entry_ref) eq 'GLOB') { |
f7543739 |
142 | # XXX: assigning to any typeglob slot also initializes the SCALAR slot, |
143 | # and saying that an undef scalar variable doesn't exist is probably |
144 | # vaguely less surprising than a scalar variable popping into existence |
145 | # without anyone defining it |
146 | if ($type eq 'SCALAR') { |
147 | return defined ${ *{$entry_ref}{$type} }; |
148 | } |
149 | else { |
150 | return defined *{$entry_ref}{$type}; |
151 | } |
f10f6217 |
152 | } |
153 | else { |
154 | # a symbol table entry can be -1 (stub), string (stub with prototype), |
155 | # or reference (constant) |
156 | return $type eq 'CODE'; |
157 | } |
158 | } |
159 | |
2905fb35 |
160 | sub get_symbol { |
e55803fc |
161 | my ($self, $variable, %opts) = @_; |
f10f6217 |
162 | |
163 | my ($name, $sigil, $type) = ref $variable eq 'HASH' |
164 | ? @{$variable}{qw[name sigil type]} |
165 | : $self->_deconstruct_variable_name($variable); |
166 | |
167 | my $namespace = $self->namespace; |
168 | |
7486ccf3 |
169 | if (!exists $namespace->{$name}) { |
dc378b60 |
170 | if ($opts{vivify}) { |
171 | if ($type eq 'ARRAY') { |
172 | if (BROKEN_ISA_ASSIGNMENT) { |
2905fb35 |
173 | $self->add_symbol( |
dc378b60 |
174 | $variable, |
175 | $name eq 'ISA' ? () : ([]) |
176 | ); |
177 | } |
178 | else { |
2905fb35 |
179 | $self->add_symbol($variable, []); |
dc378b60 |
180 | } |
181 | } |
182 | elsif ($type eq 'HASH') { |
2905fb35 |
183 | $self->add_symbol($variable, {}); |
dc378b60 |
184 | } |
185 | elsif ($type eq 'SCALAR') { |
2905fb35 |
186 | $self->add_symbol($variable); |
dc378b60 |
187 | } |
188 | elsif ($type eq 'IO') { |
2905fb35 |
189 | $self->add_symbol($variable, Symbol::geniosym); |
dc378b60 |
190 | } |
191 | elsif ($type eq 'CODE') { |
192 | confess "Don't know how to vivify CODE variables"; |
193 | } |
194 | else { |
195 | confess "Unknown type $type in vivication"; |
196 | } |
5d3589c8 |
197 | } |
e55803fc |
198 | else { |
4723417a |
199 | return undef; |
e55803fc |
200 | } |
30d1a098 |
201 | } |
f10f6217 |
202 | |
203 | my $entry_ref = \$namespace->{$name}; |
204 | |
205 | if (ref($entry_ref) eq 'GLOB') { |
206 | return *{$entry_ref}{$type}; |
207 | } |
208 | else { |
209 | if ($type eq 'CODE') { |
210 | no strict 'refs'; |
211 | return \&{ $self->name . '::' . $name }; |
212 | } |
213 | else { |
214 | return undef; |
215 | } |
216 | } |
217 | } |
218 | |
2905fb35 |
219 | sub get_or_add_symbol { |
e55803fc |
220 | my $self = shift; |
2905fb35 |
221 | $self->get_symbol(@_, vivify => 1); |
e55803fc |
222 | } |
223 | |
2905fb35 |
224 | sub remove_symbol { |
f10f6217 |
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 | # FIXME: |
9a3d1390 |
232 | # no doubt this is grossly inefficient and |
f10f6217 |
233 | # could be done much easier and faster in XS |
234 | |
b1a00d0e |
235 | my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = ( |
f10f6217 |
236 | { sigil => '$', type => 'SCALAR', name => $name }, |
237 | { sigil => '@', type => 'ARRAY', name => $name }, |
238 | { sigil => '%', type => 'HASH', name => $name }, |
239 | { sigil => '&', type => 'CODE', name => $name }, |
b1a00d0e |
240 | { sigil => '', type => 'IO', name => $name }, |
f10f6217 |
241 | ); |
242 | |
b1a00d0e |
243 | my ($scalar, $array, $hash, $code, $io); |
f10f6217 |
244 | if ($type eq 'SCALAR') { |
2905fb35 |
245 | $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc); |
246 | $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc); |
247 | $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc); |
248 | $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc); |
f10f6217 |
249 | } |
250 | elsif ($type eq 'ARRAY') { |
2905fb35 |
251 | $scalar = $self->get_symbol($scalar_desc); |
252 | $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc); |
253 | $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc); |
254 | $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc); |
f10f6217 |
255 | } |
256 | elsif ($type eq 'HASH') { |
2905fb35 |
257 | $scalar = $self->get_symbol($scalar_desc); |
258 | $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc); |
259 | $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc); |
260 | $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc); |
f10f6217 |
261 | } |
262 | elsif ($type eq 'CODE') { |
2905fb35 |
263 | $scalar = $self->get_symbol($scalar_desc); |
264 | $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc); |
265 | $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc); |
266 | $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc); |
b1a00d0e |
267 | } |
268 | elsif ($type eq 'IO') { |
2905fb35 |
269 | $scalar = $self->get_symbol($scalar_desc); |
270 | $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc); |
271 | $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc); |
272 | $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc); |
f10f6217 |
273 | } |
274 | else { |
275 | confess "This should never ever ever happen"; |
276 | } |
277 | |
2905fb35 |
278 | $self->remove_glob($name); |
f10f6217 |
279 | |
2905fb35 |
280 | $self->add_symbol($scalar_desc => $scalar); |
281 | $self->add_symbol($array_desc => $array) if defined $array; |
282 | $self->add_symbol($hash_desc => $hash) if defined $hash; |
283 | $self->add_symbol($code_desc => $code) if defined $code; |
284 | $self->add_symbol($io_desc => $io) if defined $io; |
f10f6217 |
285 | } |
286 | |
2905fb35 |
287 | sub list_all_symbols { |
f10f6217 |
288 | my ($self, $type_filter) = @_; |
289 | |
290 | my $namespace = $self->namespace; |
291 | return keys %{$namespace} unless defined $type_filter; |
292 | |
293 | # NOTE: |
9a3d1390 |
294 | # or we can filter based on |
f10f6217 |
295 | # type (SCALAR|ARRAY|HASH|CODE) |
296 | if ($type_filter eq 'CODE') { |
297 | return grep { |
25c87f5c |
298 | # any non-typeglob in the symbol table is a constant or stub |
299 | ref(\$namespace->{$_}) ne 'GLOB' |
300 | # regular subs are stored in the CODE slot of the typeglob |
d1f721b3 |
301 | || defined(*{$namespace->{$_}}{CODE}) |
302 | } keys %{$namespace}; |
303 | } |
304 | elsif ($type_filter eq 'SCALAR') { |
305 | return grep { |
306 | ref(\$namespace->{$_}) eq 'GLOB' |
307 | && defined(${*{$namespace->{$_}}{'SCALAR'}}) |
308 | } keys %{$namespace}; |
309 | } |
310 | else { |
311 | return grep { |
312 | ref(\$namespace->{$_}) eq 'GLOB' |
313 | && defined(*{$namespace->{$_}}{$type_filter}) |
f10f6217 |
314 | } keys %{$namespace}; |
f10f6217 |
315 | } |
316 | } |
f4979588 |
317 | |
2905fb35 |
318 | sub get_all_symbols { |
319 | my ($self, $type_filter) = @_; |
320 | |
321 | my $namespace = $self->namespace; |
322 | return { %{$namespace} } unless defined $type_filter; |
323 | |
324 | return { |
325 | map { $_ => $self->get_symbol({name => $_, type => $type_filter}) } |
326 | $self->list_all_symbols($type_filter) |
327 | } |
328 | } |
329 | |
0992f4ec |
330 | =head1 BUGS |
331 | |
2905fb35 |
332 | =over 4 |
333 | |
334 | =item * Scalar slots are only considered to exist if they are defined |
335 | |
336 | This is due to a shortcoming within perl itself. See |
337 | L<perlref/Making References> point 7 for more information. |
338 | |
339 | =item * remove_symbol also replaces the associated typeglob |
340 | |
341 | This can cause unexpected behavior when doing manipulation at compile time - |
342 | removing subroutines will still allow them to be called from within the package |
343 | as subroutines (although they will not be available as methods). This can be |
344 | considered a feature in some cases (this is how L<namespace::clean> works, for |
345 | instance), but should not be relied upon - use C<remove_glob> directly if you |
346 | want this behavior. |
347 | |
cdb543b5 |
348 | =item * Some minor memory leaks |
349 | |
350 | The pure perl implementation has a couple minor memory leaks (see the TODO |
351 | tests in t/20-leaks.t) that I'm having a hard time tracking down - these may be |
352 | core perl bugs, it's hard to tell. |
353 | |
2905fb35 |
354 | =back |
0992f4ec |
355 | |
356 | Please report any bugs through RT: email |
357 | C<bug-package-stash at rt.cpan.org>, or browse to |
358 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>. |
359 | |
f4979588 |
360 | =head1 SEE ALSO |
361 | |
f4979588 |
362 | =over 4 |
363 | |
988beb41 |
364 | =item * L<Class::MOP::Package> |
f4979588 |
365 | |
988beb41 |
366 | This module is a factoring out of code that used to live here |
f4979588 |
367 | |
368 | =back |
369 | |
0992f4ec |
370 | =head1 SUPPORT |
371 | |
372 | You can find this documentation for this module with the perldoc command. |
373 | |
374 | perldoc Package::Stash |
375 | |
376 | You can also look for information at: |
377 | |
378 | =over 4 |
379 | |
380 | =item * AnnoCPAN: Annotated CPAN documentation |
381 | |
382 | L<http://annocpan.org/dist/Package-Stash> |
383 | |
384 | =item * CPAN Ratings |
385 | |
386 | L<http://cpanratings.perl.org/d/Package-Stash> |
387 | |
388 | =item * RT: CPAN's request tracker |
389 | |
390 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash> |
391 | |
392 | =item * Search CPAN |
393 | |
394 | L<http://search.cpan.org/dist/Package-Stash> |
395 | |
396 | =back |
397 | |
398 | =head1 AUTHOR |
399 | |
400 | Jesse Luehrs <doy at tozt dot net> |
401 | |
402 | Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the |
403 | Moose Cabal. |
404 | |
a912fc4b |
405 | =begin Pod::Coverage |
406 | |
407 | BROKEN_ISA_ASSIGNMENT |
408 | add_symbol |
409 | get_all_symbols |
410 | get_or_add_symbol |
411 | get_symbol |
412 | has_symbol |
413 | list_all_symbols |
414 | name |
415 | namespace |
416 | new |
417 | remove_glob |
418 | |
419 | =end Pod::Coverage |
420 | |
f4979588 |
421 | =cut |
422 | |
423 | 1; |