Import namespace-clean-0.06.tar.gz.
[p5sagit/namespace-clean.git] / lib / namespace / clean.pm
CommitLineData
40aef9d6 1package namespace::clean;
2
3=head1 NAME
4
6c0ece9b 5namespace::clean - Keep imports and functions out of your namespace
40aef9d6 6
7=cut
8
9use warnings;
10use strict;
11
9b680ffe 12use vars qw( $VERSION $STORAGE_VAR );
40aef9d6 13use Symbol qw( qualify_to_ref );
14use Filter::EOF;
15
16=head1 VERSION
17
99d2e6b6 180.06
40aef9d6 19
20=cut
21
99d2e6b6 22$VERSION = 0.06;
9b680ffe 23$STORAGE_VAR = '__NAMESPACE_CLEAN_STORAGE';
40aef9d6 24
25=head1 SYNOPSIS
26
27 package Foo;
28 use warnings;
29 use strict;
30
6c0ece9b 31 use Carp qw(croak); # 'croak' will be removed
40aef9d6 32
6c0ece9b 33 sub bar { 23 } # 'bar' will be removed
40aef9d6 34
6c0ece9b 35 # remove all previously defined functions
40aef9d6 36 use namespace::clean;
37
6c0ece9b 38 sub baz { bar() } # 'baz' still defined, 'bar' still bound
40aef9d6 39
6c0ece9b 40 # begin to collection function names from here again
9b680ffe 41 no namespace::clean;
42
6c0ece9b 43 sub quux { baz() } # 'quux' will be removed
9b680ffe 44
6c0ece9b 45 # remove all functions defined after the 'no' unimport
9b680ffe 46 use namespace::clean;
47
6c0ece9b 48 # Will print: 'No', 'No', 'Yes' and 'No'
40aef9d6 49 print +(__PACKAGE__->can('croak') ? 'Yes' : 'No'), "\n";
50 print +(__PACKAGE__->can('bar') ? 'Yes' : 'No'), "\n";
51 print +(__PACKAGE__->can('baz') ? 'Yes' : 'No'), "\n";
9b680ffe 52 print +(__PACKAGE__->can('quux') ? 'Yes' : 'No'), "\n";
40aef9d6 53
54 1;
55
56=head1 DESCRIPTION
57
58When you define a function, or import one, into a Perl package, it will
59naturally also be available as a method. This does not per se cause
60problems, but it can complicate subclassing and, for example, plugin
6c0ece9b 61classes that are included via multiple inheritance by loading them as
62base classes.
40aef9d6 63
64The C<namespace::clean> pragma will remove all previously declared or
65imported symbols at the end of the current package's compile cycle.
6c0ece9b 66Functions called in the package itself will still be bound by their
67name, but they won't show up as methods on your class or instances.
68
69By unimporting via C<no> you can tell C<namespace::clean> to start
70collecting functions for the next C<use namespace::clean;> specification.
40aef9d6 71
53e92ec5 72You can use the C<-except> flag to tell C<namespace::clean> that you
472d4b1e 73don't want it to remove a certain function or method. A common use would
74be a module exporting an C<import> method along with some functions:
53e92ec5 75
76 use ModuleExportingImport;
77 use namespace::clean -except => [qw( import )];
78
472d4b1e 79If you just want to C<-except> a single sub, you can pass it directly.
80For more than one value you have to use an array reference.
81
40aef9d6 82=head1 METHODS
83
84You shouldn't need to call any of these. Just C<use> the package at the
85appropriate place.
86
6c0ece9b 87=cut
88
40aef9d6 89=head2 import
90
91Makes a snapshot of the current defined functions and registers a
6c0ece9b 92L<Filter::EOF> cleanup routine to remove those symbols at the end
93of the compile-time.
40aef9d6 94
95=cut
96
97sub import {
53e92ec5 98 my ($pragma, %args) = @_;
40aef9d6 99
6c0ece9b 100 # calling class, all current functions and our storage
40aef9d6 101 my $cleanee = caller;
102 my $functions = $pragma->get_functions($cleanee);
9b680ffe 103 my $store = $pragma->get_class_store($cleanee);
53e92ec5 104
472d4b1e 105 # except parameter can be array ref or single value
106 my %except = map {( $_ => 1 )} (
107 $args{ -except }
108 ? ( ref $args{ -except } eq 'ARRAY' ? @{ $args{ -except } } : $args{ -except } )
109 : ()
110 );
53e92ec5 111
6c0ece9b 112 # register symbols for removal, if they have a CODE entry
9b680ffe 113 for my $f (keys %$functions) {
53e92ec5 114 next if $except{ $f };
9b680ffe 115 next unless $functions->{ $f }
116 and *{ $functions->{ $f } }{CODE};
117 $store->{remove}{ $f } = 1;
118 }
119
6c0ece9b 120 # register EOF handler on first call to import
9b680ffe 121 unless ($store->{handler_is_installed}) {
122 Filter::EOF->on_eof_call(sub {
53e92ec5 123 SYMBOL:
9b680ffe 124 for my $f (keys %{ $store->{remove} }) {
53e92ec5 125
126 # ignore already removed symbols
127 next SYMBOL if $store->{exclude}{ $f };
9b680ffe 128 no strict 'refs';
53e92ec5 129
130 # keep original value to restore non-code slots
99d2e6b6 131 { no warnings 'uninitialized'; # fix possible unimports
132 local *__tmp = *{ ${ "${cleanee}::" }{ $f } };
133 delete ${ "${cleanee}::" }{ $f };
134 }
53e92ec5 135
136 SLOT:
137 # restore non-code slots to symbol
138 for my $t (qw( SCALAR ARRAY HASH IO FORMAT )) {
139 next SLOT unless defined *__tmp{ $t };
140 *{ "${cleanee}::$f" } = *__tmp{ $t };
141 }
40aef9d6 142 }
9b680ffe 143 });
144 $store->{handler_is_installed} = 1;
145 }
146
147 return 1;
148}
149
150=head2 unimport
151
152This method will be called when you do a
153
154 no namespace::clean;
155
156It will start a new section of code that defines functions to clean up.
157
158=cut
159
160sub unimport {
161 my ($pragma) = @_;
162
6c0ece9b 163 # the calling class, the current functions and our storage
9b680ffe 164 my $cleanee = caller;
165 my $functions = $pragma->get_functions($cleanee);
166 my $store = $pragma->get_class_store($cleanee);
167
6c0ece9b 168 # register all unknown previous functions as excluded
9b680ffe 169 for my $f (keys %$functions) {
170 next if $store->{remove}{ $f }
171 or $store->{exclude}{ $f };
172 $store->{exclude}{ $f } = 1;
173 }
174
175 return 1;
176}
177
178=head2 get_class_store
179
6c0ece9b 180This returns a reference to a hash in a passed package containing
181information about function names included and excluded from removal.
9b680ffe 182
183=cut
184
185sub get_class_store {
186 my ($pragma, $class) = @_;
187 no strict 'refs';
188 return \%{ "${class}::${STORAGE_VAR}" };
40aef9d6 189}
190
191=head2 get_functions
192
193Takes a class as argument and returns all currently defined functions
194in it as a hash reference with the function name as key and a typeglob
195reference to the symbol as value.
196
197=cut
198
199sub get_functions {
200 my ($pragma, $class) = @_;
201
202 return {
6c0ece9b 203 map { @$_ } # key => value
204 grep { *{ $_->[1] }{CODE} } # only functions
205 map { [$_, qualify_to_ref( $_, $class )] } # get globref
206 grep { $_ !~ /::$/ } # no packages
207 do { no strict 'refs'; keys %{ "${class}::" } } # symbol entries
40aef9d6 208 };
209}
210
6c0ece9b 211=head1 IMPLEMENTATION DETAILS
212
213This module works through the effect that a
214
215 delete $SomePackage::{foo};
216
217will remove the C<foo> symbol from C<$SomePackage> for run time lookups
218(e.g., method calls) but will leave the entry alive to be called by
472d4b1e 219already resolved names in the package itself. C<namespace::clean> will
220restore and therefor in effect keep all glob slots that aren't C<CODE>.
6c0ece9b 221
222A test file has been added to the perl core to ensure that this behaviour
223will be stable in future releases.
224
225Just for completeness sake, if you want to remove the symbol completely,
226use C<undef> instead.
227
40aef9d6 228=head1 SEE ALSO
229
230L<Filter::EOF>
231
232=head1 AUTHOR AND COPYRIGHT
233
234Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
235Matt S Trout for the inspiration on the whole idea.
236
237=head1 LICENSE
238
239This program is free software; you can redistribute it and/or modify
240it under the same terms as perl itself.
241
242=cut
243
2441;