This file is indexed.

/usr/share/perl5/Module/ExtractUse.pm is in libmodule-extractuse-perl 0.33-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package Module::ExtractUse;

use strict;
use warnings;
use 5.008;

use Pod::Strip;
use Parse::RecDescent 1.967009;
use Module::ExtractUse::Grammar;
use Carp;
use version; our $VERSION=version->new('0.33');

# ABSTRACT: Find out what modules are used

#$::RD_TRACE=1;
#$::RD_HINT=1;



sub new {
    my $class=shift;
    return bless {
        found=>{},
        files=>0,
    },$class;
}


# Regular expression to detect eval
#  On newer perl, you can use named capture groups and (?&name) for recursive regex
#  However, it requires perl newer than 5.008 declared as requirement in this module
my $re_block;
$re_block = qr {
    ( # eval BLOCK, corresponding to the group 10 in the entire regex
        {
            ((?:
                (?> [^{}]+ )  # Non-braces without backtracking
            |
                (??{$re_block}) # Recurse to group 10
            )*)
        }
    )
}xs;
my $re = qr{
    \G(.*?) # group 1
    eval
    (?:
        (?:\s+
            (?:
                qq?\((.*?)\) # eval q(), group 2
                |
                qq?\[(.*?)\] # eval q[], group 3
                |
                qq?{(.*?)}   # eval q{}, group 4
                |
                qq?<(.*?)>   # eval q<>, group 5
                |
                qq?(\S)(.*?)\6 # eval q'' or so, group 6, group 7
            )
        )
        |
        (?:\s*(?:
            (?:(['"])(.*?)\8) # eval '' or eval "", group 8, group 9
            |
            ( # eval BLOCK, group 10
                {
                    ((?: # group 11
                        (?> [^{}]+ )  # Non-braces without backtracking
                    |
                        (??{$re_block}) # Recurse to group 10
                    )*)
                }
            )
        ))
    )
}xs;

sub extract_use {
    my $self=shift;
    my $code_to_parse=shift;

    my $podless;
    my $pod_parser=Pod::Strip->new;
    $pod_parser->output_string(\$podless);
    $pod_parser->parse_characters(1) if $pod_parser->can('parse_characters');
    if (ref($code_to_parse) eq 'SCALAR') {
        $pod_parser->parse_string_document($$code_to_parse);
    }
    else {
        $pod_parser->parse_file($code_to_parse);
    }

    # Strip obvious comments.
    $podless =~ s/(^|[\};])\s*#.*$/$1/mg;

    # Strip __(DATA|END)__ sections.
    $podless =~ s/\n__(?:DATA|END)__\b.*$//s;

    my @statements;
    while($podless =~ /$re/gc) {
    # to keep parsing time short, split code in statements
    # (I know that this is not very exact, patches welcome!)
        my $pre = $1;
        my $eval = join('', grep { defined $_ } ($2, $3, $4, $5, $7, $9, $11));
        push @statements, map { [ 0, $_ ] } split(/;/, $pre); # non-eval context
        push @statements, map { [ 1, $_ ] } split(/;/, $eval); # eval context
    }
    push @statements, map { [ 0, $_ ] } split(/;/, substr($podless, pos($podless) || 0)); # non-eval context

    foreach my $statement_ (@statements) {
        my ($eval, $statement) = @$statement_;
        $statement=~s/\n+/ /gs;
        my $result;

        # now that we've got some code containing 'use' or 'require',
        # parse it! (using different entry point to save some more
        # time)
        my $type;
        if ($statement=~/\buse/) {
            $statement=~s/^(.*?)use\b/use/;
            next if $1 && $1 =~ /->\s*$/;
            eval {
                my $parser=Module::ExtractUse::Grammar->new();
                $result=$parser->token_use($statement.';');
            };
            $type = 'use';
        }
        elsif ($statement=~/\brequire/) {
            $statement=~s/^(.*?)require\b/require/s;
            next if $1 && $1 =~ /->\s*$/;
            eval {
                my $parser=Module::ExtractUse::Grammar->new();
                $result=$parser->token_require($statement.';');
            };
            $type = 'require';
        }
        elsif ($statement=~/\bno/) {
            $statement=~s/^(.*?)no\b/no/s;
            next if $1 && $1 =~ /->\s*$/;
            eval {
                my $parser=Module::ExtractUse::Grammar->new();
                $result=$parser->token_no($statement.';');
            };
            $type = 'no';
        }

        next unless $result;

        foreach (split(/\s+/,$result)) {
            $self->_add($_, $eval, $type) if($_);
        }
    }

    # increment file counter
    $self->_inc_files;

    return $self;
}



sub used {
    my $self=shift;
    my $key=shift;
    return $self->{found}{$key} if ($key);
    return $self->{found};
}


sub used_in_eval {
    my $self=shift;
    my $key=shift;
    return $self->{found_in_eval}{$key} if ($key);
    return $self->{found_in_eval};
}


sub used_out_of_eval {
    my $self=shift;
    my $key=shift;
    return $self->{found_not_in_eval}{$key} if ($key);
    return $self->{found_not_in_eval};
}


sub required {
    my $self=shift;
    my $key=shift;
    return $self->{require}{$key} if ($key);
    return $self->{require};
}


sub required_in_eval {
    my $self=shift;
    my $key=shift;
    return $self->{require_in_eval}{$key} if ($key);
    return $self->{require_in_eval};
}


sub required_out_of_eval {
    my $self=shift;
    my $key=shift;
    return $self->{require_not_in_eval}{$key} if ($key);
    return $self->{require_not_in_eval};
}


sub noed {
    my $self=shift;
    my $key=shift;
    return $self->{no}{$key} if ($key);
    return $self->{no};
}


sub noed_in_eval {
    my $self=shift;
    my $key=shift;
    return $self->{no_in_eval}{$key} if ($key);
    return $self->{no_in_eval};
}


sub noed_out_of_eval {
    my $self=shift;
    my $key=shift;
    return $self->{no_not_in_eval}{$key} if ($key);
    return $self->{no_not_in_eval};
}


sub string {
    my $self=shift;
    my $sep=shift || ' ';
    return join($sep,sort keys(%{$self->{found}}));
}


sub string_in_eval {
    my $self=shift;
    my $sep=shift || ' ';
    return join($sep,sort keys(%{$self->{found_in_eval}}));
}


sub string_out_of_eval {
    my $self=shift;
    my $sep=shift || ' ';
    return join($sep,sort keys(%{$self->{found_not_in_eval}}));
}


sub array {
    return keys(%{shift->{found}})
}


sub array_in_eval {
    return keys(%{shift->{found_in_eval}})
}


sub array_out_of_eval {
    return keys(%{shift->{found_not_in_eval}})
}


sub arrayref { 
    my @a=shift->array;
    return \@a if @a;
    return;
}


sub arrayref_in_eval {
    my @a=shift->array_in_eval;
    return \@a if @a;
    return;
}


sub arrayref_out_of_eval {
    my @a=shift->array_out_of_eval;
    return \@a if @a;
    return;
}


sub files {
    return shift->{files};
}

# Internal Accessor Methods
sub _add {
    my $self=shift;
    my $found=shift;
    my $eval=shift;
    my $type=shift;
    $self->{found}{$found}++;
    $self->{$type}{$found}++;
    if ($eval) {
        $self->{found_in_eval}{$found}++;
        $self->{"${type}_in_eval"}{$found}++;
    } else {
        $self->{found_not_in_eval}{$found}++;
        $self->{"${type}_not_in_eval"}{$found}++;
    }
}

sub _found {
    return shift->{found}
}

sub _inc_files {
    shift->{files}++
}

1;

__END__

=pod

=head1 NAME

Module::ExtractUse - Find out what modules are used

=head1 VERSION

version 0.33

=head1 SYNOPSIS

  use Module::ExtractUse;
  
  # get a parser
  my $p=Module::ExtractUse->new;
  
  # parse from a file
  $p->extract_use('/path/to/module.pm');
  
  # or parse from a ref to a string in memory
  $p->extract_use(\$string_containg_code);
  
  # use some reporting methods
  my $used=$p->used;           # $used is a HASHREF
  print $p->used('strict')     # true if code includes 'use strict'
  
  my @used=$p->array;
  my $used=$p->string;
  
  # you can get optional modules, that is use in eval context, in the same style
  my $used=$p->used_in_eval;           # $used is a HASHREF
  print $p->used_in_eval('strict')     # true if code includes 'use strict'
  
  my @used=$p->array_in_eval;
  my $used=$p->string_in_eval;
  
  # and mandatory modules, that is use out of eval context, in the same style, also.
  my $used=$p->used_out_of_eval;           # $used is a HASHREF
  print $p->used_out_of_eval('strict')     # true if code includes 'use strict'
  
  my @used=$p->array_out_of_eval;
  my $used=$p->string_out_of_eval;

=head1 DESCRIPTION

Module::ExtractUse is basically a Parse::RecDescent grammar to parse
Perl code. It tries very hard to find all modules (whether pragmas,
Core, or from CPAN) used by the parsed code.

"Usage" is defined by either calling C<use> or C<require>.

=head2 Methods

=head3 new

 my $p=Module::ExtractUse->new;

Returns a parser object

=head3 extract_use

  $p->extract_use('/path/to/module.pm');
  $p->extract_use(\$string_containg_code);

Runs the parser.

C<$code_to_parse> can be either a SCALAR, in which case
Module::ExtractUse tries to open the file specified in
$code_to_parse. Or a reference to a SCALAR, in which case
Module::ExtractUse assumes the referenced scalar contains the source
code.

The code will be stripped from POD (using Pod::Strip) and split on ";"
(semicolon). Each statement (i.e. the stuff between two semicolons) is
checked by a simple regular expression.

If the statement contains either 'use' or 'require', the statement is
handed over to the parser, who then tries to figure out, B<what> is
used or required. The results will be saved in a data structure that
you can examine afterwards.

You can call C<extract_use> several times on different files. It will
count how many files where examined and how often each module was used.

=head2 Accessor Methods

Those are various ways to get at the result of the parse.

Note that C<extract_use> returns the parser object, so you can say

  print $p->extract_use($code_to_parse)->string;

=head3 used

    my $used=$p->used;           # $used is a HASHREF
    print $p->used('strict')     # true if code includes 'use strict'

If called without an argument, returns a reference to an hash of all
used modules. Keys are the names of the modules, values are the number
of times they were used.

If called with an argument, looks up the value of the argument in the
hash and returns the number of times it was found during parsing.

This is the preferred accessor.

=head3 used_in_eval

Same as C<used>, except for considering in-eval-context only.

=head3 used_out_of_eval

Same as C<used>, except for considering NOT-in-eval-context only.

=head3 required

Same as C<used>, except for considering 'require'd modules only.

=head3 required_in_eval

Same as C<required>, except for considering in-eval-context only.

=head3 required_out_of_eval

Same as C<required>, except for considering NOT-in-eval-context only.

=head3 noed

Same as C<used>, except for considering 'no'ed modules only.

=head3 noed_in_eval

Same as C<noed>, except for considering in-eval-context only.

=head3 noed_out_of_eval

Same as C<noed>, except for considering NOT-in-eval-context only.

=head3 string

    print $p->string($seperator)

Returns a sorted string of all used modules, joined using the value of
C<$seperator> or using a blank space as a default;

Module names are sorted by ascii value (i.e by C<sort>)

=head3 string_in_eval

Same as C<string>, except for considering in-eval-context only.

=head3 string_out_of_eval

Same as C<string>, except for considering NOT-in-eval-context only.

=head3 array

    my @array = $p->array;

Returns an array of all used modules.

=head3 array_in_eval

Same as C<array>, except for considering in-eval-context only.

=head3 array_out_of_eval

Same as C<array>, except for considering NOT-in-eval-context only.

=head3 arrayref

    my $arrayref = $p->arrayref;

Returns a reference to an array of all used modules. Surprise!

=head3 arrayref_in_eval

Same as C<array_ref>, except for considering in-eval-context only.

=head3 arrayref_out_of_eval

Same as C<array_ref>, except for considering NOT-in-eval-context only.

=head3 files

Returns the number of files parsed by the parser object.

=head1 RE-COMPILING THE GRAMMAR

If - for some reasons - you need to alter the grammar, edit the file
F<grammar> and afterwards run:

  perl -MParse::RecDescent - grammar Module::ExtractUse::Grammar

Make sure you're in the right directory, i.e. in F<.../Module/ExtractUse/>

=head1 EXPORTS

Nothing.

=head1 SEE ALSO

Parse::RecDescent, Module::ScanDeps, Module::Info, Module::CPANTS::Analyse

=head1 AUTHORS

=over 4

=item *

Thomas Klausner <domm@cpan.org>

=item *

Kenichi Ishigaki <kishigaki@gmail.com>

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Thomas Klausner.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut