1 package Package::Stash::PP;
4 # ABSTRACT: pure perl implementation of the Package::Stash API
8 use Scalar::Util qw(blessed reftype weaken);
10 # before 5.12, assigning to the ISA glob would make it lose its magical ->isa
12 use constant BROKEN_ISA_ASSIGNMENT => ($] < 5.012);
13 # before 5.10, stashes don't ever seem to drop to a refcount of zero, so
14 # weakening them isn't helpful
15 use constant BROKEN_WEAK_STASH => ($] < 5.010);
16 # before 5.10, the scalar slot was always treated as existing if the
18 use constant BROKEN_SCALAR_INITIALIZATION => ($] < 5.010);
26 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.
34 if (!defined($package) || (ref($package) && ref($package) ne 'HASH')) {
35 confess "Package::Stash->new must be passed the name of the "
36 . "package to access";
38 elsif (ref($package) eq 'HASH') {
39 confess "The pure perl implementation of Package::Stash doesn't "
40 . "currently support anonymous stashes. You should install "
41 . "Package::Stash::XS";
43 elsif ($package !~ /[0-9A-Z_a-z]+(?:::[0-9A-Z_a-z]+)*/) {
44 confess "$package is not a module name";
48 'package' => $package,
53 confess "Can't call name as a class method"
54 unless blessed($_[0]);
55 return $_[0]->{package};
59 confess "Can't call namespace as a class method"
60 unless blessed($_[0]);
62 if (BROKEN_WEAK_STASH) {
64 return \%{$_[0]->name . '::'};
67 return $_[0]->{namespace} if defined $_[0]->{namespace};
71 $_[0]->{namespace} = \%{$_[0]->name . '::'};
74 weaken($_[0]->{namespace});
76 return $_[0]->{namespace};
89 sub _deconstruct_variable_name {
90 my ($self, $variable) = @_;
92 (defined $variable && length $variable)
93 || confess "You must pass a variable name";
95 my $sigil = substr($variable, 0, 1, '');
97 if (exists $SIGIL_MAP{$sigil}) {
98 return ($variable, $sigil, $SIGIL_MAP{$sigil});
101 return ("${sigil}${variable}", '', $SIGIL_MAP{''});
106 sub _valid_for_type {
108 my ($value, $type) = @_;
109 if ($type eq 'HASH' || $type eq 'ARRAY'
110 || $type eq 'IO' || $type eq 'CODE') {
111 return reftype($value) eq $type;
114 my $ref = reftype($value);
115 return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE' || $ref eq 'REGEXP' || $ref eq 'VSTRING';
120 my ($self, $variable, $initial_value, %opts) = @_;
122 my ($name, $sigil, $type) = ref $variable eq 'HASH'
123 ? @{$variable}{qw[name sigil type]}
124 : $self->_deconstruct_variable_name($variable);
126 my $pkg = $self->name;
129 $self->_valid_for_type($initial_value, $type)
130 || confess "$initial_value is not of type $type";
132 # cheap fail-fast check for PERLDBf_SUBLINE and '&'
133 if ($^P and $^P & 0x10 && $sigil eq '&') {
134 my $filename = $opts{filename};
135 my $first_line_num = $opts{first_line_num};
137 (undef, $filename, $first_line_num) = caller
138 if not defined $filename;
140 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
142 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
143 $DB::sub{$pkg . '::' . $name} = "$filename:$first_line_num-$last_line_num";
148 no warnings 'redefine', 'misc', 'prototype';
149 *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
153 my ($self, $name) = @_;
154 delete $self->namespace->{$name};
158 my ($self, $variable) = @_;
160 my ($name, $sigil, $type) = ref $variable eq 'HASH'
161 ? @{$variable}{qw[name sigil type]}
162 : $self->_deconstruct_variable_name($variable);
164 my $namespace = $self->namespace;
166 return unless exists $namespace->{$name};
168 my $entry_ref = \$namespace->{$name};
169 if (reftype($entry_ref) eq 'GLOB') {
170 if ($type eq 'SCALAR') {
171 if (BROKEN_SCALAR_INITIALIZATION) {
172 return defined ${ *{$entry_ref}{$type} };
175 return B::svref_2object($entry_ref)->SV->isa('B::SV');
179 return defined *{$entry_ref}{$type};
183 # a symbol table entry can be -1 (stub), string (stub with prototype),
184 # or reference (constant)
185 return $type eq 'CODE';
190 my ($self, $variable, %opts) = @_;
192 my ($name, $sigil, $type) = ref $variable eq 'HASH'
193 ? @{$variable}{qw[name sigil type]}
194 : $self->_deconstruct_variable_name($variable);
196 my $namespace = $self->namespace;
198 if (!exists $namespace->{$name}) {
200 if ($type eq 'ARRAY') {
201 if (BROKEN_ISA_ASSIGNMENT) {
204 $name eq 'ISA' ? () : ([])
208 $self->add_symbol($variable, []);
211 elsif ($type eq 'HASH') {
212 $self->add_symbol($variable, {});
214 elsif ($type eq 'SCALAR') {
215 $self->add_symbol($variable);
217 elsif ($type eq 'IO') {
218 $self->add_symbol($variable, Symbol::geniosym);
220 elsif ($type eq 'CODE') {
221 confess "Don't know how to vivify CODE variables";
224 confess "Unknown type $type in vivication";
232 my $entry_ref = \$namespace->{$name};
234 if (ref($entry_ref) eq 'GLOB') {
235 return *{$entry_ref}{$type};
238 if ($type eq 'CODE') {
240 return \&{ $self->name . '::' . $name };
248 sub get_or_add_symbol {
250 $self->get_symbol(@_, vivify => 1);
254 my ($self, $variable) = @_;
256 my ($name, $sigil, $type) = ref $variable eq 'HASH'
257 ? @{$variable}{qw[name sigil type]}
258 : $self->_deconstruct_variable_name($variable);
261 # no doubt this is grossly inefficient and
262 # could be done much easier and faster in XS
264 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
265 { sigil => '$', type => 'SCALAR', name => $name },
266 { sigil => '@', type => 'ARRAY', name => $name },
267 { sigil => '%', type => 'HASH', name => $name },
268 { sigil => '&', type => 'CODE', name => $name },
269 { sigil => '', type => 'IO', name => $name },
272 my ($scalar, $array, $hash, $code, $io);
273 if ($type eq 'SCALAR') {
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);
277 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
279 elsif ($type eq 'ARRAY') {
280 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
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);
283 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
285 elsif ($type eq 'HASH') {
286 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
287 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
288 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
289 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
291 elsif ($type eq 'CODE') {
292 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
293 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
294 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
295 $io = $self->get_symbol($io_desc) if $self->has_symbol($io_desc);
297 elsif ($type eq 'IO') {
298 $scalar = $self->get_symbol($scalar_desc) if $self->has_symbol($scalar_desc) || BROKEN_SCALAR_INITIALIZATION;
299 $array = $self->get_symbol($array_desc) if $self->has_symbol($array_desc);
300 $hash = $self->get_symbol($hash_desc) if $self->has_symbol($hash_desc);
301 $code = $self->get_symbol($code_desc) if $self->has_symbol($code_desc);
304 confess "This should never ever ever happen";
307 $self->remove_glob($name);
309 $self->add_symbol($scalar_desc => $scalar) if defined $scalar;
310 $self->add_symbol($array_desc => $array) if defined $array;
311 $self->add_symbol($hash_desc => $hash) if defined $hash;
312 $self->add_symbol($code_desc => $code) if defined $code;
313 $self->add_symbol($io_desc => $io) if defined $io;
316 sub list_all_symbols {
317 my ($self, $type_filter) = @_;
319 my $namespace = $self->namespace;
320 return keys %{$namespace} unless defined $type_filter;
323 # or we can filter based on
324 # type (SCALAR|ARRAY|HASH|CODE)
325 if ($type_filter eq 'CODE') {
327 # any non-typeglob in the symbol table is a constant or stub
328 ref(\$namespace->{$_}) ne 'GLOB'
329 # regular subs are stored in the CODE slot of the typeglob
330 || defined(*{$namespace->{$_}}{CODE})
331 } keys %{$namespace};
333 elsif ($type_filter eq 'SCALAR') {
335 BROKEN_SCALAR_INITIALIZATION
336 ? (ref(\$namespace->{$_}) eq 'GLOB'
337 && defined(${*{$namespace->{$_}}{'SCALAR'}}))
339 my $entry = \$namespace->{$_};
340 ref($entry) eq 'GLOB'
341 && B::svref_2object($entry)->SV->isa('B::SV')
343 } keys %{$namespace};
347 ref(\$namespace->{$_}) eq 'GLOB'
348 && defined(*{$namespace->{$_}}{$type_filter})
349 } keys %{$namespace};
353 sub get_all_symbols {
354 my ($self, $type_filter) = @_;
356 my $namespace = $self->namespace;
357 return { %{$namespace} } unless defined $type_filter;
360 map { $_ => $self->get_symbol({name => $_, type => $type_filter}) }
361 $self->list_all_symbols($type_filter)
369 =item * remove_symbol also replaces the associated typeglob
371 This can cause unexpected behavior when doing manipulation at compile time -
372 removing subroutines will still allow them to be called from within the package
373 as subroutines (although they will not be available as methods). This can be
374 considered a feature in some cases (this is how L<namespace::clean> works, for
375 instance), but should not be relied upon - use C<remove_glob> directly if you
378 =item * Some minor memory leaks
380 The pure perl implementation has a couple minor memory leaks (see the TODO
381 tests in t/20-leaks.t) that I'm having a hard time tracking down - these may be
382 core perl bugs, it's hard to tell.
386 Please report any bugs through RT: email
387 C<bug-package-stash at rt.cpan.org>, or browse to
388 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Package-Stash>.
394 =item * L<Class::MOP::Package>
396 This module is a factoring out of code that used to live here
402 You can find this documentation for this module with the perldoc command.
404 perldoc Package::Stash
406 You can also look for information at:
410 =item * AnnoCPAN: Annotated CPAN documentation
412 L<http://annocpan.org/dist/Package-Stash>
416 L<http://cpanratings.perl.org/d/Package-Stash>
418 =item * RT: CPAN's request tracker
420 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Package-Stash>
424 L<http://search.cpan.org/dist/Package-Stash>
430 Jesse Luehrs <doy at tozt dot net>
432 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
437 BROKEN_ISA_ASSIGNMENT