1Module::ExtractUse(3) User Contributed Perl DocumentationModule::ExtractUse(3)
2
3
4
6 Module::ExtractUse - Find out what modules are used
7
9 use Module::ExtractUse;
10
11 # get a parser
12 my $p=Module::ExtractUse->new;
13
14 # parse from a file
15 $p->extract_use('/path/to/module.pm');
16
17 # or parse from a ref to a string in memory
18 $p->extract_use(\$string_containg_code);
19
20 # use some reporting methods
21 my $used=$p->used; # $used is a HASHREF
22 print $p->used('strict') # true if code includes 'use strict'
23
24 my @used=$p->array;
25 my $used=$p->string;
26
28 Module::ExtractUse is basically a Parse::RecDescent grammar to parse
29 Perl code. It tries very hard to find all modules (whether pragmas,
30 Core, or from CPAN) used by the parsed code.
31
32 "Usage" is defined by either calling "use" or "require".
33
34 Methods
35 new
36
37 my $p=Module::ExtractUse->new;
38
39 Returns a parser object
40
41 extract_use
42
43 $p->extract_use('/path/to/module.pm');
44 $p->extract_use(\$string_containg_code);
45
46 Runs the parser.
47
48 $code_to_parse can be either a SCALAR, in which case Module::ExtractUse
49 tries to open the file specified in $code_to_parse. Or a reference to a
50 SCALAR, in which case Module::ExtractUse assumes the referenced scalar
51 contains the source code.
52
53 The code will be stripped from POD (using Pod::Strip) and splitted on
54 ";" (semicolon). Each statement (i.e. the stuff between two semicolons)
55 is checked by a simple regular expression.
56
57 If the statement contains either 'use' or 'require', the statment is
58 handed over to the parser, who then tries to figure out, what is used
59 or required. The results will be saved in a data structure that you can
60 examine afterwards.
61
62 You can call "extract_use" several times on different files. It will
63 count how many files where examined and how often each module was used.
64
65 Accessor Methods
66 Those are various ways to get at the result of the parse.
67
68 Note that "extract_use" returns the parser object, so you can say
69
70 print $p->extract_use($code_to_parse)->string;
71
72 used
73
74 my $used=$p->used; # $used is a HASHREF
75 print $p->used('strict') # true if code includes 'use strict'
76
77 If called without an argument, returns a reference to an hash of all
78 used modules. Keys are the names of the modules, values are the number
79 of times they were used.
80
81 If called with an argument, looks up the value of the argument in the
82 hash and returns the number of times it was found during parsing.
83
84 This is the prefered accessor.
85
86 string
87
88 print $p->string($seperator)
89
90 Returns a sorted string of all used modules, joined using the value of
91 $seperator or using a blank space as a default;
92
93 Module names are sorted by ascii value (i.e by "sort")
94
95 array
96
97 my @array = $p->array;
98
99 Returns an array of all used modules.
100
101 arrayref
102
103 my $arrayref = $p->arrayref;
104
105 Returns a reference to an array of all used modules. Surprise!
106
107 files
108
109 Returns the number of files parsed by the parser object.
110
112 If - for some reasons - you need to alter the grammar, edit the file
113 grammar and afterwards run:
114
115 perl -MParse::RecDescent - grammar Module::ExtractUse::Grammar
116
117 Make sure you're in the right directory, i.e. in .../Module/ExtractUse/
118
120 Nothing.
121
123 Parse::RecDescent, Module::ScanDeps, Module::Info,
124 Module::CPANTS::Analyse
125
127 Thomas Klausner <domm@zsi.at>
128
130 Please report any bugs or feature requests to
131 "bug-module-extractuse@rt.cpan.org", or through the web interface at
132 <http://rt.cpan.org>. I will be notified, and then you'll
133 automatically be notified of progress on your bug as I make changes.
134
136 Module::ExtractUse is Copyright (c) 2003-2008, Thomas Klausner.
137
138 You may use and distribute this module according to the same terms that
139 Perl is distributed under.
140
141
142
143perl v5.12.0 2010-05-07 Module::ExtractUse(3)