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.
30 'package' => $package,
35 confess "Can't call name as a class method"
36 unless blessed($_[0]);
37 return $_[0]->{package};
41 confess "Can't call namespace as a class method"
42 unless blessed($_[0]);
44 if (BROKEN_WEAK_STASH) {
46 return \%{$_[0]->name . '::'};
49 return $_[0]->{namespace} if defined $_[0]->{namespace};
53 $_[0]->{namespace} = \%{$_[0]->name . '::'};
56 weaken($_[0]->{namespace});
58 return $_[0]->{namespace};
71 sub _deconstruct_variable_name {
72 my ($self, $variable) = @_;
74 (defined $variable && length $variable)
75 || confess "You must pass a variable name";
77 my $sigil = substr($variable, 0, 1, '');
79 if (exists $SIGIL_MAP{$sigil}) {
80 return ($variable, $sigil, $SIGIL_MAP{$sigil});
83 return ("${sigil}${variable}", '', $SIGIL_MAP{''});
90 my ($value, $type) = @_;
91 if ($type eq 'HASH' || $type eq 'ARRAY'
92 || $type eq 'IO' || $type eq 'CODE') {
93 return reftype($value) eq $type;
96 my $ref = reftype($value);
97 return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE';
102 my ($self, $variable, $initial_value, %opts) = @_;
104 my ($name, $sigil, $type) = ref $variable eq 'HASH'
105 ? @{$variable}{qw[name sigil type]}
106 : $self->_deconstruct_variable_name($variable);
108 my $pkg = $self->name;
111 $self->_valid_for_type($initial_value, $type)
112 || confess "$initial_value is not of type $type";
114 # cheap fail-fast check for PERLDBf_SUBLINE and '&'
115 if ($^P and $^P & 0x10 && $sigil eq '&') {
116 my $filename = $opts{filename};
117 my $first_line_num = $opts{first_line_num};
119 (undef, $filename, $first_line_num) = caller
120 if not defined $filename;
122 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
124 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
125 $DB::sub{$pkg . '::' . $name} = "$filename:$first_line_num-$last_line_num";
130 no warnings 'redefine', 'misc', 'prototype';
131 *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
135 my ($self, $name) = @_;
136 delete $self->namespace->{$name};
140 my ($self, $variable) = @_;
142 my ($name, $sigil, $type) = ref $variable eq 'HASH'
143 ? @{$variable}{qw[name sigil type]}
144 : $self->_deconstruct_variable_name($variable);
146 my $namespace = $self->namespace;
148 return unless exists $namespace->{$name};
150 my $entry_ref = \$namespace->{$name};
151 if (reftype($entry_ref) eq 'GLOB') {
152 # XXX: assigning to any typeglob slot also initializes the SCALAR slot,
153 # and saying that an undef scalar variable doesn't exist is probably
154 # vaguely less surprising than a scalar variable popping into existence
155 # without anyone defining it
156 if ($type eq 'SCALAR') {
157 return defined ${ *{$entry_ref}{$type} };
160 return defined *{$entry_ref}{$type};
164 # a symbol table entry can be -1 (stub), string (stub with prototype),
165 # or reference (constant)
166 return $type eq 'CODE';
171 my ($self, $variable, %opts) = @_;
173 my ($name, $sigil, $type) = ref $variable eq 'HASH'
174 ? @{$variable}{qw[name sigil type]}
175 : $self->_deconstruct_variable_name($variable);
177 my $namespace = $self->namespace;
179 if (!exists $namespace->{$name}) {
181 if ($type eq 'ARRAY') {
182 if (BROKEN_ISA_ASSIGNMENT) {
185 $name eq 'ISA' ? () : ([])
189 $self->add_symbol($variable, []);
192 elsif ($type eq 'HASH') {
193 $self->add_symbol($variable, {});
195 elsif ($type eq 'SCALAR') {
196 $self->add_symbol($variable);
198 elsif ($type eq 'IO') {
199 $self->add_symbol($variable, Symbol::geniosym);
201 elsif ($type eq 'CODE') {
202 confess "Don't know how to vivify CODE variables";
205 confess "Unknown type $type in vivication";
213 my $entry_ref = \$namespace->{$name};
215 if (ref($entry_ref) eq 'GLOB') {
216 return *{$entry_ref}{$type};
219 if ($type eq 'CODE') {
221 return \&{ $self->name . '::' . $name };
229 sub get_or_add_symbol {
231 $self->get_symbol(@_, vivify => 1);
235 my ($self, $variable) = @_;
237 my ($name, $sigil, $type) = ref $variable eq 'HASH'
238 ? @{$variable}{qw[name sigil type]}
239 : $self->_deconstruct_variable_name($variable);
242 # no doubt this is grossly inefficient and
243 # could be done much easier and faster in XS
245 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
246 { sigil => '$', type => 'SCALAR', name => $name },
247 { sigil => '@', type => 'ARRAY', name => $name },
248 { sigil => '%', type => 'HASH', name => $name },
249 { sigil => '&', type => 'CODE', name => $name },
250 { sigil => '', type => 'IO', name => $name },
253 my ($scalar, $array, $hash, $code, $io);
254 if ($type eq 'SCALAR') {
255 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
256 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
257 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
258 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
260 elsif ($type eq 'ARRAY') {
261 $scalar = $self->get_symbol($scalar_desc);
262 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
263 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
264 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
266 elsif ($type eq 'HASH') {
267 $scalar = $self->get_symbol($scalar_desc);
268 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
269 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
270 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
272 elsif ($type eq 'CODE') {
273 $scalar = $self->get_symbol($scalar_desc);
274 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
275 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
276 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
278 elsif ($type eq 'IO') {
279 $scalar = $self->get_symbol($scalar_desc);
280 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
281 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
282 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
285 confess "This should never ever ever happen";
288 $self->remove_glob($name);
290 $self->add_symbol($scalar_desc => $scalar);
291 $self->add_symbol($array_desc => $array) if defined $array;
292 $self->add_symbol($hash_desc => $hash) if defined $hash;
293 $self->add_symbol($code_desc => $code) if defined $code;
294 $self->add_symbol($io_desc => $io) if defined $io;
297 sub list_all_symbols {
298 my ($self, $type_filter) = @_;
300 my $namespace = $self->namespace;
301 return keys %{$namespace} unless defined $type_filter;
304 # or we can filter based on
305 # type (SCALAR|ARRAY|HASH|CODE)
306 if ($type_filter eq 'CODE') {
308 # any non-typeglob in the symbol table is a constant or stub
309 ref(\$namespace->{$_}) ne 'GLOB'
310 # regular subs are stored in the CODE slot of the typeglob
311 || defined(*{$namespace->{$_}}{CODE})
312 } keys %{$namespace};
314 elsif ($type_filter eq 'SCALAR') {
316 ref(\$namespace->{$_}) eq 'GLOB'
317 && defined(${*{$namespace->{$_}}{'SCALAR'}})
318 } keys %{$namespace};
322 ref(\$namespace->{$_}) eq 'GLOB'
323 && defined(*{$namespace->{$_}}{$type_filter})
324 } keys %{$namespace};
328 sub get_all_symbols {
329 my ($self, $type_filter) = @_;
331 my $namespace = $self->namespace;
332 return { %{$namespace} } unless defined $type_filter;
335 map { $_ => $self->get_symbol({name => $_, type => $type_filter}) }
336 $self->list_all_symbols($type_filter)
344 =item * Scalar slots are only considered to exist if they are defined
346 This is due to a shortcoming within perl itself. See
347 L<perlref/Making References> point 7 for more information.
349 =item * remove_symbol also replaces the associated typeglob
351 This can cause unexpected behavior when doing manipulation at compile time -
352 removing subroutines will still allow them to be called from within the package
353 as subroutines (although they will not be available as methods). This can be
354 considered a feature in some cases (this is how L<namespace::clean> works, for
355 instance), but should not be relied upon - use C<remove_glob> directly if you
358 =item * Some minor memory leaks
360 The pure perl implementation has a couple minor memory leaks (see the TODO
361 tests in t/20-leaks.t) that I'm having a hard time tracking down - these may be
362 core perl bugs, it's hard to tell.
366 Please report any bugs through RT: email
367 C<bug-package-stash at rt.cpan.org>, or browse to
368 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
374 =item * L<Class::MOP::Package>
376 This module is a factoring out of code that used to live here
382 You can find this documentation for this module with the perldoc command.
384 perldoc Package::Stash
386 You can also look for information at:
390 =item * AnnoCPAN: Annotated CPAN documentation
392 L<http://annocpan.org/dist/Package-Stash>
396 L<http://cpanratings.perl.org/d/Package-Stash>
398 =item * RT: CPAN's request tracker
400 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
404 L<http://search.cpan.org/dist/Package-Stash>
410 Jesse Luehrs <doy at tozt dot net>
412 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
417 BROKEN_ISA_ASSIGNMENT