Import namespace-clean-0.03.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
6c0ece9b 180.03
40aef9d6 19
20=cut
21
6c0ece9b 22$VERSION = 0.03;
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
72=head1 METHODS
73
74You shouldn't need to call any of these. Just C<use> the package at the
75appropriate place.
76
6c0ece9b 77=cut
78
40aef9d6 79=head2 import
80
81Makes a snapshot of the current defined functions and registers a
6c0ece9b 82L<Filter::EOF> cleanup routine to remove those symbols at the end
83of the compile-time.
40aef9d6 84
85=cut
86
87sub import {
88 my ($pragma) = @_;
89
6c0ece9b 90 # calling class, all current functions and our storage
40aef9d6 91 my $cleanee = caller;
92 my $functions = $pragma->get_functions($cleanee);
9b680ffe 93 my $store = $pragma->get_class_store($cleanee);
40aef9d6 94
6c0ece9b 95 # register symbols for removal, if they have a CODE entry
9b680ffe 96 for my $f (keys %$functions) {
97 next unless $functions->{ $f }
98 and *{ $functions->{ $f } }{CODE};
99 $store->{remove}{ $f } = 1;
100 }
101
6c0ece9b 102 # register EOF handler on first call to import
9b680ffe 103 unless ($store->{handler_is_installed}) {
104 Filter::EOF->on_eof_call(sub {
105 for my $f (keys %{ $store->{remove} }) {
106 next if $store->{exclude}{ $f };
107 no strict 'refs';
40aef9d6 108 delete ${ "${cleanee}::" }{ $f };
109 }
9b680ffe 110 });
111 $store->{handler_is_installed} = 1;
112 }
113
114 return 1;
115}
116
117=head2 unimport
118
119This method will be called when you do a
120
121 no namespace::clean;
122
123It will start a new section of code that defines functions to clean up.
124
125=cut
126
127sub unimport {
128 my ($pragma) = @_;
129
6c0ece9b 130 # the calling class, the current functions and our storage
9b680ffe 131 my $cleanee = caller;
132 my $functions = $pragma->get_functions($cleanee);
133 my $store = $pragma->get_class_store($cleanee);
134
6c0ece9b 135 # register all unknown previous functions as excluded
9b680ffe 136 for my $f (keys %$functions) {
137 next if $store->{remove}{ $f }
138 or $store->{exclude}{ $f };
139 $store->{exclude}{ $f } = 1;
140 }
141
142 return 1;
143}
144
145=head2 get_class_store
146
6c0ece9b 147This returns a reference to a hash in a passed package containing
148information about function names included and excluded from removal.
9b680ffe 149
150=cut
151
152sub get_class_store {
153 my ($pragma, $class) = @_;
154 no strict 'refs';
155 return \%{ "${class}::${STORAGE_VAR}" };
40aef9d6 156}
157
158=head2 get_functions
159
160Takes a class as argument and returns all currently defined functions
161in it as a hash reference with the function name as key and a typeglob
162reference to the symbol as value.
163
164=cut
165
166sub get_functions {
167 my ($pragma, $class) = @_;
168
169 return {
6c0ece9b 170 map { @$_ } # key => value
171 grep { *{ $_->[1] }{CODE} } # only functions
172 map { [$_, qualify_to_ref( $_, $class )] } # get globref
173 grep { $_ !~ /::$/ } # no packages
174 do { no strict 'refs'; keys %{ "${class}::" } } # symbol entries
40aef9d6 175 };
176}
177
6c0ece9b 178=head1 IMPLEMENTATION DETAILS
179
180This module works through the effect that a
181
182 delete $SomePackage::{foo};
183
184will remove the C<foo> symbol from C<$SomePackage> for run time lookups
185(e.g., method calls) but will leave the entry alive to be called by
186already resolved names in the package itself.
187
188A test file has been added to the perl core to ensure that this behaviour
189will be stable in future releases.
190
191Just for completeness sake, if you want to remove the symbol completely,
192use C<undef> instead.
193
40aef9d6 194=head1 SEE ALSO
195
196L<Filter::EOF>
197
198=head1 AUTHOR AND COPYRIGHT
199
200Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
201Matt S Trout for the inspiration on the whole idea.
202
203=head1 LICENSE
204
205This program is free software; you can redistribute it and/or modify
206it under the same terms as perl itself.
207
208=cut
209
2101;