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