File Coverage

lib/Devel/SharedLibs.pm
Criterion Covered Total %
statement 17 18 94.4
branch 1 2 50.0
condition n/a
subroutine 5 5 100.0
pod n/a
total 23 25 92.0


line stmt bran cond sub pod time code
1             ########################################################################
2             # housekeeping
3             ########################################################################
4              
5             package Devel::SharedLibs;
6 1     1   655 use v5.22;
  1         3  
7              
8 1     1   738 use IO::File;
  1         8394  
  1         210  
9              
10 1     1   843 use List::MoreUtils qw( uniq );
  1         11887  
  1         8  
11              
12             ########################################################################
13             # package variables
14             ########################################################################
15              
16             our $VERSION = 'v0.3.2';
17             $VERSION = eval "$VERSION";
18              
19             sub import
20 0           {
21             state $usable_ldd
22             = do
23 1     1   10 {
24 1         4 $DB::single = 1;
25              
26             # Aside: Need to check whether ldd on BSD, etc, supports
27             # long option for version.
28              
29 1         54245 my $result = qx{ ldd --version 2>&1 };
30              
31 1 50       44 my $success = ! $?
32             or warn <<"END";
33             # Non-zeo exit: ldd --version ($result)
34             # SharedLibs unable to generate output.
35             END
36              
37 1         17 $success
38             };
39              
40             # all this does it localize $path for the END closure.
41             # kwikhak for the moment until I find out if there is
42             # any decent way to deal with ldd on BSD.
43              
44 1         87 my ( undef, $path ) = @_;
45              
46             END
47             {
48             $DB::single = 1;
49              
50             $ENV{ DEVEL_SHAREDLIBS_PRINT }
51             or return;
52              
53             $path ||= $ENV{ DEVEL_SHAREDLIBS_PATH }
54             or say "# False path & DEVEL_SHAREDLIBS_PATH: output to stdout.";
55              
56             # avoid stale output.
57             # if ldd is not usable then this leaves nothing behind.
58              
59             my $continue = $usable_ldd;
60              
61             if( $path && -e $path )
62             {
63             say "# Remove stale: '$path'";
64              
65             unlink $path
66             or do
67             {
68             warn <<"END";
69             # Failed unlink: '$path', no SharedLibs output.
70             # SharedLibs unable to generate output.
71             END
72              
73             $continue = ''
74             };
75             }
76              
77             if( $continue )
78             {
79 1     1   1683 use autodie qw( open close );
  1         17894  
  1         6  
80              
81             local $, = "\n";
82             local $\ = "\n";
83              
84             my $fh
85             = $path
86             ? IO::File->new( $path, 'w' )
87             : *STDOUT{ IO }
88             ;
89              
90             # sort isn't necessary for uniqueness but helps
91             # by giving a stable output.
92              
93             print $fh
94             "# ldd '$^X', '$0'" =>
95             sort { $a cmp $b }
96             uniq
97             map
98             {
99             # the literal '=>' dodges linux-vdso & friends,
100             # which lack a path and also the
101             # "not a dynamic executable" messages from
102             # most of the contents.
103              
104             my ( $lib ) = m{ => \s+ (\S+) }x;
105              
106             $lib || ()
107             }
108             map
109             {
110             split "\n" => qx(ldd $_ 2>/dev/null )
111             }
112             (
113             $^X,
114             values %INC
115             );
116              
117             close $fh;
118              
119             say STDERR "SharedLibs output: '$path'";
120             }
121             else
122             {
123             say STDERR "SharedLibs unable to generate output."
124             }
125             }
126             }
127              
128             # keep require happy
129             1
130             __END__