1Module::Extract::Use(3)User Contributed Perl DocumentatioMnodule::Extract::Use(3)
2
3
4

NAME

6       Module::Extract::Use - Discover the modules a module explicitly uses
7

SYNOPSIS

9               use Module::Extract::Use;
10
11               my $extor = Module::Extract::Use->new;
12
13               my @modules = $extor->get_modules( $file );
14               if( $extor->error ) { ... }
15
16               my $details = $extor->get_modules_with_details( $file );
17               foreach my $detail ( @$details ) {
18                       printf "%s %s imports %s\n",
19                               $detail->module, $detail->version,
20                               join ' ', @{ $detail->imports }
21                       }
22

DESCRIPTION

24       Extract the names of the modules used in a file using a static
25       analysis. Since this module does not run code, it cannot find dynamic
26       uses of modules, such as "eval "require $class"". It only reports
27       modules that the file loads directly or are in the import lists for
28       parent or base.
29
30       The module can handle the conventional inclusion of modules with either
31       "use" or "require" as the statement:
32
33               use Foo;
34               require Foo;
35
36               use Foo 1.23;
37               use Foo qw(this that);
38
39       It now finds "require" as an expression, which is useful to lazily load
40       a module once (and may be faster):
41
42               sub do_something {
43                       state $rc = require Foo;
44                       ...
45                       }
46
47       Additionally, it finds module names used with "parent" and "base",
48       either of which establish an inheritance relationship:
49
50               use parent qw(Foo);
51               use base qw(Foo);
52
53       In the case of namespaces found in "base" or "parent", the value of the
54       "direct" method is false. In all other cases, it is true. You can then
55       skip those namespaces:
56
57               my $details = $extor->get_modules_with_details( $file );
58               foreach my $detail ( @$details ) {
59                       next unless $detail->direct;
60
61                       ...
62                       }
63
64       This module does not discover runtime machinations to load something,
65       such as string evals:
66
67               eval "use Foo";
68
69               my $bar = 'Bar';
70               eval "use $bar";
71
72       If you want that, you might consider Module::ExtractUse (a confusingly
73       similar name).
74
75       new Makes an object. The object doesn't do anything just yet, but you
76           need it to call the methods.
77
78       init
79           Set up the object. You shouldn't need to call this yourself.
80
81       get_modules( FILE )
82           Returns a list of namespaces explicity use-d in FILE. Returns the
83           empty list if the file does not exist or if it can't parse the
84           file.
85
86           Each used namespace is only in the list even if it is used multiple
87           times in the file. The order of the list does not correspond to
88           anything so don't use the order to infer anything.
89
90       get_modules_with_details( FILE )
91           Returns a list of hash references, one reference for each namespace
92           explicitly use-d in FILE. Each reference has keys for:
93
94                   namespace - the namespace, always defined
95                   version   - defined if a module version was specified
96                   imports   - an array reference to the import list
97                   pragma    - true if the module thinks this namespace is a pragma
98                   direct    - false if the module name came from parent or base
99
100           Each used namespace is only in the list even if it is used multiple
101           times in the file. The order of the list does not correspond to
102           anything so don't use the order to infer anything.
103
104       error
105           Return the error from the last call to "get_modules".
106

TO DO

SEE ALSO

109       Module::ScanDeps, Module::Extract
110

SOURCE AVAILABILITY

112       The source code is in Github:
113
114               https://github.com/briandfoy/module-extract-use
115

AUTHOR

117       brian d foy, "<bdfoy@cpan.org>"
118
120       Copyright © 2008-2020, brian d foy "<bdfoy@cpan.org>". All rights
121       reserved.
122
123       This project is under the Artistic License 2.0.
124
125
126
127perl v5.32.0                      2020-07-28           Module::Extract::Use(3)
Impressum