blib/script/scandeps.pl untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 201 CC 1 SCAR 6.9
CC
1
Cov
0%
_name
2.0
_modtree
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2  
3 $VERSION = '0.76';
4  
5 use strict;
6 use Config;
7 use Getopt::Long qw(:config bundling no_ignore_case);
8 use Module::ScanDeps;
9 use ExtUtils::MakeMaker;
10 use subs qw( _name _modtree );
11  
12 my $usage = "Usage: $0 [ -B ] [ -V ] [ -T ] [ -x [ --xargs STRING ] | -c ] [ -R ] [-C FILE ] [ -e STRING | FILE ... ]\n";
13  
14 my %opts;
15 GetOptions(\%opts,
16 "B|bundle",
17 "C|cachedeps=s",
18 "c|compile",
19 "e|eval=s",
20 "xargs=s",
21 "R|no-recurse",
22 "T|modtree",
23 "V|verbose",
24 "x|execute",
25 ) or die $usage;
26  
27 my (%map, %skip);
28 my $core = $opts{B};
29 my $verbose = $opts{V};
30 my $eval = $opts{e};
31 my $recurse = $opts{R} ? 0 : 1;
32 my $modtree = {} unless $opts{T}; # i.e. disable it unless explicitly requested
33  
34 if ($eval) {
35 require File::Temp;
36 my ($fh, $filename) = File::Temp::tempfile( UNLINK => 1 );
37 print $fh $eval, "\n" or die $!;
38 close $fh;
39 push @ARGV, $filename;
40 }
41  
42 if ($opts{x} && defined $opts{xargs}) {
43 require Text::ParseWords;
44 $opts{x} = [ Text::ParseWords::shellwords($opts{xargs}) ];
45 }
46  
47 die $usage unless @ARGV;
48  
49 my @files = @ARGV;
50 while (<>) {
51 next unless /^package\s+([\w:]+)/;
52 $skip{$1}++;
53 }
54  
55 my $map = scan_deps(
56 files => \@files,
57 recurse => $recurse,
58 $opts{x} ? ( execute => $opts{x} ) :
59 $opts{c} ? ( compile => 1 ) : (),
60 $opts{V} ? ( warn_missing => 1 ) : (),
61 $opts{C} ? ( cache_file => $opts{C}) : (),
62 );
63  
64  
65 my $len = 0;
66 my @todo;
67 my (%seen, %dist, %core, %bin);
68  
69 foreach my $key (sort keys %$map) {
70 my $mod = $map->{$key};
71 my $name = $mod->{name} = _name($key);
72  
73 print "# $key [$mod->{type}]\n" if $verbose;
74  
75 if ($mod->{type} eq 'shared') {
76 $key =~ s!auto/!!;
77 $key =~ s!/[^/]+$!!;
78 $key =~ s!/!::!;
79 $bin{$key}++;
80 }
81  
82 next unless $mod->{type} eq 'module';
83  
84 next if $skip{$name};
85  
86 my $privPath = "$Config{privlibexp}/$key";
87 my $archPath = "$Config{archlibexp}/$key";
88 $privPath =~ s|\\|\/|og;
89 $archPath =~ s|\\|\/|og;
90 if ($mod->{file} eq $privPath
91 or $mod->{file} eq $archPath) {
92 next unless $core;
93  
94 $core{$name}++;
95 }
96 elsif (my $dist = _modtree->{$name}) {
97 $seen{$name} = $dist{$dist->package}++;
98 }
99  
100 $len = length($name) if $len < length($name);
101 $mod->{used_by} ||= [];
102  
103 push @todo, $mod;
104 }
105  
106 $len += 2;
107  
108 print "#\n# Legend: [C]ore [X]ternal [S]ubmodule [?]NotOnCPAN\n" if $verbose;
109  
110 foreach my $mod (sort { $a->{name} cmp $b->{name} } @todo ) {
111 my $version = MM->parse_version($mod->{file});
112  
113 if (!$verbose) {
114 printf "%-${len}s => '$version',", "'$mod->{name}'" if $version;
115 } else {
116 printf "%-${len}s => '0', # ", "'$mod->{name}'";
117 my @used_by = sort map(_name($_), @{$mod->{used_by}});
118 print $seen{$mod->{name}} ? 'S' : ' ';
119 print $bin{$mod->{name}} ? 'X' : ' ';
120 print $core{$mod->{name}} ? 'C' : ' ';
121 print _modtree && !_modtree->{$mod->{name}} ? '?' : ' ';
122 print " # ";
123 print "@used_by" if @used_by;
124 }
125 print "\n";
126  
127 }
128  
129 warn "No modules found!\n" unless @todo;
130  
131 sub _name {
132 my $str = shift;
133 $str =~ s!/!::!g;
134 $str =~ s!.pm$!!i;
135 $str =~ s!^auto::(.+)::.*!$1!;
136 return $str;
137 }
138  
139 sub _modtree {
140 $modtree ||= eval {
141 require CPANPLUS::Backend;
142 CPANPLUS::Backend->new->module_tree;
143 } || {};
144 }
145  
146  
147 1;
148  
149 __END__