blib/lib/Catmandu/Importer/Modules.pm

filter: Statementstmt 100% 33 / 33 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 97.7% 43 / 44 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Catmandu/Fix/copy_field.pm ↑ Summary blib/lib/Catmandu/Fix/assoc.pm »
1 package Catmandu::Importer::Modules;
2  
3 2 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '1.0201_02';
6  
7 2 ▶use Module::Info;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Catmandu::Util qw(array_split pod_section);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 with 'Catmandu::Importer';
15  
16 has inc => (
17 is => 'ro',
18 lazy => 1,
19 1 ▶ default => sub {[@INC]},
Subroutine __ANON__: called CC=1 SCAR=0.0
20 2 coerce => \&array_split,
21 );
22  
23 has namespace =>
24 1 ▶ (is => 'ro', default => sub {[""]}, coerce => \&array_split,);
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 2 has max_depth => (is => 'ro', predicate => 1,);
27  
28 2 has pattern => (is => 'ro',);
29  
30 2 has primary => (is => 'ro',);
31  
32 sub generator {
33 10 ▶ my ($self) = @_;
Subroutine generator: called CC=1 SCAR=0.0
Pod undocumented
34  
35 sub {
36 state $pattern = $self->pattern;
37 state $files = {};
38 state $names = {};
39  
40 # array of [ $directory => $namespace ]
41 state $search = [
42 map {
43 my $ns = $_;
44 my $parts = [map {grep length, split(/::/, $_)} $ns];
45 map {[File::Spec->catdir($_, @$parts) => $ns]} @{$self->inc};
46 } @{$self->namespace}
47 ];
48  
49 state $cur = shift(@$search) // return;
50  
51 state $rule = do {
52 my $r = File::Find::Rule->new->file->name('*.pm');
53 $r->maxdepth($self->max_depth) if $self->has_max_depth;
54 $r->start($cur->[0]);
55 };
56  
57 while (1) {
58 my ($dir, $ns) = @$cur;
59  
60 if (defined(my $file = $rule->match)) {
61 my $path = File::Spec->abs2rel($file, $dir);
62 my $name = join('::', File::Spec->splitdir($path));
63 $name =~ s/\.pm$//;
64 $name = join('::', $ns, $name) if $ns;
65  
66 next if defined $pattern && $name !~ $pattern;
67  
68 my $info = Module::Info->new_from_file($file);
69 my $file = File::Spec->rel2abs($file);
70  
71 next if $files->{$file};
72 $files->{$file} = 1;
73  
74 if ($self->primary) {
75 next if $names->{$name};
76 $names->{$name} = 1;
77 }
78  
79 my $data = {file => $file, name => $name, path => $dir,};
80 $data->{version} = "" . $info->version
81 if defined $info->version;
82  
83 my $about = pod_section($file, 'NAME');
84 $about =~ s/^[^-]+(\s*-?\s*)?|\n.*$//sg;
85 $data->{about} = $about if $about ne '';
86  
87 return $data;
88 }
89 else {
90 $cur = shift(@$search) // return;
91 $rule->start($cur->[0]);
92 }
93 }
94 10 };
95 }
96  
97 2 1;
98  
99 __END__
100  
101 =pod
102  
103 =head1 NAME
104  
105 Catmandu::Importer::Modules - list installed perl modules in a given namespace
106  
107 =head1 DESCRIPTION
108  
109 This L<Catmandu::Importer> list perl modules from all perl library paths with
110 their C<name>, C<version>, absolute C<file>, library C<path>, and short
111 description (C<about>).
112  
113 =head1 CONFIGURATION
114  
115 =over
116  
117 =item file
118  
119 Read input from a local file given by its path. Alternatively a scalar
120 reference can be passed to read from a string.
121  
122 =item fh
123  
124 Read input from an L<IO::Handle>. If not specified, L<Catmandu::Util::io> is used to
125 create the input stream from the C<file> argument or by using STDIN.
126  
127 =item encoding
128  
129 Binmode of the input stream C<fh>. Set to C<:utf8> by default.
130  
131 =item fix
132  
133 An ARRAY of one or more fixes or file scripts to be applied to imported items.
134  
135 =item namespace
136  
137 Namespace(s) for the modules to list, given as array or comma-separated list
138  
139 =item inc
140  
141 List of library paths (defaults to C<@INC>)
142  
143 =item max_depth
144  
145 Maximum depth to recurse into the namespace e.g. if the namespace is
146 Catmandu::Fix then Catmandu::Fix::add_field has a depth of 1 and
147 Catmandu::Fix::Condition::exists a depth of 2
148  
149 =item pattern
150  
151 Filter modules by the given regex pattern
152  
153 =item primary
154  
155 Filter modules to the first module of each name
156  
157 =back
158  
159 =head1 METHODS
160  
161 Every L<Catmandu::Importer> is a L<Catmandu::Iterable> all its methods are
162 inherited.
163  
164 =head1 SEE ALSO
165  
166 L<Catmandu::Importer::CPAN>, L<Catmandu::Cmd::info>
167  
168 =cut
« blib/lib/Catmandu/Fix/copy_field.pm ↑ Summary blib/lib/Catmandu/Fix/assoc.pm »