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