1 package namespace::clean;
5 namespace::clean - Keep imports out of your namespace
12 use vars qw( $VERSION );
13 use Symbol qw( qualify_to_ref );
30 use Carp qw(croak); # will be removed
32 sub bar { 23 } # will be removed
36 sub baz { bar() } # still defined, 'bar' still bound
42 print +(__PACKAGE__->can('croak') ? 'Yes' : 'No'), "\n";
43 print +(__PACKAGE__->can('bar') ? 'Yes' : 'No'), "\n";
44 print +(__PACKAGE__->can('baz') ? 'Yes' : 'No'), "\n";
50 When you define a function, or import one, into a Perl package, it will
51 naturally also be available as a method. This does not per se cause
52 problems, but it can complicate subclassing and, for example, plugin
53 classes that are included by loading them as base classes.
55 The C<namespace::clean> pragma will remove all previously declared or
56 imported symbols at the end of the current package's compile cycle.
57 This means that functions are already bound by their name, and calls to
58 them still work. But they will not be available as methods on your class
63 You shouldn't need to call any of these. Just C<use> the package at the
68 Makes a snapshot of the current defined functions and registers a
69 L<Filter::EOF> cleanup routine to remove those symbols from the package
70 at the end of the compile-time.
78 my $functions = $pragma->get_functions($cleanee);
80 Filter::EOF->on_eof_call(sub {
81 for my $f (keys %$functions) {
82 next unless $functions->{ $f }
83 and *{ $functions->{ $f } }{CODE};
85 delete ${ "${cleanee}::" }{ $f };
93 Takes a class as argument and returns all currently defined functions
94 in it as a hash reference with the function name as key and a typeglob
95 reference to the symbol as value.
100 my ($pragma, $class) = @_;
104 grep { *{ $_->[1] }{CODE} }
105 map { [$_, qualify_to_ref( $_, $class )] }
107 do { no strict 'refs'; keys %{ "${class}::" } }
115 =head1 AUTHOR AND COPYRIGHT
117 Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
118 Matt S Trout for the inspiration on the whole idea.
122 This program is free software; you can redistribute it and/or modify
123 it under the same terms as perl itself.