File Coverage

lib/all/mandatory.pm
Criterion Covered Total %
statement 53 53 100.0
branch 11 12 91.6
condition n/a
subroutine 10 10 100.0
pod n/a
total 74 75 98.6


line stmt bran cond sub pod time code
1             package all::mandatory;
2              
3 5     5   1936930 use 5.006;
  5         19  
4 5     5   33 use strict;
  5         7  
  5         173  
5 5     5   60 use warnings;
  5         14  
  5         343  
6              
7 5     5   30 use File::Spec ();
  5         15  
  5         134  
8 5     5   23 use File::Find ();
  5         10  
  5         4114  
9              
10             our $VERSION = 0.03;
11              
12             sub import {
13 7     7   78 my $class = shift;
14 7         16 my $of = shift;
15 7         18 my $args = [ @_ ];
16 7 100       34 if ($of ne 'of') {
17 5         15 unshift @$args, $of;
18             }
19 7         23 foreach my $arg (@$args) {
20 9         26 my $modules = _find_modules( $arg );
21              
22 9 100       89 @$modules
23             or die "all::mandatory - No modules under $arg namespace exist";
24              
25 8         26 foreach my $module (@$modules) {
26 20         47 my $package = $module->{ module };
27 20 100       40 eval {
28 20         8794 require $module->{ path };
29 18         418 $package->import;
30 18         122 1;
31             } or die "all::mandatory - Could not load module $module->{path}:\n$@\n";
32             }
33             }
34 4         75 1;
35             }
36              
37             sub _find_modules {
38 9     9   40 my $module = shift;
39 9         22 my $moduledir = _module_to_file( $module );
40 9         20 my $list = [];
41              
42 9         18 foreach my $incdir (@INC) {
43 108 50       253 next if ref $incdir;
44              
45 108         854 my $dir = File::Spec->catfile($incdir, $moduledir);
46 108 100       1840 next unless -d $dir;
47              
48 8         17 my @files = ();
49             File::Find::find({
50             wanted => sub {
51 45 100   45   1993 return unless $File::Find::name =~ /\.pm$/;
52 33         798 push @files, $File::Find::name;
53             },
54 8         1000 no_chdir => 1,
55             }, $dir);
56              
57 8         57 foreach my $absfile (@files) {
58 33         2132 my $relfile = File::Spec->abs2rel( $absfile, $incdir );
59 33         104 push @$list, {
60             path => $relfile,
61             module => _file_to_module( $relfile )
62             };
63             }
64             }
65 9         30 return $list;
66             }
67              
68             sub _file_to_module {
69 33     33   52 my $file = shift;
70 33         136 $file =~ s/\.pm$//;
71 33         362 my @list = File::Spec->splitpath( $file );
72 33         50 shift @list;
73 33         195 return join('::', @list)
74             }
75              
76             sub _module_to_file {
77 9     9   16 my $module = shift;
78 9         64 $module =~ s{::\*?$}{};
79 9         37 $module =~ s{::}{/}g;
80 9         22 return $module;
81             }
82              
83             1;
84              
85             __END__