| 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 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | $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<use> or C<require> 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<use lib> 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 | 1 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||
| 47 | 1 | my ($class, @pkgs) = @_; | |||||||||||||||||||||||||||
| 48 | 0 | my %intercept = map { s{::}{/}g; "$_.pm" => 1 } @pkgs; | |||||||||||||||||||||||||||
| 49 | 1 | my $cur_prefix; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 22 | ▶ | unshift @INC, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=12 SCAR=25.6
| |||||||||||||||||||||||||||||
| 52 | 22 | my ($self, $file) = @_; | |||||||||||||||||||||||||||
| 53 | 22 | ▶ | return undef if %intercept and !$intercept{$file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
%intercept and not $intercept{$file}
MC/DC0%0/2
%intercept and not $intercept{$file}
%intercept
not $intercept{$file}
Truth table33%1/3
A and not(B)
A= %interceptB= not $intercept{$file}
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 22 | my ($cur_ver, $cur_file) = (-1, undef); | |||||||||||||||||||||||||||
| 56 | 22 | foreach my $prefix ($cur_prefix, grep { $_ ne $cur_prefix } @INC) { | |||||||||||||||||||||||||||
| 57 | 240 | ▶ | next if !defined($prefix) or ref($prefix); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $prefix or ref $prefix
MC/DC100%2/2
not defined $prefix or ref $prefix
not defined $prefix
ref $prefix
Truth table100%3/3
not(A) or B
A= not defined $prefixB= ref $prefix
| |||||||||||||||||||||||||||||
| 58 | 196 | my $pathname = "$prefix/$file"; | |||||||||||||||||||||||||||
| 59 | 196 | ▶ | next unless -e $pathname and !-d $pathname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-e $pathname and not -d $pathname
MC/DC50%1/2
-e $pathname and not -d $pathname
-e $pathname
not -d $pathname
Truth table66%2/3
A and not(B)
A= -e $pathnameB= not -d $pathname
| |||||||||||||||||||||||||||||
| 60 | 20 | my $ver = $class->parse_version($pathname); | |||||||||||||||||||||||||||
| 61 | 20 | ▶ | next unless $ver > $cur_ver; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 20 | ▶ | $cur_prefix = $prefix if $cur_file; # if it wins, remember it | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 20 | ($cur_ver, $cur_file) = ($ver, $pathname); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 22 | ▶ | return undef unless $cur_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 20 | ▶ | open my($fh), $cur_file or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 20 | return $fh; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 1 | } | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # Copied verbatim from ExtUtils::MM_Unix | ||||||||||||||||||||||||||||
| 73 | 20 | ▶ | sub parse_version { | ||||||||||||||||||||||||||
|
Subroutine
parse_version: called CC=10 SCAR=23.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 20 | my($self,$parsefile) = @_; | |||||||||||||||||||||||||||
| 75 | 20 | my $result; | |||||||||||||||||||||||||||
| 76 | 20 | local *FH; | |||||||||||||||||||||||||||
| 77 | 20 | local $/ = "\n"; | |||||||||||||||||||||||||||
| 78 | 20 | local $_; | |||||||||||||||||||||||||||
| 79 | 20 | ▶ | open(FH,$parsefile) or die "Could not open '$parsefile': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 20 | my $inpod = 0; | |||||||||||||||||||||||||||
| 81 | 20 | ▶ | while (<FH>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 277 | ▶ | $inpod = /^=(?!cut)/ ? 1 : /^=cut/ ? 0 : $inpod; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 83 | 277 | ▶ | next if $inpod || /^\s*#/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$inpod or /^\s*#/
MC/DC100%2/2
$inpod or /^\s*#/
$inpod
/^\s*#/
Truth table100%3/3
A or B
A= $inpodB= /^\s*#/
| |||||||||||||||||||||||||||||
| 84 | 160 | chop; | |||||||||||||||||||||||||||
| 85 | 160 | ▶ | next unless /(?<!\\)([\$*])(([\w\:\']*)\bVERSION)\b.*\=/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 19 | my $eval = qq{ | |||||||||||||||||||||||||||
| 87 | package ExtUtils::MakeMaker::_version; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | local $1$2; | ||||||||||||||||||||||||||||
| 90 | \$$2=undef; do { | ||||||||||||||||||||||||||||
| 91 | $_ | ||||||||||||||||||||||||||||
| 92 | }; \$$2 | ||||||||||||||||||||||||||||
| 93 | }; | ||||||||||||||||||||||||||||
| 94 | 19 | local $^W = 0; | |||||||||||||||||||||||||||
| 95 | 19 | $result = eval($eval); | |||||||||||||||||||||||||||
| 96 | 19 | ▶ | warn "Could not eval '$eval' in $parsefile: $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 19 | last; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 20 | close FH; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 20 | ▶ | $result = "undef" unless defined $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 20 | return $result; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1 | 1; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | Autrijus Tang E<lt>autrijus@autrijus.orgE<gt> | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Part of code derived from L<ExtUtils::MM_Unix>. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Copyright 2003 by Autrijus Tang E<lt>autrijus@autrijus.orgE<gt>. | ||||||||||||||||||||||||||||
| 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<http://www.perl.com/perl/misc/Artistic.html> | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||