File Coverage

blib/lib/only/latest.pm
Criterion Covered Total %
statement 42 44 95.4
branch 22 28 78.5
condition 9 12 75.0
subroutine 4 4 100.0
pod 0 1 0.0
total 77 89 86.5


line stmt bran cond sub pod time code
1             # $File: //member/autrijus/only-latest/lib/only/latest.pm $ $Author: autrijus $
2             # $Revision: #1 $ $Change: 8676 $ $only: 2003/11/01 06:14:05 $
3              
4             package only::latest;
5 1     1   6028 use 5.006;
  1         23  
  1         871  
6              
7             $only::latest::VERSION = '0.01';
8              
9             =head1 NAME
10              
11             only::latest - Always use the latest version of a module in @INC
12              
13             =head1 VERSION
14              
15             This document describes version 0.01 of only::latest, released
16             November 4, 2003.
17              
18             =head1 SYNOPSIS
19              
20             use lib "/some/dir";
21             use only::latest;
22             use DBI; # use "/some/dir/DBI.pm" only if it's newer than system's
23              
24             =head1 DESCRIPTION
25              
26             This module is for people with separately-maintained INC directories
27             containing overlapping modules, who wishes to always use the latest version
28             of a module, regardless of the directory it is in.
29              
30             If you C or C a module living in more than one directory,
31             the one with the highest C<$VERSION> is preferred, and its directory will
32             be tried first during the next time. If there is a tie, the first-tried one
33             is used.
34              
35             The implementation puts a hook in front of C<@INC>; this means it should
36             come after all C statements.
37              
38             If you wish to limit this module to some specific targets, list them as
39             the import arguments, like this:
40              
41             use only::latest qw(CGI CGI::Fast);
42             use DBI; # not affected
43              
44             =cut
45              
46             sub import {
47 1     1   8 my ($class, @pkgs) = @_;
48 1         2 my %intercept = map { s{::}{/}g; "$_.pm" => 1 } @pkgs;
  0         0  
  0         0  
49 1         2 my $cur_prefix;
50              
51             unshift @INC, sub {
52 6     6   138780 my ($self, $file) = @_;
53 6 50 33     121 return undef if %intercept and !$intercept{$file};
54              
55 6         17 my ($cur_ver, $cur_file) = (-1, undef);
56 6         15 foreach my $prefix ($cur_prefix, grep { $_ ne $cur_prefix } @INC) {
  78         202  
57 84 100 100     478 next if !defined($prefix) or ref($prefix);
58 72         127 my $pathname = "$prefix/$file";
59 72 100 66     2485 next unless -e $pathname and !-d $pathname;
60 5         43 my $ver = $class->parse_version($pathname);
61 5 50       32 next unless $ver > $cur_ver;
62 5 50       12 $cur_prefix = $prefix if $cur_file; # if it wins, remember it
63 5         13 ($cur_ver, $cur_file) = ($ver, $pathname);
64             }
65              
66 6 100       431 return undef unless $cur_file;
67 5 50       318 open my($fh), $cur_file or return undef;
68 5         2805 return $fh;
69             }
70 1         1895 }
71              
72             # Copied verbatim from ExtUtils::MM_Unix
73             sub parse_version {
74 5     5 0 12 my($self,$parsefile) = @_;
75 5         5 my $result;
76 5         14 local *FH;
77 5         33 local $/ = "\n";
78 5         7 local $_;
79 5 50       290 open(FH,$parsefile) or die "Could not open '$parsefile': $!";
80 5         12 my $inpod = 0;
81 5         285 while () {
82 155 100       631 $inpod = /^=(?!cut)/ ? 1 : /^=cut/ ? 0 : $inpod;
    100          
83 155 100 100     1028 next if $inpod || /^\s*#/;
84 91         107 chop;
85 91 100       403 next unless /(?
86 4         32 my $eval = qq{
87             package ExtUtils::MakeMaker::_version;
88              
89             local $1$2;
90             \$$2=undef; do {
91             $_
92             }; \$$2
93             };
94 4         18 local $^W = 0;
95 4         523 $result = eval($eval);
96 4 50       20 warn "Could not eval '$eval' in $parsefile: $@" if $@;
97 4         12 last;
98             }
99 5         154 close FH;
100              
101 5 100       17 $result = "undef" unless defined $result;
102 5         37 return $result;
103             }
104              
105             1;
106              
107             =head1 AUTHORS
108              
109             Autrijus Tang Eautrijus@autrijus.orgE
110              
111             Part of code derived from L.
112              
113             =head1 COPYRIGHT
114              
115             Copyright 2003 by Autrijus Tang Eautrijus@autrijus.orgE.
116              
117             This program is free software; you can redistribute it and/or
118             modify it under the same terms as Perl itself.
119              
120             See L
121              
122             =cut