1 package Package::Stash::PP;
4 # ABSTRACT: pure perl implementation of the Package::Stash API
7 use Scalar::Util qw(blessed reftype);
9 # before 5.12, assigning to the ISA glob would make it lose its magical ->isa
11 use constant BROKEN_ISA_ASSIGNMENT => ($] < 5.012);
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.
29 # supposedly this caused a bug in earlier perls, but I can't reproduce
30 # it, so re-enabling the caching
31 $namespace = \%{$package . '::'};
34 'package' => $package,
35 'namespace' => $namespace,
40 confess "Can't call name as a class method"
41 unless blessed($_[0]);
42 return $_[0]->{package};
46 confess "Can't call namespace as a class method"
47 unless blessed($_[0]);
48 return $_[0]->{namespace};
60 sub _deconstruct_variable_name {
61 my ($self, $variable) = @_;
63 (defined $variable && length $variable)
64 || confess "You must pass a variable name";
66 my $sigil = substr($variable, 0, 1, '');
68 if (exists $SIGIL_MAP{$sigil}) {
69 return ($variable, $sigil, $SIGIL_MAP{$sigil});
72 return ("${sigil}${variable}", '', $SIGIL_MAP{''});
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;
85 my $ref = reftype($value);
86 return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE';
91 my ($self, $variable, $initial_value, %opts) = @_;
93 my ($name, $sigil, $type) = ref $variable eq 'HASH'
94 ? @{$variable}{qw[name sigil type]}
95 : $self->_deconstruct_variable_name($variable);
97 my $pkg = $self->name;
100 $self->_valid_for_type($initial_value, $type)
101 || confess "$initial_value is not of type $type";
103 # cheap fail-fast check for PERLDBf_SUBLINE and '&'
104 if ($^P and $^P & 0x10 && $sigil eq '&') {
105 my $filename = $opts{filename};
106 my $first_line_num = $opts{first_line_num};
108 (undef, $filename, $first_line_num) = caller
109 if not defined $filename;
111 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
113 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
114 $DB::sub{$pkg . '::' . $name} = "$filename:$first_line_num-$last_line_num";
119 no warnings 'redefine', 'misc', 'prototype';
120 *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
124 my ($self, $name) = @_;
126 delete ${$self->name . '::'}{$name};
130 my ($self, $variable) = @_;
132 my ($name, $sigil, $type) = ref $variable eq 'HASH'
133 ? @{$variable}{qw[name sigil type]}
134 : $self->_deconstruct_variable_name($variable);
136 my $namespace = $self->namespace;
138 return unless exists $namespace->{$name};
140 my $entry_ref = \$namespace->{$name};
141 if (reftype($entry_ref) eq 'GLOB') {
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} };
150 return defined *{$entry_ref}{$type};
154 # a symbol table entry can be -1 (stub), string (stub with prototype),
155 # or reference (constant)
156 return $type eq 'CODE';
161 my ($self, $variable, %opts) = @_;
163 my ($name, $sigil, $type) = ref $variable eq 'HASH'
164 ? @{$variable}{qw[name sigil type]}
165 : $self->_deconstruct_variable_name($variable);
167 my $namespace = $self->namespace;
169 if (!exists $namespace->{$name}) {
171 if ($type eq 'ARRAY') {
172 if (BROKEN_ISA_ASSIGNMENT) {
175 $name eq 'ISA' ? () : ([])
179 $self->add_symbol($variable, []);
182 elsif ($type eq 'HASH') {
183 $self->add_symbol($variable, {});
185 elsif ($type eq 'SCALAR') {
186 $self->add_symbol($variable);
188 elsif ($type eq 'IO') {
189 $self->add_symbol($variable, Symbol::geniosym);
191 elsif ($type eq 'CODE') {
192 confess "Don't know how to vivify CODE variables";
195 confess "Unknown type $type in vivication";
203 my $entry_ref = \$namespace->{$name};
205 if (ref($entry_ref) eq 'GLOB') {
206 return *{$entry_ref}{$type};
209 if ($type eq 'CODE') {
211 return \&{ $self->name . '::' . $name };
219 sub get_or_add_symbol {
221 $self->get_symbol(@_, vivify => 1);
225 my ($self, $variable) = @_;
227 my ($name, $sigil, $type) = ref $variable eq 'HASH'
228 ? @{$variable}{qw[name sigil type]}
229 : $self->_deconstruct_variable_name($variable);
232 # no doubt this is grossly inefficient and
233 # could be done much easier and faster in XS
235 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
236 { sigil => '$', type => 'SCALAR', name => $name },
237 { sigil => '@', type => 'ARRAY', name => $name },
238 { sigil => '%', type => 'HASH', name => $name },
239 { sigil => '&', type => 'CODE', name => $name },
240 { sigil => '', type => 'IO', name => $name },
243 my ($scalar, $array, $hash, $code, $io);
244 if ($type eq 'SCALAR') {
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);
250 elsif ($type eq 'ARRAY') {
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);
256 elsif ($type eq 'HASH') {
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);
262 elsif ($type eq 'CODE') {
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);
268 elsif ($type eq 'IO') {
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);
275 confess "This should never ever ever happen";
278 $self->remove_glob($name);
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;
287 sub list_all_symbols {
288 my ($self, $type_filter) = @_;
290 my $namespace = $self->namespace;
291 return keys %{$namespace} unless defined $type_filter;
294 # or we can filter based on
295 # type (SCALAR|ARRAY|HASH|CODE)
296 if ($type_filter eq 'CODE') {
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
301 || defined(*{$namespace->{$_}}{CODE})
302 } keys %{$namespace};
304 elsif ($type_filter eq 'SCALAR') {
306 ref(\$namespace->{$_}) eq 'GLOB'
307 && defined(${*{$namespace->{$_}}{'SCALAR'}})
308 } keys %{$namespace};
312 ref(\$namespace->{$_}) eq 'GLOB'
313 && defined(*{$namespace->{$_}}{$type_filter})
314 } keys %{$namespace};
318 sub get_all_symbols {
319 my ($self, $type_filter) = @_;
321 my $namespace = $self->namespace;
322 return { %{$namespace} } unless defined $type_filter;
325 map { $_ => $self->get_symbol({name => $_, type => $type_filter}) }
326 $self->list_all_symbols($type_filter)
334 =item * Scalar slots are only considered to exist if they are defined
336 This is due to a shortcoming within perl itself. See
337 L<perlref/Making References> point 7 for more information.
339 =item * remove_symbol also replaces the associated typeglob
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
348 =item * Some minor memory leaks
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.
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>.
364 =item * L<Class::MOP::Package>
366 This module is a factoring out of code that used to live here
372 You can find this documentation for this module with the perldoc command.
374 perldoc Package::Stash
376 You can also look for information at:
380 =item * AnnoCPAN: Annotated CPAN documentation
382 L<http://annocpan.org/dist/Package-Stash>
386 L<http://cpanratings.perl.org/d/Package-Stash>
388 =item * RT: CPAN's request tracker
390 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
394 L<http://search.cpan.org/dist/Package-Stash>
400 Jesse Luehrs <doy at tozt dot net>
402 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
407 BROKEN_ISA_ASSIGNMENT