blib/lib/Badger/Exporter.pm untested

filter: Statementstmt 0.0% 0 / 262 Branchbran 0.0% 0 / 150 Conditioncond 0.0% 0 / 62 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 518 CC 75 SCAR 61.8
CC
75
Cov
0%
export
1260.0
exportables
272.0
export_hook_generator
72.0
CRAP 482.0
1 #========================================================================
2 #
3 # Badger::Exporter
4 #
5 # DESCRIPTION
6 # This module is an OO version of the Exporter module. It
7 # does the same kind of thing but with an OO interface that means
8 # you don't have to go messing around with package variables. It
9 # correctly handles inheritance, exporting not only those symbols
10 # defined by a subclass, but also those of its base classes.
11 #
12 # AUTHOR
13 # Andy Wardley <abw@wardley.org>
14 #
15 #========================================================================
16  
17 package Badger::Exporter;
18  
19 use Carp;
20 use strict;
21 use warnings;
22 use constant {
23 ALL => 'all', # Alas, we can't pull these in from
24 NONE => 'none', # Badger::Constants because it's a
25 DEFAULT => 'default', # subclass of Badger::Exporter which
26 IMPORT => 'import', # gives us a chicken-and-egg dependency
27 IMPORTS => 'imports', # problem.We could pull them into
28 HOOKS => 'hooks', # Badger::Constants though because
29 ARRAY => 'ARRAY', # that's a subclass... hmmm....
30 HASH => 'HASH',
31 CODE => 'CODE',
32 EXPORT_ALL => 'EXPORT_ALL',
33 EXPORT_ANY => 'EXPORT_ANY',
34 EXPORT_TAGS => 'EXPORT_TAGS',
35 EXPORT_FAIL => 'EXPORT_FAIL',
36 EXPORT_HOOKS => 'EXPORT_HOOKS',
37 EXPORT_BEFORE => 'EXPORT_BEFORE',
38 EXPORT_AFTER => 'EXPORT_AFTER',
39 EXPORTABLES => 'EXPORTABLES',
40 ISA => 'ISA',
41 REFS => 'refs',
42 ONCE => 'once',
43 PKG => '::',
44 DELIMITER => qr/(?:,\s*)|\s+/, # match a comma or whitespace
45 MISSING => "Missing value for the '%s' option%s",
46 BAD_HANDLER => "Invalid export %s handler specified: %s",
47 BAD_HOOK => "Invalid export hook handler specified for the '%s' option: %s",
48 WANTED => " (%s wanted, %s specified)",
49 UNDEFINED => " (argument %s of %s is undefined)",
50 };
51  
52 our $VERSION = 0.01;
53 our $DEBUG = 0 unless defined $DEBUG;
54 our $HANDLERS = {
55 all => \&export_all,
56 any => \&export_any,
57 tags => \&export_tags,
58 hooks => \&export_hooks,
59 fail => \&export_fail,
60 before => \&export_before,
61 after => \&export_after,
62 };
63  
64  
65 #-----------------------------------------------------------------------
66 # export declaration methods:
67 # exports( all => [...], any => [...], ...etc... )
68 # export_all('foo bar baz')
69 # export_any('foo bar baz')
70 # export_before( sub { ... } )
71 # export_after( sub { ... } )
72 # export_tags( set1 => 'foo bar baz', set2 => 'wam bam' )
73 # export_hooks( foo => sub { ... }, bar => sub { ... } )
74 # export_fail( sub { ... } )
75 #-----------------------------------------------------------------------
76  
77 sub exports {
78 my $self = shift;
79 my $data = @_ == 1 && ref $_[0] eq HASH ? shift : { @_ };
80 my $handler;
81  
82 # delegate each key in $data to a handler in $HANDLERS
83 while (my ($key, $value) = each %$data) {
84 $handler = $HANDLERS->{ $key }
85 || croak "Invalid exports key: $key\n";
86 $handler->($self, $value);
87 }
88 }
89  
90 sub export_all {
91 my $self = shift;
92 my $args = @_ == 1 ? shift : [ @_ ];
93 my $list = $self->export_variable( EXPORT_ALL => [ ] );
94 push( @$list, ref $args eq ARRAY ? @$args : split(DELIMITER, $args) );
95 }
96  
97 sub export_any {
98 my $self = shift;
99 my $args = @_ == 1 ? shift : [ @_ ];
100 my $list = $self->export_variable( EXPORT_ANY => [ ] );
101 push( @$list, ref $args eq ARRAY ? @$args : split(DELIMITER, $args) );
102 }
103  
104 sub export_before {
105 my $self = shift;
106 my $args = @_ == 1 ? shift : [ @_ ];
107 my $list = $self->export_variable( EXPORT_BEFORE => [ ] );
108 push( @$list, ref $args eq ARRAY ? @$args : $args );
109 }
110  
111 sub export_after {
112 my $self = shift;
113 my $args = @_ == 1 ? shift : [ @_ ];
114 my $list = $self->export_variable( EXPORT_AFTER => [ ] );
115 push( @$list, ref $args eq ARRAY ? @$args : $args );
116 }
117  
118 sub export_tags {
119 my $self = shift;
120 my $args = (@_ == 1) && (ref $_[0] eq HASH) ? shift : { @_ };
121 my $tags = $self->export_variable( EXPORT_TAGS => { } );
122  
123 # Add new tags into $EXPORT_TAGS hash ref
124 @$tags{ keys %$args } = map {
125 # Tags can be defined as hash arrays containing (key => '=value')
126 # declarataions. We upgrade each '=value' to a constant subroutine.
127 if (ref && ref eq HASH) {
128 while (my ($key, $value) = each %$_) {
129 if ($value =~ s/^=//) {
130 _debug("export_tags() constructing constant: $key => $value\n") if $DEBUG;
131 $_->{ $key } = sub() { $value };
132 }
133 }
134 }
135 $_;
136 }
137 values %$args;
138  
139 # all symbols referenced in tagsets (except other tag sets) must be
140 # flagged as exportable
141 $self->export_any(
142 grep {
143 # ignore references to code or other tag sets
144 not (ref || /^(:|=)/);
145 }
146 map {
147 # symbols in tagset can be a list ref, hash ref or string
148 ref $_ eq ARRAY ? @$_ :
149 ref $_ eq HASH ? %$_ :
150 split DELIMITER
151 }
152 values %$args
153 );
154  
155 return $tags;
156 }
157  
158 sub export_hooks {
159 my $self = shift;
160 my $args = (@_ == 1) && (ref $_[0] eq HASH) ? shift : { @_ };
161 my $hooks = $self->export_variable( EXPORT_HOOKS => { } );
162 @$hooks{ keys %$args } = values %$args;
163 return $hooks;
164 }
165  
166 sub export_fail {
167 my $self = shift;
168 my $class = ref $self || $self;
169 no strict REFS;
170  
171 # get/set $EXPORT_FAIL
172 return @_
173 ? (${$class.PKG.EXPORT_FAIL} = shift)
174 : ${$class.PKG.EXPORT_FAIL};
175 }
176  
177  
178 #------------------------------------------------------------------------
179 # import/export methods:
180 # import(@imports)
181 # export($target, @exports)
182 #------------------------------------------------------------------------
183  
184 sub import {
185 my $class = shift;
186 my $target = (caller())[0];
187  
188 # enable strict and warnings in the caller - this ensures that every
189 # Badger module (that calls this method - which is pretty much all of
190 # them) has strict/warnings enabled, without having to explicitly write
191 # it. Thx Moose!
192 strict->import;
193 warnings->import;
194  
195 # call in the heavy guns
196 $class->export($target, @_);
197 }
198  
199 sub export {
200 my $class = shift;
201 my $target = shift;
202 my $imports = @_ == 1 ? shift : [ @_ ];
203 my ($all, $any, $tags, $hooks, $fails, $before, $after)
204 = $class->exportables;
205 my $can_hook = (%$hooks ? 1 : 0);
206 my $added_all = 0;
207 my $count = 0;
208 my ($symbol, $symbols, $source, $hook, $pkg, $nargs,
209 %done, @args, @errors);
210  
211 no strict REFS;
212 no warnings ONCE;
213  
214 # imports can be a single whitespace delimited string of symbols
215 $imports = [ split(DELIMITER, $imports) ]
216 unless ref $imports eq ARRAY;
217  
218 # default to export_all if list of exports not specified
219 # TODO: what about: use Badger::Example qw(); ? perhaps we should
220 # return unless @_ up above?
221 @$imports = @$all unless @$imports;
222  
223 foreach $hook (@$before) {
224 $hook->($class, $target, $imports);
225 }
226  
227 SYMBOL: while (@$imports) {
228 next unless ($symbol = shift @$imports);
229 next if $done{ $symbol }++;
230  
231 # look for :tagset symbols and expand their contents onto @$imports
232 if ($symbol =~ s/^://) {
233 if ($symbols = $tags->{ $symbol }) {
234 if (ref $symbols eq ARRAY) {
235 # expand list of symbols onto @$imports list
236 unshift(@$imports, @$symbols);
237 }
238 elsif (ref $symbols eq HASH) {
239 # map hash into [name => $symbol] pairs
240 unshift(@$imports, map { [$_ => $symbols->{ $_ }] } keys %$symbols);
241 }
242 else {
243 # string of space-delimited symbols
244 unshift(@$imports, split(DELIMITER, $symbols));
245 }
246 }
247 elsif ($symbol eq DEFAULT) {
248 unshift(@$imports, @$all);
249 }
250 elsif ($symbol eq ALL) {
251 unshift(@$imports, keys %$any);
252 $added_all = 1;
253 }
254 else {
255 push(@errors, "Invalid import tag: $symbol\n");
256 }
257 next SYMBOL;
258 }
259  
260 if (ref $symbol eq ARRAY) {
261 # a pair of [name, $symbol] expanded from a :tag hash set
262 ($symbol, $source) = @$symbol;
263 # _debug("expanded export pair: $symbol => $source\n") if $DEBUG;
264 }
265 elsif ($can_hook && ($hook = $hooks->{ $symbol })) {
266 # a hook can be specified as [$code,$nargs] in which case we
267 # generate a closure around the $code which shifts $nargs off
268 # the symbols list and passes them as arguments to $code
269 $hook = $hooks->{ $symbol } = $class->export_hook_generator($symbol, $hook)
270 unless ref $hook eq CODE;
271  
272 # fire off handler hooked to this import item
273 &$hook($class, $target, $symbol, $imports);
274  
275 # hooks can be repeated so pretend we haven't done it
276 $done{ $symbol }--;
277 next SYMBOL;
278 }
279 elsif ($symbol eq IMPORTS) {
280 # special 'imports' hook disables any more hooks causing
281 # all remaining arguments to be imported as symbols
282 $can_hook = 0;
283 next SYMBOL;
284 }
285 elsif ($symbol eq IMPORT) {
286 # 'import' hook accepts the next item as an import list/string
287 # and unpacks it onto the front of the imports list. We disable
288 # hooks for the duration of the import and insert a dummy HOOKS
289 # symbol at the end to re-enable hooks
290 $can_hook = 0;
291 if ($symbols = shift @$imports) {
292 $symbols = [ split(DELIMITER, $symbols) ]
293 unless ref $symbols eq ARRAY;
294 unshift(@$imports, @$symbols, HOOKS);
295 }
296 else {
297 push(@errors, "Missing argument for $symbol hook\n");
298 }
299 next SYMBOL;
300 }
301 elsif ($symbol eq HOOKS) {
302 # special 'hooks' item turns hooks back on
303 $can_hook = 1;
304 next SYMBOL;
305 }
306 else {
307 # otherwise the symbol exported is the one requested
308 $source = $symbol;
309 }
310  
311 # check we're allowed to export the symbol requested
312 if ($pkg = $any->{ $symbol }) {
313 # _debug("exporting $symbol from $pkg to $target\n") if $DEBUG;
314 }
315 else {
316 foreach $hook (@$fails) {
317 if (&$hook($class, $target, $symbol, $imports)) {
318 # hooks can be repeated so pretend we haven't done it
319 $done{ $symbol }--;
320 next SYMBOL;
321 }
322 }
323 push(@errors, "$symbol is not exported by $class\n");
324 next SYMBOL;
325 }
326  
327 if (ref $source eq CODE) {
328 # patch directly into the code ref
329 # _debug("exporting $symbol from code reference\n") if $DEBUG;
330 *{ $target.PKG.$symbol } = $source;
331 }
332 else {
333 my $type = "&";
334 $symbol =~ s/^(\W)//;
335 $source =~ s/^(\W)// and $type = $1;
336 # NOTE: '=value' should *probably* never be found at this point
337 # because we're now upgrading them to constant subroutines in
338 # the import_tags() method. However, I'm leaving this in here
339 # until I've had a chance to properly review the code and convince
340 # myself that this assumption is correct.
341 _debug("export() constructing constant: $symbol => $source\n")
342 if $DEBUG && $type eq '=';
343 $source = $pkg.PKG.$source unless $source =~ /::/ or $type eq '=';
344 _debug("exporting $type$symbol from $source into $target\n") if $DEBUG;
345 *{ $target.PKG.$symbol } =
346 $type eq '&' ? \&{$source} :
347 $type eq '$' ? \${$source} :
348 $type eq '@' ? \@{$source} :
349 $type eq '%' ? \%{$source} :
350 $type eq '*' ? *{$source} :
351 $type eq '=' ? sub(){$source} :
352 do { push(@errors, "Can't export symbol: $type$symbol\n"); next; };
353 }
354 $count++;
355 }
356 continue {
357 # if we're on the last item and we've only processed hooks
358 # (i.e. no real symbols were specified then we export the
359 # default set of symbols instead
360 unless (@$imports or $count or $added_all) {
361 unshift(@$imports, @$all);
362 $added_all = 1;
363 }
364 }
365  
366 if (@errors) {
367 require Carp;
368 Carp::croak("@{errors}Can't continue after import errors");
369 }
370  
371 foreach $hook (@$after) {
372 $hook->($class, $target);
373 }
374  
375 return 1;
376 }
377  
378 sub exportables {
379 my $class = shift;
380 no strict REFS;
381 no warnings ONCE;
382  
383 my $cache = ${ $class.PKG.EXPORTABLES } ||= do {
384 my ($pkg, $symbols, %done, @all, %any, %tags, %hooks, @fails, @before, @after);
385 my @pending = ($class);
386 no strict REFS;
387  
388 # walk up inheritance tree collecting values from the @$EXPORT_ALL,
389 # @$EXPORT_ANY, %$EXPORT_TAGS, %$EXPORT_HOOKS and $EXPORT_FAIL pkg
390 # variables, then cache them in $EXPORT_CACHE for subsequent use
391  
392 while ($pkg = shift @pending) {
393 next if $done{ $pkg }++;
394  
395 # TODO: we could optimise here by looking for a previously
396 # computed EXPORTABLES in the base class and merging it in...
397  
398 # $EXPORT_ANY package vars are list references containing symbols,
399 # which we use to populate the %any hash which maps symbols to
400 # their source packages. e.g. { foo => 'My::Package' }
401 # The presence of an entry in this table indicates that the symbol
402 # key can be exported. The corresponding value indicates the
403 # package that it must be exported from. We don't replace any
404 # existing entries in the %any hash because we're working from
405 # sub-class upwards to super-class, . This ensures that the
406 # entries put in first by more specialised sub-classes are used
407 # in preference to those defined by more general super-classes.
408 if ($symbols = ${ $pkg.PKG.EXPORT_ANY }) {
409 $symbols = [ split(DELIMITER, $symbols) ]
410 unless ref $symbols eq ARRAY;
411 $any{ $_ } ||= $pkg
412 for @$symbols;
413 }
414  
415 # $EXPORT_ALL is merged into @all and all symbols are mapped
416 # to their packages in %any
417 if ($symbols = ${ $pkg.PKG.EXPORT_ALL }) {
418 $symbols = [ split(DELIMITER, $symbols) ]
419 unless ref $symbols eq ARRAY;
420 push(
421 @all,
422 map { $any{ $_ } ||= $pkg; $_ }
423 @$symbols
424 );
425 }
426  
427 # $EXPORT_TAGS are copied into %tags unless already defined
428 if ($symbols = ${ $pkg.PKG.EXPORT_TAGS }) {
429 $tags{ $_ } ||= $symbols->{ $_ }
430 for keys %$symbols;
431 }
432  
433 # $EXPORT_HOOKS are copied into %hooks unless already defined
434 # (by a more specific subclass) either as hooks or any/all items
435 if ($symbols = ${ $pkg.PKG.EXPORT_HOOKS }) {
436 $any{ $_ } or $hooks{ $_ } ||= $symbols->{ $_ }
437 for keys %$symbols;
438 }
439  
440 # $EXPORT_FAIL has only one value per package, but we can have
441 # several packages in the class ancestry
442 if ($symbols = ${ $pkg.PKG.EXPORT_FAIL }) {
443 push(@fails, $symbols);
444 }
445  
446 # $EXPORT_BEFORE and $EXPORT_AFTER are references to CODE or
447 # ARRAY refs (of CODE refs, we assume). As we travel up from
448 # subclass to superclass, we unshift() the handlers onto the
449 # start of the @before/@after arrays. This ensures that the base
450 # class handlers get called before subclass handlers.
451 if ($symbols = ${ $pkg.PKG.EXPORT_BEFORE }) {
452 unshift(
453 @before,
454 ref $symbols eq CODE ? $symbols :
455 ref $symbols eq ARRAY ? @$symbols :
456 croak sprintf(BAD_HANDLER, before => $symbols)
457 );
458 }
459  
460 if ($symbols = ${ $pkg.PKG.EXPORT_AFTER }) {
461 unshift(
462 @after,
463 ref $symbols eq CODE ? $symbols :
464 ref $symbols eq ARRAY ? @$symbols :
465 croak sprintf(BAD_HANDLER, after => $symbols)
466 );
467 }
468  
469 # This is the same depth-first inheritance resolution algorithm
470 # that Perl uses. We can't use the fancy heritage() method in
471 # Badger::Class because of the Chicken-and-Egg dependency problem
472 # between Badger::Exporter and Badger::Class
473 push(@pending, @{$pkg.PKG.ISA});
474 }
475  
476 [\@all, \%any, \%tags, \%hooks, \@fails, \@before, \@after];
477 };
478  
479 return wantarray
480 ? @$cache
481 : $cache;
482 }
483  
484 sub export_symbol {
485 my ($self, $target, $symbol, $ref) = @_;
486 no strict REFS;
487 no warnings ONCE;
488 *{ $target.PKG.$symbol } = $ref;
489 }
490  
491 sub export_variable {
492 my ($self, $name, $default) = @_;
493 my $class = ref $self || $self;
494 my $var = $class.PKG.$name;
495 my $item;
496 no strict REFS;
497  
498 unless (defined ($item = ${$var})) {
499 # install the default value ref into the SCALAR $EXPORT_XXXX var
500 ${$var} = $item = $default;
501 # then poke the symbol table to make Perl notice it's defined
502 *{$var} = \${$var};
503 }
504  
505 return $item;
506 }
507  
508 sub export_hook_generator {
509 my $self = shift;
510 my $name = shift;
511 my $hook = @_ == 1 ? shift : [ @_ ];
512  
513 # do nothing if we've already got a code ref that doesn't require args
514 return $hook
515 if ref $hook eq CODE;
516  
517 # anything else must be a list ref containing [$code_ref, $n_args]
518 croak sprintf(BAD_HOOK, $name, $hook)
519 unless ref $hook eq ARRAY;
520  
521 my ($code, $nargs) = @$hook;
522  
523 # user is trying to confuse us with [$non_code_ref, ...]
524 croak sprintf(BAD_HOOK, $name, $code)
525 unless ref $code eq CODE;
526  
527 # [$code, 0] or [$code] is fine as just $code, also reject $nargs < 0
528 return $code
529 unless $nargs && $nargs > 0;
530  
531 # OK it's safe to proceed
532 return sub {
533 my ($this, $target, $symbol, $symbols) = @_;
534 my $n = 1;
535 # check we've got enough arguments
536 croak sprintf(MISSING, $symbol, sprintf(WANTED, $nargs, scalar @$symbols))
537 if @$symbols < $nargs;
538  
539 # call the code ref with the first $nargs arguments, making sure
540 # they all have defined values
541 $code->(
542 $this, $target, $symbol,
543 ( map {
544 croak sprintf(MISSING, $symbol, sprintf(UNDEFINED, $n, $nargs))
545 unless defined $_;
546 $n++;
547 $_
548 }
549 splice(@$symbols, 0, $nargs)
550 ),
551 $symbols,
552 );
553 }
554 }
555  
556 sub _debug {
557 print STDERR @_;
558 }
559  
560  
561 1;
562  
563 __END__