6 use Scalar::Util qw(reftype);
10 Stash::Manip - routines for manipulating stashes
14 my $stash = Stash::Manip->new('Foo');
15 $stash->add_package_symbol('%foo', {bar => 1});
17 $stash->has_package_symbol('$foo') # false
18 my $namespace = $stash->namespace;
19 *{ $namespace->{foo} }{HASH} # {bar => 1}
23 Manipulating stashes (Perl's symbol tables) is occasionally necessary, but
24 incredibly messy, and easy to get wrong. This module hides all of that behind a
27 NOTE: Most methods in this class require a variable specification that includes
28 a sigil. If this sigil is absent, it is assumed to represent the IO slot.
34 =head2 new $package_name
36 Creates a new C<Stash::Manip> object, for the package given as the only
44 return bless { package => $namespace }, $class;
49 Returns the name of the package that this object represents.
54 return $_[0]->{package};
59 Returns the raw stash itself.
65 # because of issues with the Perl API
66 # to the typeglob in some versions, we
67 # need to just always grab a new
68 # reference to the hash here. Ideally
69 # we could just store a ref and it would
70 # Just Work, but oh well :\
72 return \%{$_[0]->name . '::'};
84 sub _deconstruct_variable_name {
85 my ($self, $variable) = @_;
87 (defined $variable && length $variable)
88 || confess "You must pass a variable name";
90 my $sigil = substr($variable, 0, 1, '');
92 if (exists $SIGIL_MAP{$sigil}) {
93 return ($variable, $sigil, $SIGIL_MAP{$sigil});
96 return ("${sigil}${variable}", '', $SIGIL_MAP{''});
101 =head2 add_package_symbol $variable $value
103 Adds a new package symbol, for the symbol given as C<$variable>, and optionally
104 gives it an initial value of C<$value>. C<$variable> should be the name of
105 variable including the sigil, so
107 Stash::Manip->new('Foo')->add_package_symbol('%foo')
109 will create C<%Foo::foo>.
113 sub _valid_for_type {
115 my ($value, $type) = @_;
116 if ($type eq 'HASH' || $type eq 'ARRAY'
117 || $type eq 'IO' || $type eq 'CODE') {
118 return reftype($value) eq $type;
121 my $ref = reftype($value);
122 return !defined($ref) || $ref eq 'SCALAR' || $ref eq 'REF' || $ref eq 'LVALUE';
126 sub add_package_symbol {
127 my ($self, $variable, $initial_value) = @_;
129 my ($name, $sigil, $type) = ref $variable eq 'HASH'
130 ? @{$variable}{qw[name sigil type]}
131 : $self->_deconstruct_variable_name($variable);
134 $self->_valid_for_type($initial_value, $type)
135 || confess "$initial_value is not of type $type";
138 my $pkg = $self->name;
141 no warnings 'redefine', 'misc', 'prototype';
142 *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
145 =head2 remove_package_glob $name
147 Removes all package variables with the given name, regardless of sigil.
151 sub remove_package_glob {
152 my ($self, $name) = @_;
154 delete ${$self->name . '::'}{$name};
157 # ... these functions deal with stuff on the namespace level
159 =head2 has_package_symbol $variable
161 Returns whether or not the given package variable (including sigil) exists.
165 sub has_package_symbol {
166 my ($self, $variable) = @_;
168 my ($name, $sigil, $type) = ref $variable eq 'HASH'
169 ? @{$variable}{qw[name sigil type]}
170 : $self->_deconstruct_variable_name($variable);
172 my $namespace = $self->namespace;
174 return unless exists $namespace->{$name};
176 my $entry_ref = \$namespace->{$name};
177 if (reftype($entry_ref) eq 'GLOB') {
178 if ( $type eq 'SCALAR' ) {
179 return defined ${ *{$entry_ref}{SCALAR} };
182 return defined *{$entry_ref}{$type};
186 # a symbol table entry can be -1 (stub), string (stub with prototype),
187 # or reference (constant)
188 return $type eq 'CODE';
192 =head2 get_package_symbol $variable
194 Returns the value of the given package variable (including sigil).
198 sub get_package_symbol {
199 my ($self, $variable) = @_;
201 my ($name, $sigil, $type) = ref $variable eq 'HASH'
202 ? @{$variable}{qw[name sigil type]}
203 : $self->_deconstruct_variable_name($variable);
205 my $namespace = $self->namespace;
207 if (!exists $namespace->{$name}) {
208 if ($type eq 'ARRAY') {
209 $self->add_package_symbol($variable, []);
211 elsif ($type eq 'HASH') {
212 $self->add_package_symbol($variable, {});
216 $self->add_package_symbol($variable)
220 my $entry_ref = \$namespace->{$name};
222 if (ref($entry_ref) eq 'GLOB') {
223 return *{$entry_ref}{$type};
226 if ($type eq 'CODE') {
228 return \&{ $self->name . '::' . $name };
236 =head2 remove_package_symbol $variable
238 Removes the package variable described by C<$variable> (which includes the
239 sigil); other variables with the same name but different sigils will be
244 sub remove_package_symbol {
245 my ($self, $variable) = @_;
247 my ($name, $sigil, $type) = ref $variable eq 'HASH'
248 ? @{$variable}{qw[name sigil type]}
249 : $self->_deconstruct_variable_name($variable);
252 # no doubt this is grossly inefficient and
253 # could be done much easier and faster in XS
255 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
256 { sigil => '$', type => 'SCALAR', name => $name },
257 { sigil => '@', type => 'ARRAY', name => $name },
258 { sigil => '%', type => 'HASH', name => $name },
259 { sigil => '&', type => 'CODE', name => $name },
260 { sigil => '', type => 'IO', name => $name },
263 my ($scalar, $array, $hash, $code, $io);
264 if ($type eq 'SCALAR') {
265 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
266 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
267 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
268 $io = $self->get_package_symbol($io_desc) if $self->has_package_symbol($io_desc);
270 elsif ($type eq 'ARRAY') {
271 $scalar = $self->get_package_symbol($scalar_desc);
272 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
273 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
274 $io = $self->get_package_symbol($io_desc) if $self->has_package_symbol($io_desc);
276 elsif ($type eq 'HASH') {
277 $scalar = $self->get_package_symbol($scalar_desc);
278 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
279 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
280 $io = $self->get_package_symbol($io_desc) if $self->has_package_symbol($io_desc);
282 elsif ($type eq 'CODE') {
283 $scalar = $self->get_package_symbol($scalar_desc);
284 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
285 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
286 $io = $self->get_package_symbol($io_desc) if $self->has_package_symbol($io_desc);
288 elsif ($type eq 'IO') {
289 $scalar = $self->get_package_symbol($scalar_desc);
290 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
291 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
292 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
295 confess "This should never ever ever happen";
298 $self->remove_package_glob($name);
300 $self->add_package_symbol($scalar_desc => $scalar);
301 $self->add_package_symbol($array_desc => $array) if defined $array;
302 $self->add_package_symbol($hash_desc => $hash) if defined $hash;
303 $self->add_package_symbol($code_desc => $code) if defined $code;
304 $self->add_package_symbol($io_desc => $io) if defined $io;
307 =head2 list_all_package_symbols $type_filter
309 Returns a list of package variable names in the package, without sigils. If a
310 C<type_filter> is passed, it is used to select package variables of a given
311 type, where valid types are the slots of a typeglob ('SCALAR', 'CODE', 'HASH',
316 sub list_all_package_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 (ref($namespace->{$_})
328 ? (ref($namespace->{$_}) eq 'SCALAR')
329 : (ref(\$namespace->{$_}) eq 'GLOB'
330 && defined(*{$namespace->{$_}}{CODE})));
331 } keys %{$namespace};
333 return grep { *{$namespace->{$_}}{$type_filter} } keys %{$namespace};
341 Please report any bugs through RT: email
342 C<bug-stash-manip at rt.cpan.org>, or browse to
343 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Stash-Manip>.
347 L<Class::MOP::Package> - this module is a factoring out of code that used to
352 You can find this documentation for this module with the perldoc command.
356 You can also look for information at:
360 =item * AnnoCPAN: Annotated CPAN documentation
362 L<http://annocpan.org/dist/Stash-Manip>
366 L<http://cpanratings.perl.org/d/Stash-Manip>
368 =item * RT: CPAN's request tracker
370 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Stash-Manip>
374 L<http://search.cpan.org/dist/Stash-Manip>
380 Jesse Luehrs <doy at tozt dot net>
382 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
385 =head1 COPYRIGHT AND LICENSE
387 This software is copyright (c) 2010 by Jesse Luehrs.
389 This is free software; you can redistribute it and/or modify it under
390 the same terms as perl itself.