File | /wise/base/static/lib/perl5/site_perl/5.10.0/Exporter/Lite.pm | Statements Executed | 279 | Total Time | 0.000912000000000002 seconds |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine | |
---|---|---|---|---|---|---|
14 | 14 | 10 | 0.00055 | 0.00099 | Exporter::Lite:: | import |
14 | 1 | 1 | 0.00044 | 0.00044 | Exporter::Lite:: | _export |
0 | 0 | 0 | 0 | 0 | Exporter::Lite:: | _not_exported |
Line | Stmts. | Exclusive Time | Avg. | Code |
---|---|---|---|---|
1 | package Exporter::Lite; | |||
2 | ||||
3 | 1 | 2.8e-5 | 2.8e-5 | require 5.004; |
4 | ||||
5 | # Using strict or vars almost doubles our load time. Turn them back | |||
6 | # on when debugging. | |||
7 | #use strict 'vars'; # we're going to be doing a lot of sym refs | |||
8 | #use vars qw($VERSION @EXPORT); | |||
9 | ||||
10 | 1 | 1.8e-5 | 1.8e-5 | $VERSION = 0.02; |
11 | 1 | 1.0e-6 | 1.0e-6 | @EXPORT = qw(import); # we'll know pretty fast if it doesn't work :) |
12 | ||||
13 | ||||
14 | ||||
15 | # spent 989µs (547+442) within Exporter::Lite::import which was called 14 times, avg 71µs/call:
# once (200µs+216µs) at line 79 of /wise/base/deliv/dev/lib/perl/WISE/Utils.pm
# once (62µs+37µs) at line 78 of /wise/base/deliv/dev/lib/perl/WISE/Utils.pm
# once (50µs+22µs) by WISE::BandUtils::BEGIN at line 14 of /wise/base/deliv/dev/lib/perl/WISE/BandUtils.pm
# once (40µs+30µs) at line 18 of /wise/base/deliv/dev/lib/perl/WISE/UtilsLight.pm
# once (29µs+11µs) by WISE::UtilsLight::BEGIN at line 1 of (eval 198)[/wise/base/deliv/dev/lib/perl/WISE/UtilsLight.pm:168] at line 168 of /wise/base/deliv/dev/lib/perl/WISE/UtilsLight.pm
# once (19µs+20µs) by WISE::Wrap::BEGIN at line 23 of /wise/base/deliv/dev/lib/perl/WISE/Wrap.pm
# once (19µs+20µs) by WISE::WCS::BEGIN at line 18 of /wise/base/deliv/dev/lib/perl/WISE/FITSIO/Utils.pm
# once (21µs+16µs) at line 46 of /wise/base/deliv/dev/lib/perl/WISE/Params.pm
# once (15µs+22µs) at line 12 of /wise/base/deliv/dev/lib/perl/WISE/Spawn.pm
# once (23µs+11µs) at line 18 of /wise/base/deliv/dev/lib/perl/WISE/Pars.pm
# once (20µs+10µs) at line 16 of (eval 4)[/wise/base/deliv/dev/lib/perl/WISE.pm:11] at line 11 of /wise/base/deliv/dev/lib/perl/WISE.pm
# once (19µs+9µs) at line 40 of /wise/base/deliv/dev/lib/perl/WISE/Params.pm
# once (17µs+9µs) at line 39 of /wise/base/deliv/dev/lib/perl/WISE/Params.pm
# once (13µs+9µs) at line 87 of /wise/base/deliv/dev/lib/perl/WISE/Utils.pm | |||
16 | 200 | 0.00047 | 2.4e-6 | my($exporter, @imports) = @_; |
17 | my($caller, $file, $line) = caller; | |||
18 | ||||
19 | unless( @imports ) { # Default import. | |||
20 | @imports = @{$exporter.'::EXPORT'}; | |||
21 | } | |||
22 | else { | |||
23 | # Because @EXPORT_OK = () would indicate that nothing is | |||
24 | # to be exported, we cannot simply check the length of @EXPORT_OK. | |||
25 | # We must to oddness to see if the variable exists at all as | |||
26 | # well as avoid autovivification. | |||
27 | # XXX idea stolen from base.pm, this might be all unnecessary | |||
28 | my $eokglob; | |||
29 | if( $eokglob = ${$exporter.'::'}{EXPORT_OK} and *$eokglob{ARRAY} ) { | |||
30 | if( @{$exporter.'::EXPORT_OK'} ) { | |||
31 | # This can also be cached. | |||
32 | my %ok = map { s/^&//; $_ => 1 } @{$exporter.'::EXPORT_OK'}, | |||
33 | @{$exporter.'::EXPORT'}; | |||
34 | ||||
35 | my($denied) = grep {s/^&//; !$ok{$_}} @imports; | |||
36 | _not_exported($denied, $exporter, $file, $line) if $denied; | |||
37 | } | |||
38 | else { # We don't export anything. | |||
39 | _not_exported($imports[0], $exporter, $file, $line); | |||
40 | } | |||
41 | } | |||
42 | } | |||
43 | ||||
44 | _export($caller, $exporter, @imports); # spent 442µs making 14 calls to Exporter::Lite::_export, avg 32µs/call | |||
45 | } | |||
46 | ||||
47 | ||||
48 | ||||
49 | # spent 442µs within Exporter::Lite::_export which was called 14 times, avg 32µs/call:
# 14 times (442µs+0) by Exporter::Lite::import at line 44, avg 32µs/call | |||
50 | 75 | 0.00039 | 5.1e-6 | my($caller, $exporter, @imports) = @_; |
51 | ||||
52 | # Stole this from Exporter::Heavy. I'm sure it can be written better | |||
53 | # but I'm lazy at the moment. | |||
54 | foreach my $sym (@imports) { | |||
55 | # shortcut for the common case of no type character | |||
56 | (*{$caller.'::'.$sym} = \&{$exporter.'::'.$sym}, next) | |||
57 | unless $sym =~ s/^(\W)//; | |||
58 | ||||
59 | my $type = $1; | |||
60 | my $caller_sym = $caller.'::'.$sym; | |||
61 | my $export_sym = $exporter.'::'.$sym; | |||
62 | *{$caller_sym} = | |||
63 | $type eq '&' ? \&{$export_sym} : | |||
64 | $type eq '$' ? \${$export_sym} : | |||
65 | $type eq '@' ? \@{$export_sym} : | |||
66 | $type eq '%' ? \%{$export_sym} : | |||
67 | $type eq '*' ? *{$export_sym} : | |||
68 | do { require Carp; Carp::croak("Can't export symbol: $type$sym") }; | |||
69 | } | |||
70 | } | |||
71 | ||||
72 | ||||
73 | #"# | |||
74 | sub _not_exported { | |||
75 | my($thing, $exporter, $file, $line) = @_; | |||
76 | die sprintf qq|"%s" is not exported by the %s module at %s line %d\n|, | |||
77 | $thing, $exporter, $file, $line; | |||
78 | } | |||
79 | ||||
80 | 1 | 7.0e-6 | 7.0e-6 | 1; |
81 | ||||
82 | __END__ | |||
83 | ||||
84 | =head1 NAME | |||
85 | ||||
86 | Exporter::Lite - Lightweight exporting of variables | |||
87 | ||||
88 | =head1 SYNOPSIS | |||
89 | ||||
90 | package Foo; | |||
91 | use Exporter::Lite; | |||
92 | ||||
93 | # Just like Exporter. | |||
94 | @EXPORT = qw($This That); | |||
95 | @EXPORT_OK = qw(@Left %Right); | |||
96 | ||||
97 | ||||
98 | # Meanwhile, in another piece of code! | |||
99 | package Bar; | |||
100 | use Foo; # exports $This and &That. | |||
101 | ||||
102 | ||||
103 | =head1 DESCRIPTION | |||
104 | ||||
105 | This is an alternative to Exporter intended to provide a lightweight | |||
106 | subset of its functionality. It supports C<import()>, C<@EXPORT> and | |||
107 | C<@EXPORT_OK> and not a whole lot else. | |||
108 | ||||
109 | Unlike Exporter, it is not necessary to inherit from Exporter::Lite | |||
110 | (ie. no C<@ISA = qw(Exporter::Lite)> mantra). Exporter::Lite simply | |||
111 | exports its import() function. This might be called a "mix-in". | |||
112 | ||||
113 | Setting up a module to export its variables and functions is simple: | |||
114 | ||||
115 | package My::Module; | |||
116 | use Exporter::Lite; | |||
117 | ||||
118 | @EXPORT = qw($Foo bar); | |||
119 | ||||
120 | now when you C<use My::Module>, C<$Foo> and C<bar()> will show up. | |||
121 | ||||
122 | In order to make exporting optional, use @EXPORT_OK. | |||
123 | ||||
124 | package My::Module; | |||
125 | use Exporter::Lite; | |||
126 | ||||
127 | @EXPORT_OK = qw($Foo bar); | |||
128 | ||||
129 | when My::Module is used, C<$Foo> and C<bar()> will I<not> show up. | |||
130 | You have to ask for them. C<use My::Module qw($Foo bar)>. | |||
131 | ||||
132 | =head1 Methods | |||
133 | ||||
134 | Export::Lite has one public method, import(), which is called | |||
135 | automaticly when your modules is use()'d. | |||
136 | ||||
137 | In normal usage you don't have to worry about this at all. | |||
138 | ||||
139 | =over 4 | |||
140 | ||||
141 | =item B<import> | |||
142 | ||||
143 | Some::Module->import; | |||
144 | Some::Module->import(@symbols); | |||
145 | ||||
146 | Works just like C<Exporter::import()> excepting it only honors | |||
147 | @Some::Module::EXPORT and @Some::Module::EXPORT_OK. | |||
148 | ||||
149 | The given @symbols are exported to the current package provided they | |||
150 | are in @Some::Module::EXPORT or @Some::Module::EXPORT_OK. Otherwise | |||
151 | an exception is thrown (ie. the program dies). | |||
152 | ||||
153 | If @symbols is not given, everything in @Some::Module::EXPORT is | |||
154 | exported. | |||
155 | ||||
156 | =back | |||
157 | ||||
158 | =head1 DIAGNOSTICS | |||
159 | ||||
160 | =over 4 | |||
161 | ||||
162 | =item '"%s" is not exported by the %s module' | |||
163 | ||||
164 | Attempted to import a symbol which is not in @EXPORT or @EXPORT_OK. | |||
165 | ||||
166 | =item 'Can\'t export symbol: %s' | |||
167 | ||||
168 | Attempted to import a symbol of an unknown type (ie. the leading $@% salad | |||
169 | wasn't recognized). | |||
170 | ||||
171 | =back | |||
172 | ||||
173 | =head1 BUGS and CAVEATS | |||
174 | ||||
175 | Its not yet clear if this is actually any lighter or faster than | |||
176 | Exporter. I know its at least on par. | |||
177 | ||||
178 | OTOH, the docs are much clearer and not having to say C<@ISA = | |||
179 | qw(Exporter)> is kinda nice. | |||
180 | ||||
181 | =head1 AUTHORS | |||
182 | ||||
183 | Michael G Schwern <schwern@pobox.com> | |||
184 | ||||
185 | =head1 LICENSE | |||
186 | ||||
187 | This program is free software; you can redistribute it and/or | |||
188 | modify it under the same terms as Perl itself. | |||
189 | ||||
190 | See F<http://www.perl.com/perl/misc/Artistic.html> | |||
191 | ||||
192 | =head1 SEE ALSO | |||
193 | ||||
194 | L<Exporter>, L<Exporter::Simple>, L<UNIVERSAL::exports> | |||
195 | ||||
196 | =cut |