File Coverage

blib/lib/Module/Pluggable/Ordered.pm
Criterion Covered Total %
statement 48 49 97.9
branch 9 12 75.0
condition 5 11 45.4
subroutine 8 8 100.0
pod n/a
total 70 80 87.5


line stmt bran cond sub pod time code
1             package Module::Pluggable::Ordered;
2             # I really regret this ugliness, but it's needed in the face of backwards
3             # compatibility, and I don't want "code this funky" wandering around CPAN
4             # without warnings if I can help it. :) Those using ancient versions of Perl can
5             # use -w for global warnings. Unfortunately, there's not much else I can do. If
6             # anyone has any suggestions, please do file a bug report. Thanks.
7              
8             BEGIN
9             {
10 4 50   4   148925 if($] >= 5.00600)
11             {
12 4         41 require warnings;
13 4         172 import warnings;
14             }
15             }
16 4     4   22 use strict;
  4         8  
  4         140  
17             require Module::Pluggable;
18 4     4   4402 use UNIVERSAL::require;
  4         8419  
  4         44  
19 4     4   117 use vars qw($VERSION);
  4         8  
  4         566  
20             $VERSION = '1.5';
21              
22             sub import {
23 4     4   72 my ($self, %args) = @_;
24 4   50     30 my $subname = $args{sub_name} || "plugins";
25              
26 4         11 my %only;
27             my %except;
28              
29 4 100       25 %only = map { $_ => 1 } @{$args{'only'}} if defined $args{'only'};
  1         7  
  1         4  
30 4 100       18 %except = map { $_ => 1 } @{$args{'$except'}} if defined $args{'except'};
  0         0  
  1         4  
31              
32 4   33     42 my $caller = $args{package} || caller;
33            
34 4     4   21 no strict;
  4         7  
  4         3914  
35              
36 4         55 *{"${caller}::call_plugins"} = sub {
37 3     3   9858 my ($thing, $name, @args) = @_;
38 3         8 my @plugins = ();
39 3         19 for ($thing->$subname()) {
40 6 50 66     6554 next if (keys %only && !$only{$_} );
41 6 50 33     25 next if (keys %except && $except{$_} );
42 6         13 push @plugins, $_;
43             }
44            
45 3         31 $_->require for @plugins;
46              
47 3         77 my $order_name = "${name}_order";
48 3         11 for my $class (sort { $a->$order_name() <=> $b->$order_name() }
  1         7  
  6         96  
49             grep { $_->can($order_name) }
50             @plugins) {
51 4         581 $class->$name(@args);
52             }
53 4         44 };
54              
55 4         37 *{"${caller}::${subname}_ordered"} = sub {
56 1     1   12 my $thing = shift;
57              
58 1         7 my @plugins = $thing->$subname();
59 1         2987 $_->require for @plugins;
60              
61 3         14 return map { $_->[0] }
  2         14  
62 3 100       49 sort { $a->[1] <=> $b->[1] }
63 1         19 map { [ $_, ( $_->can('_order') ? $_->_order : 50 ) ] }
64             @plugins;
65 4         36 };
66            
67 4         26 goto &Module::Pluggable::import;
68             }
69              
70             1;
71             __END__