4 package namespace::autoclean;
7 # ABSTRACT: Keep imports out of your namespace
10 use B::Hooks::EndOfScope;
11 use List::Util qw( first );
16 my ($class, %args) = @_;
20 return $i if ref $i eq 'CODE';
21 return sub { $_ =~ $i } if ref $i eq 'Regexp';
22 return sub { $_ eq $i };
26 my ($code, $method_name) = @_;
27 local $_ = $method_name;
31 my $cleanee = exists $args{-cleanee} ? $args{-cleanee} : scalar caller;
33 my @also = map { $subcast->($_) } (
35 ? (ref $args{-also} eq 'ARRAY' ? @{ $args{-also} } : $args{-also})
40 my $meta = Class::MOP::Class->initialize($cleanee);
41 my %methods = map { ($_ => 1) } $meta->get_method_list;
42 $methods{meta} = 1 if $meta->isa('Moose::Meta::Role') && Moose->VERSION < 0.90;
45 for my $method (keys %methods) {
46 next if exists $extra{$_};
47 next unless first { $runtest->($_, $method) } @also;
48 $extra{ $method } = 1;
51 my @symbols = keys %{ $meta->get_all_package_symbols('CODE') };
52 namespace::clean->clean_subroutines($cleanee, keys %extra, grep { !$methods{$_} } @symbols);
64 namespace::autoclean - Keep imports out of your namespace
73 use namespace::autoclean;
74 use Some::Package qw/imported_function/;
76 sub bar { imported_function('stuff') }
80 Foo->imported_function; # will fail. imported_function got cleaned after compilation
84 When you import a function into a Perl package, it will naturally also be
85 available as a method.
87 The C<namespace::autoclean> pragma will remove all imported symbols at the end
88 of the current package's compile cycle. Functions called in the package itself
89 will still be bound by their name, but they won't show up as methods on your
92 This module is very similar to L<namespace::clean|namespace::clean>, except it
93 will clean all imported functions, no matter if you imported them before or
94 after you C<use>d the pragma. It will also not touch anything that looks like a
95 method, according to C<Class::MOP::Class::get_method_list>.
97 If you're writing an exporter and you want to clean up after yourself (and your
98 peers), you can use the C<-cleanee> switch to specify what package to clean:
100 package My::MooseX::namespace::autoclean;
103 use namespace::autocleanclean (); # no cleanup, just load
106 namespace::autoclean->import(
107 -cleanee => scalar(caller),
113 =head2 -also => [ ITEM | REGEX | SUB, .. ]
117 =head2 -also => REGEX
121 Sometimes you don't want to clean imports only, but also helper functions
122 you're using in your methods. The C<-also> switch can be used to declare a list
123 of functions that should be removed additional to any imports:
125 use namespace::autoclean -also => ['some_function', 'another_function'];
127 If only one function needs to be additionally cleaned the C<-also> switch also
128 accepts a plain string:
130 use namespace::autoclean -also => 'some_function';
132 In some situations, you may wish for a more I<powerful> cleaning solution.
134 The C<-also> switch can take a Regex or a CodeRef to match against local
135 function names to clean.
137 use namespace::autoclean -also => qr/^_/
139 use namespace::autoclean -also => sub { $_ =~ m{^_} };
141 use namespace::autoclean -also => [qr/^_/ , qr/^hidden_/ ];
143 use namespace::autoclean -also => [sub { $_ =~ m/^_/ or $_ =~ m/^hidden/ }, sub { uc($_) == $_ } ];
151 L<B::Hooks::EndOfScope>
157 Florian Ragwitz <rafl@debian.org>
159 =head1 COPYRIGHT AND LICENSE
161 This software is copyright (c) 2009 by Florian Ragwitz.
163 This is free software; you can redistribute it and/or modify it under
164 the same terms as the Perl 5 programming language system itself.