File Coverage

lib/ex/newest.pm
Criterion Covered Total %
statement 46 50 92.0
branch 11 14 78.5
condition 2 5 40.0
subroutine 9 9 100.0
pod 0 2 0.0
total 68 80 85.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             ex::newest - look for newest versions of modules installed several times
4              
5             =cut
6              
7             package ex::newest;
8              
9             {
10             local $^W = 0;
11             require v5.7.2;
12             }
13 3     3   54792 use strict;
  3         8  
  3         111  
14 3     3   15 use warnings;
  3         5  
  3         111  
15 3     3   13 no warnings 'redefine';
  3         10  
  3         415  
16              
17 3     3   4803 use ExtUtils::MM_Unix;
  3         490869  
  3         1750  
18              
19             our $VERSION = '0.02';
20              
21             sub ex::newest::INC {
22 14     14 0 230479 my ($self, $file) = @_;
23 14 50       70 return undef if substr($file,-3) ne '.pm'; # Handle only modules
24 14         30 my $package = substr($file,0,-3);
25 14         43 $package =~ s,/,::,g;
26 14         22 my @found;
27             my $path;
28 14         28 for my $dir (@INC) {
29 175 100       299 next if ref $dir;
30 161         238 $path = "$dir/$file";
31 161 100 66     3346 push @found, $path if -f $path && -r _;
32             }
33 14 100       656 return undef if not @found;
34 12 100       45 if (@found == 1) {
35 10         21 $path = $found[0];
36             } else {
37 2         5 my @versions = ();
38 2         6 for my $f (@found) {
39 2   0     13 my $version = ExtUtils::MM_Unix::parse_version(undef,$f) || 0;
40 0         0 push @versions, [ $version, $f ];
41             }
42 0         0 @versions = sort { $b->[0] <=> $a->[0] } @versions;
  0         0  
43 0         0 $path = $versions[0][1];
44             }
45 10 50       506 open my $fh, $path
46             or return undef; # Fallback to the regular mechanism
47 10         32 $INC{$file} = $path;
48 10         7098 return $fh;
49             }
50              
51             sub rm_hook () {
52 6     6 0 11 @INC = grep { ref ne __PACKAGE__ } @INC;
  76         170  
53             }
54              
55             # We override lib.pm to ensure that the ex::newest object
56             # remains in front of @INC.
57              
58             sub lib::import {
59 3     3   21 rm_hook;
60 3         10 local *lib::import;
61 3         10 local $^W = 0; # Silent non-lexical warnings
62 3         9 delete $INC{'lib.pm'};
63 3         2068 require lib;
64 3         2056 import lib @_[1..$#_];
65 3         380 import ex::newest;
66             }
67              
68             sub lib::unimport {
69             rm_hook;
70             local *lib::unimport;
71             local $^W = 0;
72             delete $INC{'lib.pm'};
73             require lib;
74             unimport lib @_[1..$#_];
75             import ex::newest;
76             }
77              
78             sub import {
79 8 50   8   37 unshift @INC, bless {} unless grep { ref eq __PACKAGE__ } @INC;
  93         169  
80 8         292 $INC{'lib.pm'} = __FILE__;
81             }
82              
83             sub unimport {
84 1     1   8 rm_hook;
85             # Restore lib.pm
86 1         4 local $^W = 0;
87 1         2 delete $INC{'lib.pm'};
88 1         292 require lib;
89             }
90              
91             1;
92             __END__