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
31 =head2 new $package_name
33 Creates a new C<Stash::Manip> object, for the package given as the only
41 return bless { package => $namespace }, $class;
46 Returns the name of the package that this object represents.
51 return $_[0]->{package};
56 Returns the raw stash itself.
62 # because of issues with the Perl API
63 # to the typeglob in some versions, we
64 # need to just always grab a new
65 # reference to the hash here. Ideally
66 # we could just store a ref and it would
67 # Just Work, but oh well :\
69 return \%{$_[0]->name . '::'};
80 sub _deconstruct_variable_name {
81 my ($self, $variable) = @_;
84 || confess "You must pass a variable name";
86 my $sigil = substr($variable, 0, 1, '');
89 || confess "The variable name must include a sigil";
91 (exists $SIGIL_MAP{$sigil})
92 || confess "I do not recognize that sigil '$sigil'";
94 return ($variable, $sigil, $SIGIL_MAP{$sigil});
98 =head2 add_package_symbol $variable $value
100 Adds a new package symbol, for the symbol given as C<$variable>, and optionally
101 gives it an initial value of C<$value>. C<$variable> should be the name of
102 variable including the sigil, so
104 Stash::Manip->new('Foo')->add_package_symbol('%foo')
106 will create C<%Foo::foo>.
110 sub add_package_symbol {
111 my ($self, $variable, $initial_value) = @_;
113 my ($name, $sigil, $type) = ref $variable eq 'HASH'
114 ? @{$variable}{qw[name sigil type]}
115 : $self->_deconstruct_variable_name($variable);
117 my $pkg = $self->name;
120 no warnings 'redefine', 'misc', 'prototype';
121 *{$pkg . '::' . $name} = ref $initial_value ? $initial_value : \$initial_value;
124 =head2 remove_package_glob $name
126 Removes all package variables with the given name, regardless of sigil.
130 sub remove_package_glob {
131 my ($self, $name) = @_;
133 delete ${$self->name . '::'}{$name};
136 # ... these functions deal with stuff on the namespace level
138 =head2 has_package_symbol $variable
140 Returns whether or not the given package variable (including sigil) exists.
144 sub has_package_symbol {
145 my ($self, $variable) = @_;
147 my ($name, $sigil, $type) = ref $variable eq 'HASH'
148 ? @{$variable}{qw[name sigil type]}
149 : $self->_deconstruct_variable_name($variable);
151 my $namespace = $self->namespace;
153 return unless exists $namespace->{$name};
155 my $entry_ref = \$namespace->{$name};
156 if (reftype($entry_ref) eq 'GLOB') {
157 if ( $type eq 'SCALAR' ) {
158 return defined ${ *{$entry_ref}{SCALAR} };
161 return defined *{$entry_ref}{$type};
165 # a symbol table entry can be -1 (stub), string (stub with prototype),
166 # or reference (constant)
167 return $type eq 'CODE';
171 =head2 get_package_symbol $variable
173 Returns the value of the given package variable (including sigil).
177 sub get_package_symbol {
178 my ($self, $variable) = @_;
180 my ($name, $sigil, $type) = ref $variable eq 'HASH'
181 ? @{$variable}{qw[name sigil type]}
182 : $self->_deconstruct_variable_name($variable);
184 my $namespace = $self->namespace;
187 $self->add_package_symbol($variable)
188 unless exists $namespace->{$name};
190 my $entry_ref = \$namespace->{$name};
192 if (ref($entry_ref) eq 'GLOB') {
193 return *{$entry_ref}{$type};
196 if ($type eq 'CODE') {
198 return \&{ $self->name . '::' . $name };
206 =head2 remove_package_symbol $variable
208 Removes the package variable described by C<$variable> (which includes the
209 sigil); other variables with the same name but different sigils will be
214 sub remove_package_symbol {
215 my ($self, $variable) = @_;
217 my ($name, $sigil, $type) = ref $variable eq 'HASH'
218 ? @{$variable}{qw[name sigil type]}
219 : $self->_deconstruct_variable_name($variable);
222 # no doubt this is grossly inefficient and
223 # could be done much easier and faster in XS
225 my ($scalar_desc, $array_desc, $hash_desc, $code_desc, $io_desc) = (
226 { sigil => '$', type => 'SCALAR', name => $name },
227 { sigil => '@', type => 'ARRAY', name => $name },
228 { sigil => '%', type => 'HASH', name => $name },
229 { sigil => '&', type => 'CODE', name => $name },
230 { sigil => '', type => 'IO', name => $name },
233 my ($scalar, $array, $hash, $code, $io);
234 if ($type eq 'SCALAR') {
235 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
236 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
237 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
238 $io = $self->get_package_symbol($io_desc) if $self->has_package_symbol($io_desc);
240 elsif ($type eq 'ARRAY') {
241 $scalar = $self->get_package_symbol($scalar_desc) if $self->has_package_symbol($scalar_desc);
242 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
243 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
244 $io = $self->get_package_symbol($io_desc) if $self->has_package_symbol($io_desc);
246 elsif ($type eq 'HASH') {
247 $scalar = $self->get_package_symbol($scalar_desc) if $self->has_package_symbol($scalar_desc);
248 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
249 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
250 $io = $self->get_package_symbol($io_desc) if $self->has_package_symbol($io_desc);
252 elsif ($type eq 'CODE') {
253 $scalar = $self->get_package_symbol($scalar_desc) if $self->has_package_symbol($scalar_desc);
254 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
255 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
256 $io = $self->get_package_symbol($io_desc) if $self->has_package_symbol($io_desc);
258 elsif ($type eq 'IO') {
259 $scalar = $self->get_package_symbol($scalar_desc) if $self->has_package_symbol($scalar_desc);
260 $array = $self->get_package_symbol($array_desc) if $self->has_package_symbol($array_desc);
261 $hash = $self->get_package_symbol($hash_desc) if $self->has_package_symbol($hash_desc);
262 $code = $self->get_package_symbol($code_desc) if $self->has_package_symbol($code_desc);
265 confess "This should never ever ever happen";
268 $self->remove_package_glob($name);
270 $self->add_package_symbol($scalar_desc => $scalar) if defined $scalar;
271 $self->add_package_symbol($array_desc => $array) if defined $array;
272 $self->add_package_symbol($hash_desc => $hash) if defined $hash;
273 $self->add_package_symbol($code_desc => $code) if defined $code;
274 $self->add_package_symbol($io_desc => $io) if defined $io;
277 =head2 list_all_package_symbols $type_filter
279 Returns a list of package variable names in the package, without sigils. If a
280 C<type_filter> is passed, it is used to select package variables of a given
281 type, where valid types are the slots of a typeglob ('SCALAR', 'CODE', 'HASH',
286 sub list_all_package_symbols {
287 my ($self, $type_filter) = @_;
289 my $namespace = $self->namespace;
290 return keys %{$namespace} unless defined $type_filter;
293 # or we can filter based on
294 # type (SCALAR|ARRAY|HASH|CODE)
295 if ($type_filter eq 'CODE') {
297 (ref($namespace->{$_})
298 ? (ref($namespace->{$_}) eq 'SCALAR')
299 : (ref(\$namespace->{$_}) eq 'GLOB'
300 && defined(*{$namespace->{$_}}{CODE})));
301 } keys %{$namespace};
303 return grep { *{$namespace->{$_}}{$type_filter} } keys %{$namespace};
311 Please report any bugs through RT: email
312 C<bug-stash-manip at rt.cpan.org>, or browse to
313 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Stash-Manip>.
317 L<Class::MOP::Package> - this module is a factoring out of code that used to
322 You can find this documentation for this module with the perldoc command.
326 You can also look for information at:
330 =item * AnnoCPAN: Annotated CPAN documentation
332 L<http://annocpan.org/dist/Stash-Manip>
336 L<http://cpanratings.perl.org/d/Stash-Manip>
338 =item * RT: CPAN's request tracker
340 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Stash-Manip>
344 L<http://search.cpan.org/dist/Stash-Manip>
350 Jesse Luehrs <doy at tozt dot net>
352 Mostly copied from code from L<Class::MOP::Package>, by Stevan Little and the
355 =head1 COPYRIGHT AND LICENSE
357 This software is copyright (c) 2010 by Jesse Luehrs.
359 This is free software; you can redistribute it and/or modify it under
360 the same terms as perl itself.