1 package namespace::clean;
5 namespace::clean - Keep imports out of your namespace
12 use vars qw( $VERSION $STORAGE_VAR );
13 use Symbol qw( qualify_to_ref );
23 $STORAGE_VAR = '__NAMESPACE_CLEAN_STORAGE';
31 use Carp qw(croak); # will be removed
33 sub bar { 23 } # will be removed
37 sub baz { bar() } # still defined, 'bar' still bound
41 sub quux { baz() } # will be removed again
50 print +(__PACKAGE__->can('croak') ? 'Yes' : 'No'), "\n";
51 print +(__PACKAGE__->can('bar') ? 'Yes' : 'No'), "\n";
52 print +(__PACKAGE__->can('baz') ? 'Yes' : 'No'), "\n";
53 print +(__PACKAGE__->can('quux') ? 'Yes' : 'No'), "\n";
59 When you define a function, or import one, into a Perl package, it will
60 naturally also be available as a method. This does not per se cause
61 problems, but it can complicate subclassing and, for example, plugin
62 classes that are included by loading them as base classes.
64 The C<namespace::clean> pragma will remove all previously declared or
65 imported symbols at the end of the current package's compile cycle.
66 This means that functions are already bound by their name, and calls to
67 them still work. But they will not be available as methods on your class
72 You shouldn't need to call any of these. Just C<use> the package at the
77 Makes a snapshot of the current defined functions and registers a
78 L<Filter::EOF> cleanup routine to remove those symbols from the package
79 at the end of the compile-time.
87 my $functions = $pragma->get_functions($cleanee);
88 my $store = $pragma->get_class_store($cleanee);
90 for my $f (keys %$functions) {
91 next unless $functions->{ $f }
92 and *{ $functions->{ $f } }{CODE};
93 $store->{remove}{ $f } = 1;
96 unless ($store->{handler_is_installed}) {
97 Filter::EOF->on_eof_call(sub {
98 for my $f (keys %{ $store->{remove} }) {
99 next if $store->{exclude}{ $f };
101 delete ${ "${cleanee}::" }{ $f };
104 $store->{handler_is_installed} = 1;
112 This method will be called when you do a
116 It will start a new section of code that defines functions to clean up.
123 my $cleanee = caller;
124 my $functions = $pragma->get_functions($cleanee);
125 my $store = $pragma->get_class_store($cleanee);
127 for my $f (keys %$functions) {
128 next if $store->{remove}{ $f }
129 or $store->{exclude}{ $f };
130 $store->{exclude}{ $f } = 1;
136 =head2 get_class_store
138 This returns a reference to a hash in your package containing information
139 about function names included and excluded from removal.
143 sub get_class_store {
144 my ($pragma, $class) = @_;
146 return \%{ "${class}::${STORAGE_VAR}" };
151 Takes a class as argument and returns all currently defined functions
152 in it as a hash reference with the function name as key and a typeglob
153 reference to the symbol as value.
158 my ($pragma, $class) = @_;
162 grep { *{ $_->[1] }{CODE} }
163 map { [$_, qualify_to_ref( $_, $class )] }
165 do { no strict 'refs'; keys %{ "${class}::" } }
173 =head1 AUTHOR AND COPYRIGHT
175 Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
176 Matt S Trout for the inspiration on the whole idea.
180 This program is free software; you can redistribute it and/or modify
181 it under the same terms as perl itself.