Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / namespace / autoclean.pm
1 use strict;
2 use warnings;
3
4 package namespace::autoclean;
5 our $VERSION = '0.09';
6
7 # ABSTRACT: Keep imports out of your namespace
8
9 use Class::MOP;
10 use B::Hooks::EndOfScope;
11 use List::Util qw( first );
12 use namespace::clean;
13
14
15 sub import {
16     my ($class, %args) = @_;
17
18     my $subcast = sub {
19         my $i = shift;
20         return $i if ref $i eq 'CODE';
21         return sub { $_ =~ $i } if ref $i eq 'Regexp';
22         return sub { $_ eq $i };
23     };
24
25     my $runtest = sub {
26         my ($code, $method_name) = @_;
27         local $_ = $method_name;
28         return $code->();
29     };
30
31     my $cleanee = exists $args{-cleanee} ? $args{-cleanee} : scalar caller;
32
33     my @also = map { $subcast->($_) } (
34         exists $args{-also}
35         ? (ref $args{-also} eq 'ARRAY' ? @{ $args{-also} } : $args{-also})
36         : ()
37     );
38
39     on_scope_end {
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;
43         my %extra = ();
44
45         for my $method (keys %methods) {
46             next if exists $extra{$_};
47             next unless first { $runtest->($_, $method) } @also;
48             $extra{ $method } = 1;
49         }
50
51         my @symbols = keys %{ $meta->get_all_package_symbols('CODE') };
52         namespace::clean->clean_subroutines($cleanee, keys %extra, grep { !$methods{$_} } @symbols);
53     };
54 }
55
56 1;
57
58 __END__
59
60 =pod
61
62 =head1 NAME
63
64 namespace::autoclean - Keep imports out of your namespace
65
66 =head1 VERSION
67
68 version 0.09
69
70 =head1 SYNOPSIS
71
72     package Foo;
73     use namespace::autoclean;
74     use Some::Package qw/imported_function/;
75
76     sub bar { imported_function('stuff') }
77
78     # later on:
79     Foo->bar;               # works
80     Foo->imported_function; # will fail. imported_function got cleaned after compilation
81
82 =head1 DESCRIPTION
83
84 When you import a function into a Perl package, it will naturally also be
85 available as a method.
86
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
90 class or instances.
91
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>.
96
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:
99
100   package My::MooseX::namespace::autoclean;
101   use strict;
102
103   use namespace::autocleanclean (); # no cleanup, just load
104
105   sub import {
106       namespace::autoclean->import(
107         -cleanee => scalar(caller),
108       );
109   }
110
111 =head1 PARAMETERS
112
113 =head2 -also => [ ITEM | REGEX | SUB, .. ]
114
115 =head2 -also => ITEM
116
117 =head2 -also => REGEX
118
119 =head2 -also => SUB
120
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:
124
125     use namespace::autoclean -also => ['some_function', 'another_function'];
126
127 If only one function needs to be additionally cleaned the C<-also> switch also
128 accepts a plain string:
129
130     use namespace::autoclean -also => 'some_function';
131
132 In some situations, you may wish for a more I<powerful> cleaning solution.
133
134 The C<-also> switch can take a Regex or a CodeRef to match against local
135 function names to clean.
136
137     use namespace::autoclean -also => qr/^_/
138
139     use namespace::autoclean -also => sub { $_ =~ m{^_} };
140
141     use namespace::autoclean -also => [qr/^_/ , qr/^hidden_/ ];
142
143     use namespace::autoclean -also => [sub { $_ =~ m/^_/ or $_ =~ m/^hidden/ }, sub { uc($_) == $_ } ];
144
145 =head1 SEE ALSO
146
147 L<namespace::clean>
148
149 L<Class::MOP>
150
151 L<B::Hooks::EndOfScope>
152
153
154
155 =head1 AUTHOR
156
157   Florian Ragwitz <rafl@debian.org>
158
159 =head1 COPYRIGHT AND LICENSE
160
161 This software is copyright (c) 2009 by Florian Ragwitz.
162
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.
165
166 =cut 
167
168