File | /usr/local/share/perl/5.10.0/namespace/autoclean.pm |
Statements Executed | 149 |
Total Time | 0.0022565 seconds |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
7 | 1 | 1 | 539µs | 10.1ms | __ANON__[:57] | namespace::autoclean::
7 | 7 | 7 | 241µs | 425µs | import | namespace::autoclean::
0 | 0 | 0 | 0s | 0s | BEGIN | namespace::autoclean::
0 | 0 | 0 | 0s | 0s | __ANON__[:25] | namespace::autoclean::
0 | 0 | 0 | 0s | 0s | __ANON__[:26] | namespace::autoclean::
0 | 0 | 0 | 0s | 0s | __ANON__[:27] | namespace::autoclean::
0 | 0 | 0 | 0s | 0s | __ANON__[:33] | namespace::autoclean::
0 | 0 | 0 | 0s | 0s | __ANON__[:51] | namespace::autoclean::
Line | Stmts. | Exclusive Time | Avg. | Code |
---|---|---|---|---|
1 | 3 | 30µs | 10µs | use strict; # spent 9µs making 1 call to strict::import |
2 | 3 | 52µs | 17µs | use warnings; # spent 24µs making 1 call to warnings::import |
3 | ||||
4 | package namespace::autoclean; | |||
5 | BEGIN { | |||
6 | 1 | 1µs | 1µs | $namespace::autoclean::AUTHORITY = 'cpan:FLORA'; |
7 | 1 | 25µs | 25µs | } |
8 | BEGIN { | |||
9 | 1 | 1µs | 1µs | $namespace::autoclean::VERSION = '0.11'; |
10 | 1 | 25µs | 25µs | } |
11 | # ABSTRACT: Keep imports out of your namespace | |||
12 | ||||
13 | 3 | 166µs | 55µs | use Class::MOP 0.80; # spent 32µs making 1 call to UNIVERSAL::VERSION
# spent 3µs making 1 call to import |
14 | 3 | 184µs | 62µs | use B::Hooks::EndOfScope; # spent 359µs making 1 call to Sub::Exporter::__ANON__[/usr/local/share/perl/5.10.0/Sub/Exporter.pm:756] |
15 | 3 | 45µs | 15µs | use List::Util qw( first ); # spent 57µs making 1 call to Exporter::import |
16 | 3 | 658µs | 219µs | use namespace::clean 0.11; # spent 254µs making 1 call to namespace::clean::import
# spent 23µs making 1 call to UNIVERSAL::VERSION |
17 | ||||
18 | ||||
19 | # spent 425µs (241+185) within namespace::autoclean::import which was called 7 times, avg 61µs/call:
# once (39µs+31µs) at line 4 of /home/tamil/util/marc-moose/lib/MARC/Moose/Parser/Marcxml.pm
# once (37µs+30µs) at line 4 of /home/tamil/util/marc-moose/lib/MARC/Moose/Field/Std.pm
# once (36µs+28µs) at line 4 of /home/tamil/util/marc-moose/lib/MARC/Moose/Parser.pm
# once (36µs+29µs) at line 4 of /home/tamil/util/marc-moose/lib/MARC/Moose/Parser/MarcxmlSax.pm
# once (34µs+25µs) at line 4 of /home/tamil/util/marc-moose/lib/MARC/Moose/Field/Control.pm
# once (30µs+23µs) at line 4 of /home/tamil/util/marc-moose/lib/MARC/Moose/Field.pm
# once (28µs+19µs) at line 4 of /home/tamil/util/marc-moose/lib/MARC/Moose/Record.pm | |||
20 | 7 | 17µs | 2µs | my ($class, %args) = @_; |
21 | ||||
22 | my $subcast = sub { | |||
23 | my $i = shift; | |||
24 | return $i if ref $i eq 'CODE'; | |||
25 | return sub { $_ =~ $i } if ref $i eq 'Regexp'; | |||
26 | return sub { $_ eq $i }; | |||
27 | 7 | 36µs | 5µs | }; |
28 | ||||
29 | my $runtest = sub { | |||
30 | my ($code, $method_name) = @_; | |||
31 | local $_ = $method_name; | |||
32 | return $code->(); | |||
33 | 7 | 28µs | 4µs | }; |
34 | ||||
35 | 7 | 18µs | 3µs | my $cleanee = exists $args{-cleanee} ? $args{-cleanee} : scalar caller; |
36 | ||||
37 | my @also = map { $subcast->($_) } ( | |||
38 | exists $args{-also} | |||
39 | 7 | 12µs | 2µs | ? (ref $args{-also} eq 'ARRAY' ? @{ $args{-also} } : $args{-also}) |
40 | : () | |||
41 | ); | |||
42 | ||||
43 | # spent 10.1ms (539µs+9.54) within namespace::autoclean::__ANON__[/usr/local/share/perl/5.10.0/namespace/autoclean.pm:57] which was called 7 times, avg 1.44ms/call:
# 7 times (539µs+9.54ms) by B::Hooks::EndOfScope::__ANON__[/usr/local/share/perl/5.10.0/B/Hooks/EndOfScope.pm:47] at line 47 of /usr/local/share/perl/5.10.0/B/Hooks/EndOfScope.pm, avg 1.44ms/call | |||
44 | 7 | 44µs | 6µs | my $meta = Class::MOP::Class->initialize($cleanee); # spent 106µs making 7 calls to Class::MOP::Class::initialize, avg 15µs/call |
45 | 7 | 128µs | 18µs | my %methods = map { ($_ => 1) } $meta->get_method_list; # spent 7.46ms making 7 calls to Class::MOP::Mixin::HasMethods::get_method_list, avg 1.07ms/call |
46 | 7 | 121µs | 17µs | $methods{meta} = 1 if $meta->isa('Moose::Meta::Role') && Moose->VERSION < 0.90; # spent 109µs making 7 calls to UNIVERSAL::isa, avg 16µs/call |
47 | 7 | 6µs | 814ns | my %extra = (); |
48 | ||||
49 | 7 | 49µs | 7µs | for my $method (keys %methods) { |
50 | 17 | 14µs | 847ns | next if exists $extra{$_}; |
51 | 17 | 148µs | 9µs | next unless first { $runtest->($_, $method) } @also; # spent 49µs making 17 calls to List::Util::first, avg 3µs/call |
52 | $extra{ $method } = 1; | |||
53 | } | |||
54 | ||||
55 | 7 | 203µs | 29µs | my @symbols = keys %{ $meta->get_all_package_symbols('CODE') }; # spent 114µs making 7 calls to Class::MOP::Package::get_all_package_symbols, avg 16µs/call |
56 | 7 | 114µs | 16µs | namespace::clean->clean_subroutines($cleanee, keys %extra, grep { !$methods{$_} } @symbols); # spent 1.70ms making 7 calls to namespace::clean::clean_subroutines, avg 243µs/call |
57 | 7 | 116µs | 17µs | }; # spent 185µs making 7 calls to B::Hooks::EndOfScope::on_scope_end, avg 26µs/call |
58 | } | |||
59 | ||||
60 | 1 | 4µs | 4µs | 1; |
61 | ||||
62 | 1 | 10µs | 10µs | __END__ |
63 | =pod | |||
64 | ||||
65 | =head1 NAME | |||
66 | ||||
67 | namespace::autoclean - Keep imports out of your namespace | |||
68 | ||||
69 | =head1 SYNOPSIS | |||
70 | ||||
71 | package Foo; | |||
72 | use namespace::autoclean; | |||
73 | use Some::Package qw/imported_function/; | |||
74 | ||||
75 | sub bar { imported_function('stuff') } | |||
76 | ||||
77 | # later on: | |||
78 | Foo->bar; # works | |||
79 | Foo->imported_function; # will fail. imported_function got cleaned after compilation | |||
80 | ||||
81 | =head1 DESCRIPTION | |||
82 | ||||
83 | When you import a function into a Perl package, it will naturally also be | |||
84 | available as a method. | |||
85 | ||||
86 | The C<namespace::autoclean> pragma will remove all imported symbols at the end | |||
87 | of the current package's compile cycle. Functions called in the package itself | |||
88 | will still be bound by their name, but they won't show up as methods on your | |||
89 | class or instances. | |||
90 | ||||
91 | This module is very similar to L<namespace::clean|namespace::clean>, except it | |||
92 | will clean all imported functions, no matter if you imported them before or | |||
93 | after you C<use>d the pragma. It will also not touch anything that looks like a | |||
94 | method, according to C<Class::MOP::Class::get_method_list>. | |||
95 | ||||
96 | If you're writing an exporter and you want to clean up after yourself (and your | |||
97 | peers), you can use the C<-cleanee> switch to specify what package to clean: | |||
98 | ||||
99 | package My::MooseX::namespace::autoclean; | |||
100 | use strict; | |||
101 | ||||
102 | use namespace::autoclean (); # no cleanup, just load | |||
103 | ||||
104 | sub import { | |||
105 | namespace::autoclean->import( | |||
106 | -cleanee => scalar(caller), | |||
107 | ); | |||
108 | } | |||
109 | ||||
110 | =head1 PARAMETERS | |||
111 | ||||
112 | =head2 -also => [ ITEM | REGEX | SUB, .. ] | |||
113 | ||||
114 | =head2 -also => ITEM | |||
115 | ||||
116 | =head2 -also => REGEX | |||
117 | ||||
118 | =head2 -also => SUB | |||
119 | ||||
120 | Sometimes you don't want to clean imports only, but also helper functions | |||
121 | you're using in your methods. The C<-also> switch can be used to declare a list | |||
122 | of functions that should be removed additional to any imports: | |||
123 | ||||
124 | use namespace::autoclean -also => ['some_function', 'another_function']; | |||
125 | ||||
126 | If only one function needs to be additionally cleaned the C<-also> switch also | |||
127 | accepts a plain string: | |||
128 | ||||
129 | use namespace::autoclean -also => 'some_function'; | |||
130 | ||||
131 | In some situations, you may wish for a more I<powerful> cleaning solution. | |||
132 | ||||
133 | The C<-also> switch can take a Regex or a CodeRef to match against local | |||
134 | function names to clean. | |||
135 | ||||
136 | use namespace::autoclean -also => qr/^_/ | |||
137 | ||||
138 | use namespace::autoclean -also => sub { $_ =~ m{^_} }; | |||
139 | ||||
140 | use namespace::autoclean -also => [qr/^_/ , qr/^hidden_/ ]; | |||
141 | ||||
142 | use namespace::autoclean -also => [sub { $_ =~ m/^_/ or $_ =~ m/^hidden/ }, sub { uc($_) == $_ } ]; | |||
143 | ||||
144 | =head1 SEE ALSO | |||
145 | ||||
146 | L<namespace::clean> | |||
147 | ||||
148 | L<Class::MOP> | |||
149 | ||||
150 | L<B::Hooks::EndOfScope> | |||
151 | ||||
152 | =head1 AUTHOR | |||
153 | ||||
154 | Florian Ragwitz <rafl@debian.org> | |||
155 | ||||
156 | =head1 COPYRIGHT AND LICENSE | |||
157 | ||||
158 | This software is copyright (c) 2010 by Florian Ragwitz. | |||
159 | ||||
160 | This is free software; you can redistribute it and/or modify it under | |||
161 | the same terms as the Perl 5 programming language system itself. | |||
162 | ||||
163 | =cut | |||
164 |