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);
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.
28 'package' => $package,
33 confess "Can't call name as a class method"
34 unless blessed($_[0]);
35 return $_[0]->{package};
39 confess "Can't call namespace as a class method"
40 unless blessed($_[0]);
41 return $_[0]->{namespace} if defined $_[0]->{namespace};
45 # supposedly this caused a bug in earlier perls, but I can't reproduce
46 # it, so re-enabling the caching
47 $_[0]->{namespace} = \%{$_[0]->name . '::'};
50 weaken($_[0]->{namespace});
52 return $_[0]->{namespace};
64 sub _deconstruct_variable_name {
65 my ($self, $variable) = @_;
67 (defined $variable && length $variable)
68 || confess "You must pass a variable name";
70 my $sigil = substr($variable, 0, 1, '');
72 if (exists $SIGIL_MAP{$sigil}) {
73 return ($variable, $sigil, $SIGIL_MAP{$sigil});
76 return ("${sigil}${variable}", '', $SIGIL_MAP{''});
83 my ($value, $type) = @_;
84 if ($type eq 'HASH' || $type eq 'ARRAY'
85 || $type eq 'IO' || $type eq 'CODE') {
86 return reftype($value) eq $type;
89 my $ref = reftype($value);
90 return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE';
95 my ($self, $variable, $initial_value, %opts) = @_;
97 my ($name, $sigil, $type) = ref $variable eq 'HASH'
98 ? @{$variable}{qw[name sigil type]}
99 : $self->_deconstruct_variable_name($variable);
101 my $pkg = $self->name;
104 $self->_valid_for_type($initial_value, $type)
105 || confess "$initial_value is not of type $type";
107 # cheap fail-fast check for PERLDBf_SUBLINE and '&'
108 if ($^P and $^P & 0x10 && $sigil eq '&') {
109 my $filename = $opts{filename};
110 my $first_line_num = $opts{first_line_num};
112 (undef, $filename, $first_line_num) = caller
113 if not defined $filename;
115 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
117 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
118 $DB::sub{$pkg . '::' . $name} = "$filename:$first_line_num-$last_line_num";
123 no warnings 'redefine', 'misc', 'prototype';
124 *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
128 my ($self, $name) = @_;
130 delete ${$self->name . '::'}{$name};
134 my ($self, $variable) = @_;
136 my ($name, $sigil, $type) = ref $variable eq 'HASH'
137 ? @{$variable}{qw[name sigil type]}
138 : $self->_deconstruct_variable_name($variable);
140 my $namespace = $self->namespace;
142 return unless exists $namespace->{$name};
144 my $entry_ref = \$namespace->{$name};
145 if (reftype($entry_ref) eq 'GLOB') {
146 # XXX: assigning to any typeglob slot also initializes the SCALAR slot,
147 # and saying that an undef scalar variable doesn't exist is probably
148 # vaguely less surprising than a scalar variable popping into existence
149 # without anyone defining it
150 if ($type eq 'SCALAR') {
151 return defined ${ *{$entry_ref}{$type} };
154 return defined *{$entry_ref}{$type};
158 # a symbol table entry can be -1 (stub), string (stub with prototype),
159 # or reference (constant)
160 return $type eq 'CODE';
165 my ($self, $variable, %opts) = @_;
167 my ($name, $sigil, $type) = ref $variable eq 'HASH'
168 ? @{$variable}{qw[name sigil type]}
169 : $self->_deconstruct_variable_name($variable);
171 my $namespace = $self->namespace;
173 if (!exists $namespace->{$name}) {
175 if ($type eq 'ARRAY') {
176 if (BROKEN_ISA_ASSIGNMENT) {
179 $name eq 'ISA' ? () : ([])
183 $self->add_symbol($variable, []);
186 elsif ($type eq 'HASH') {
187 $self->add_symbol($variable, {});
189 elsif ($type eq 'SCALAR') {
190 $self->add_symbol($variable);
192 elsif ($type eq 'IO') {
193 $self->add_symbol($variable, Symbol::geniosym);
195 elsif ($type eq 'CODE') {
196 confess "Don't know how to vivify CODE variables";
199 confess "Unknown type $type in vivication";
207 my $entry_ref = \$namespace->{$name};
209 if (ref($entry_ref) eq 'GLOB') {
210 return *{$entry_ref}{$type};
213 if ($type eq 'CODE') {
215 return \&{ $self->name . '::' . $name };
223 sub get_or_add_symbol {
225 $self->get_symbol(@_, vivify => 1);
229 my ($self, $variable) = @_;
231 my ($name, $sigil, $type) = ref $variable eq 'HASH'
232 ? @{$variable}{qw[name sigil type]}
233 : $self->_deconstruct_variable_name($variable);
236 # no doubt this is grossly inefficient and
237 # could be done much easier and faster in XS
239 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
240 { sigil => '$', type => 'SCALAR', name => $name },
241 { sigil => '@', type => 'ARRAY', name => $name },
242 { sigil => '%', type => 'HASH', name => $name },
243 { sigil => '&', type => 'CODE', name => $name },
244 { sigil => '', type => 'IO', name => $name },
247 my ($scalar, $array, $hash, $code, $io);
248 if ($type eq 'SCALAR') {
249 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
250 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
251 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
252 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
254 elsif ($type eq 'ARRAY') {
255 $scalar = $self->get_symbol($scalar_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 'HASH') {
261 $scalar = $self->get_symbol($scalar_desc);
262 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_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 'CODE') {
267 $scalar = $self->get_symbol($scalar_desc);
268 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
269 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
270 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
272 elsif ($type eq 'IO') {
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 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
279 confess "This should never ever ever happen";
282 $self->remove_glob($name);
284 $self->add_symbol($scalar_desc => $scalar);
285 $self->add_symbol($array_desc => $array) if defined $array;
286 $self->add_symbol($hash_desc => $hash) if defined $hash;
287 $self->add_symbol($code_desc => $code) if defined $code;
288 $self->add_symbol($io_desc => $io) if defined $io;
291 sub list_all_symbols {
292 my ($self, $type_filter) = @_;
294 my $namespace = $self->namespace;
295 return keys %{$namespace} unless defined $type_filter;
298 # or we can filter based on
299 # type (SCALAR|ARRAY|HASH|CODE)
300 if ($type_filter eq 'CODE') {
302 # any non-typeglob in the symbol table is a constant or stub
303 ref(\$namespace->{$_}) ne 'GLOB'
304 # regular subs are stored in the CODE slot of the typeglob
305 || defined(*{$namespace->{$_}}{CODE})
306 } keys %{$namespace};
308 elsif ($type_filter eq 'SCALAR') {
310 ref(\$namespace->{$_}) eq 'GLOB'
311 && defined(${*{$namespace->{$_}}{'SCALAR'}})
312 } keys %{$namespace};
316 ref(\$namespace->{$_}) eq 'GLOB'
317 && defined(*{$namespace->{$_}}{$type_filter})
318 } keys %{$namespace};
322 sub get_all_symbols {
323 my ($self, $type_filter) = @_;
325 my $namespace = $self->namespace;
326 return { %{$namespace} } unless defined $type_filter;
329 map { $_ => $self->get_symbol({name => $_, type => $type_filter}) }
330 $self->list_all_symbols($type_filter)
338 =item * Scalar slots are only considered to exist if they are defined
340 This is due to a shortcoming within perl itself. See
341 L<perlref/Making References> point 7 for more information.
343 =item * remove_symbol also replaces the associated typeglob
345 This can cause unexpected behavior when doing manipulation at compile time -
346 removing subroutines will still allow them to be called from within the package
347 as subroutines (although they will not be available as methods). This can be
348 considered a feature in some cases (this is how L<namespace::clean> works, for
349 instance), but should not be relied upon - use C<remove_glob> directly if you
352 =item * Some minor memory leaks
354 The pure perl implementation has a couple minor memory leaks (see the TODO
355 tests in t/20-leaks.t) that I'm having a hard time tracking down - these may be
356 core perl bugs, it's hard to tell.
360 Please report any bugs through RT: email
361 C<bug-package-stash at rt.cpan.org>, or browse to
362 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
368 =item * L<Class::MOP::Package>
370 This module is a factoring out of code that used to live here
376 You can find this documentation for this module with the perldoc command.
378 perldoc Package::Stash
380 You can also look for information at:
384 =item * AnnoCPAN: Annotated CPAN documentation
386 L<http://annocpan.org/dist/Package-Stash>
390 L<http://cpanratings.perl.org/d/Package-Stash>
392 =item * RT: CPAN's request tracker
394 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
398 L<http://search.cpan.org/dist/Package-Stash>
404 Jesse Luehrs <doy at tozt dot net>
406 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
411 BROKEN_ISA_ASSIGNMENT