1Module::ExtractUse(3) User Contributed Perl DocumentationModule::ExtractUse(3)
2
3
4
6 Module::ExtractUse - Find out what modules are used
7
9 version 0.343
10
12 use Module::ExtractUse;
13
14 # get a parser
15 my $p=Module::ExtractUse->new;
16
17 # parse from a file
18 $p->extract_use('/path/to/module.pm');
19
20 # or parse from a ref to a string in memory
21 $p->extract_use(\$string_containg_code);
22
23 # use some reporting methods
24 my $used=$p->used; # $used is a HASHREF
25 print $p->used('strict') # true if code includes 'use strict'
26
27 my @used=$p->array;
28 my $used=$p->string;
29
30 # you can get optional modules, that is use in eval context, in the same style
31 my $used=$p->used_in_eval; # $used is a HASHREF
32 print $p->used_in_eval('strict') # true if code includes 'use strict'
33
34 my @used=$p->array_in_eval;
35 my $used=$p->string_in_eval;
36
37 # and mandatory modules, that is use out of eval context, in the same style, also.
38 my $used=$p->used_out_of_eval; # $used is a HASHREF
39 print $p->used_out_of_eval('strict') # true if code includes 'use strict'
40
41 my @used=$p->array_out_of_eval;
42 my $used=$p->string_out_of_eval;
43
45 Module::ExtractUse is basically a Parse::RecDescent grammar to parse
46 Perl code. It tries very hard to find all modules (whether pragmas,
47 Core, or from CPAN) used by the parsed code.
48
49 "Usage" is defined by either calling "use" or "require".
50
51 Methods
52 new
53
54 my $p=Module::ExtractUse->new;
55
56 Returns a parser object
57
58 extract_use
59
60 $p->extract_use('/path/to/module.pm');
61 $p->extract_use(\$string_containg_code);
62
63 Runs the parser.
64
65 $code_to_parse can be either a SCALAR, in which case Module::ExtractUse
66 tries to open the file specified in $code_to_parse. Or a reference to a
67 SCALAR, in which case Module::ExtractUse assumes the referenced scalar
68 contains the source code.
69
70 The code will be stripped from POD (using Pod::Strip) and split on ";"
71 (semicolon). Each statement (i.e. the stuff between two semicolons) is
72 checked by a simple regular expression.
73
74 If the statement contains either 'use' or 'require', the statement is
75 handed over to the parser, who then tries to figure out, what is used
76 or required. The results will be saved in a data structure that you can
77 examine afterwards.
78
79 You can call "extract_use" several times on different files. It will
80 count how many files where examined and how often each module was used.
81
82 Accessor Methods
83 Those are various ways to get at the result of the parse.
84
85 Note that "extract_use" returns the parser object, so you can say
86
87 print $p->extract_use($code_to_parse)->string;
88
89 used
90
91 my $used=$p->used; # $used is a HASHREF
92 print $p->used('strict') # true if code includes 'use strict'
93
94 If called without an argument, returns a reference to an hash of all
95 used modules. Keys are the names of the modules, values are the number
96 of times they were used.
97
98 If called with an argument, looks up the value of the argument in the
99 hash and returns the number of times it was found during parsing.
100
101 This is the preferred accessor.
102
103 used_in_eval
104
105 Same as "used", except for considering in-eval-context only.
106
107 used_out_of_eval
108
109 Same as "used", except for considering NOT-in-eval-context only.
110
111 required
112
113 Same as "used", except for considering 'require'd modules only.
114
115 required_in_eval
116
117 Same as "required", except for considering in-eval-context only.
118
119 required_out_of_eval
120
121 Same as "required", except for considering NOT-in-eval-context only.
122
123 noed
124
125 Same as "used", except for considering 'no'ed modules only.
126
127 noed_in_eval
128
129 Same as "noed", except for considering in-eval-context only.
130
131 noed_out_of_eval
132
133 Same as "noed", except for considering NOT-in-eval-context only.
134
135 string
136
137 print $p->string($seperator)
138
139 Returns a sorted string of all used modules, joined using the value of
140 $seperator or using a blank space as a default;
141
142 Module names are sorted by ascii value (i.e by "sort")
143
144 string_in_eval
145
146 Same as "string", except for considering in-eval-context only.
147
148 string_out_of_eval
149
150 Same as "string", except for considering NOT-in-eval-context only.
151
152 array
153
154 my @array = $p->array;
155
156 Returns an array of all used modules.
157
158 array_in_eval
159
160 Same as "array", except for considering in-eval-context only.
161
162 array_out_of_eval
163
164 Same as "array", except for considering NOT-in-eval-context only.
165
166 arrayref
167
168 my $arrayref = $p->arrayref;
169
170 Returns a reference to an array of all used modules. Surprise!
171
172 arrayref_in_eval
173
174 Same as "array_ref", except for considering in-eval-context only.
175
176 arrayref_out_of_eval
177
178 Same as "array_ref", except for considering NOT-in-eval-context only.
179
180 files
181
182 Returns the number of files parsed by the parser object.
183
185 If - for some reasons - you need to alter the grammar, edit the file
186 grammar and afterwards run:
187
188 perl -MParse::RecDescent - grammar Module::ExtractUse::Grammar
189
190 Make sure you're in the right directory, i.e. in .../Module/ExtractUse/
191
193 Nothing.
194
196 Parse::RecDescent, Module::Extract::Use, Module::ScanDeps,
197 Module::Info, Module::CPANTS::Analyse, Perl::PrereqScanner,
198 Perl::PrereqScanner::Lite, Perl::PrereqScanner::NotQuiteLite
199
201 · Anthony Brummett <https://github.com/brummett> implemented support
202 for Module::Runtime and Class::Load while participating in the CPAN
203 Pull Request Challenge <http://cpan-prc.org/>
204
205 · Jeremy Mates <https://github.com/thrig> fixed some documentation
206 errors
207
208 · Jonathan Yu provided a nice script, "example/extractuse.pl"
209
210 If I forgot to mention your contribution, please send an email or open
211 an issue / ticket.
212
214 · Thomas Klausner <domm@cpan.org>
215
216 · Kenichi Ishigaki <kishigaki@gmail.com>
217
219 This software is copyright (c) 2014 by Thomas Klausner.
220
221 This is free software; you can redistribute it and/or modify it under
222 the same terms as the Perl 5 programming language system itself.
223
224
225
226perl v5.30.1 2020-01-30 Module::ExtractUse(3)