1 package Package::Stash::PP;
4 # ABSTRACT: pure perl implementation of the Package::Stash API
7 use Scalar::Util qw(blessed reftype weaken);
9 # before 5.12, assigning to the ISA glob would make it lose its magical ->isa
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);
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.
31 'package' => $package,
36 confess "Can't call name as a class method"
37 unless blessed($_[0]);
38 return $_[0]->{package};
42 confess "Can't call namespace as a class method"
43 unless blessed($_[0]);
45 if (BROKEN_WEAK_STASH) {
47 return \%{$_[0]->name . '::'};
50 return $_[0]->{namespace} if defined $_[0]->{namespace};
54 $_[0]->{namespace} = \%{$_[0]->name . '::'};
57 weaken($_[0]->{namespace});
59 return $_[0]->{namespace};
72 sub _deconstruct_variable_name {
73 my ($self, $variable) = @_;
75 (defined $variable && length $variable)
76 || confess "You must pass a variable name";
78 my $sigil = substr($variable, 0, 1, '');
80 if (exists $SIGIL_MAP{$sigil}) {
81 return ($variable, $sigil, $SIGIL_MAP{$sigil});
84 return ("${sigil}${variable}", '', $SIGIL_MAP{''});
91 my ($value, $type) = @_;
92 if ($type eq 'HASH' || $type eq 'ARRAY'
93 || $type eq 'IO' || $type eq 'CODE') {
94 return reftype($value) eq $type;
97 my $ref = reftype($value);
98 return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE';
103 my ($self, $variable, $initial_value, %opts) = @_;
105 my ($name, $sigil, $type) = ref $variable eq 'HASH'
106 ? @{$variable}{qw[name sigil type]}
107 : $self->_deconstruct_variable_name($variable);
109 my $pkg = $self->name;
112 $self->_valid_for_type($initial_value, $type)
113 || confess "$initial_value is not of type $type";
115 # cheap fail-fast check for PERLDBf_SUBLINE and '&'
116 if ($^P and $^P & 0x10 && $sigil eq '&') {
117 my $filename = $opts{filename};
118 my $first_line_num = $opts{first_line_num};
120 (undef, $filename, $first_line_num) = caller
121 if not defined $filename;
123 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
125 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
126 $DB::sub{$pkg . '::' . $name} = "$filename:$first_line_num-$last_line_num";
131 no warnings 'redefine', 'misc', 'prototype';
132 *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
136 my ($self, $name) = @_;
137 delete $self->namespace->{$name};
141 my ($self, $variable) = @_;
143 my ($name, $sigil, $type) = ref $variable eq 'HASH'
144 ? @{$variable}{qw[name sigil type]}
145 : $self->_deconstruct_variable_name($variable);
147 my $namespace = $self->namespace;
149 return unless exists $namespace->{$name};
151 my $entry_ref = \$namespace->{$name};
152 if (reftype($entry_ref) eq 'GLOB') {
153 # XXX: assigning to any typeglob slot also initializes the SCALAR slot,
154 # and saying that an undef scalar variable doesn't exist is probably
155 # vaguely less surprising than a scalar variable popping into existence
156 # without anyone defining it
157 if ($type eq 'SCALAR') {
158 return defined ${ *{$entry_ref}{$type} };
161 return defined *{$entry_ref}{$type};
165 # a symbol table entry can be -1 (stub), string (stub with prototype),
166 # or reference (constant)
167 return $type eq 'CODE';
172 my ($self, $variable, %opts) = @_;
174 my ($name, $sigil, $type) = ref $variable eq 'HASH'
175 ? @{$variable}{qw[name sigil type]}
176 : $self->_deconstruct_variable_name($variable);
178 my $namespace = $self->namespace;
180 if (!exists $namespace->{$name}) {
182 if ($type eq 'ARRAY') {
183 if (BROKEN_ISA_ASSIGNMENT) {
186 $name eq 'ISA' ? () : ([])
190 $self->add_symbol($variable, []);
193 elsif ($type eq 'HASH') {
194 $self->add_symbol($variable, {});
196 elsif ($type eq 'SCALAR') {
197 $self->add_symbol($variable);
199 elsif ($type eq 'IO') {
200 $self->add_symbol($variable, Symbol::geniosym);
202 elsif ($type eq 'CODE') {
203 confess "Don't know how to vivify CODE variables";
206 confess "Unknown type $type in vivication";
214 my $entry_ref = \$namespace->{$name};
216 if (ref($entry_ref) eq 'GLOB') {
217 return *{$entry_ref}{$type};
220 if ($type eq 'CODE') {
222 return \&{ $self->name . '::' . $name };
230 sub get_or_add_symbol {
232 $self->get_symbol(@_, vivify => 1);
236 my ($self, $variable) = @_;
238 my ($name, $sigil, $type) = ref $variable eq 'HASH'
239 ? @{$variable}{qw[name sigil type]}
240 : $self->_deconstruct_variable_name($variable);
243 # no doubt this is grossly inefficient and
244 # could be done much easier and faster in XS
246 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
247 { sigil => '$', type => 'SCALAR', name => $name },
248 { sigil => '@', type => 'ARRAY', name => $name },
249 { sigil => '%', type => 'HASH', name => $name },
250 { sigil => '&', type => 'CODE', name => $name },
251 { sigil => '', type => 'IO', name => $name },
254 my ($scalar, $array, $hash, $code, $io);
255 if ($type eq 'SCALAR') {
256 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
257 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
258 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
259 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
261 elsif ($type eq 'ARRAY') {
262 $scalar = $self->get_symbol($scalar_desc);
263 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
264 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
265 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
267 elsif ($type eq 'HASH') {
268 $scalar = $self->get_symbol($scalar_desc);
269 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
270 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
271 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
273 elsif ($type eq 'CODE') {
274 $scalar = $self->get_symbol($scalar_desc);
275 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
276 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
277 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
279 elsif ($type eq 'IO') {
280 $scalar = $self->get_symbol($scalar_desc);
281 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
282 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
283 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
286 confess "This should never ever ever happen";
289 $self->remove_glob($name);
291 $self->add_symbol($scalar_desc => $scalar);
292 $self->add_symbol($array_desc => $array) if defined $array;
293 $self->add_symbol($hash_desc => $hash) if defined $hash;
294 $self->add_symbol($code_desc => $code) if defined $code;
295 $self->add_symbol($io_desc => $io) if defined $io;
298 sub list_all_symbols {
299 my ($self, $type_filter) = @_;
301 my $namespace = $self->namespace;
302 return keys %{$namespace} unless defined $type_filter;
305 # or we can filter based on
306 # type (SCALAR|ARRAY|HASH|CODE)
307 if ($type_filter eq 'CODE') {
309 # any non-typeglob in the symbol table is a constant or stub
310 ref(\$namespace->{$_}) ne 'GLOB'
311 # regular subs are stored in the CODE slot of the typeglob
312 || defined(*{$namespace->{$_}}{CODE})
313 } keys %{$namespace};
315 elsif ($type_filter eq 'SCALAR') {
317 ref(\$namespace->{$_}) eq 'GLOB'
318 && defined(${*{$namespace->{$_}}{'SCALAR'}})
319 } keys %{$namespace};
323 ref(\$namespace->{$_}) eq 'GLOB'
324 && defined(*{$namespace->{$_}}{$type_filter})
325 } keys %{$namespace};
329 sub get_all_symbols {
330 my ($self, $type_filter) = @_;
332 my $namespace = $self->namespace;
333 return { %{$namespace} } unless defined $type_filter;
336 map { $_ => $self->get_symbol({name => $_, type => $type_filter}) }
337 $self->list_all_symbols($type_filter)
345 =item * Scalar slots are only considered to exist if they are defined
347 This is due to a shortcoming within perl itself. See
348 L<perlref/Making References> point 7 for more information.
350 =item * remove_symbol also replaces the associated typeglob
352 This can cause unexpected behavior when doing manipulation at compile time -
353 removing subroutines will still allow them to be called from within the package
354 as subroutines (although they will not be available as methods). This can be
355 considered a feature in some cases (this is how L<namespace::clean> works, for
356 instance), but should not be relied upon - use C<remove_glob> directly if you
359 =item * Some minor memory leaks
361 The pure perl implementation has a couple minor memory leaks (see the TODO
362 tests in t/20-leaks.t) that I'm having a hard time tracking down - these may be
363 core perl bugs, it's hard to tell.
367 Please report any bugs through RT: email
368 C<bug-package-stash at rt.cpan.org>, or browse to
369 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
375 =item * L<Class::MOP::Package>
377 This module is a factoring out of code that used to live here
383 You can find this documentation for this module with the perldoc command.
385 perldoc Package::Stash
387 You can also look for information at:
391 =item * AnnoCPAN: Annotated CPAN documentation
393 L<http://annocpan.org/dist/Package-Stash>
397 L<http://cpanratings.perl.org/d/Package-Stash>
399 =item * RT: CPAN's request tracker
401 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
405 L<http://search.cpan.org/dist/Package-Stash>
411 Jesse Luehrs <doy at tozt dot net>
413 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
418 BROKEN_ISA_ASSIGNMENT