File Coverage

blib/lib/recommended.pm
Criterion Covered Total %
statement 38 40 95.0
branch 12 16 75.0
condition 3 3 100.0
subroutine 8 9 88.8
pod 1 1 100.0
total 62 69 89.8


line stmt bran cond sub pod time code
1 1     1   14231 use 5.008001;
  1         3  
  1         65  
2 1     1   5 use strict;
  1         6  
  1         34  
3 1     1   3 use warnings;
  1         1  
  1         34  
4              
5             package recommended;
6             # ABSTRACT: Load recommended modules on demand when available
7              
8 1     1   374 use version;
  1         1226  
  1         4  
9 1     1   47 use Carp ();
  1         1  
  1         13  
10 1     1   423 use Module::Runtime 0.014 (); # bugfixes for use_package_optimistically
  1         1187  
  1         244  
11              
12             our $VERSION = '0.003';
13              
14             # $MODULES{$type}{$caller}{$mod} = [$min_version, $satisfied]
15             my %MODULES;
16              
17             # for testing and diagnostics
18 0     0   0 sub __modules { return \%MODULES }
19              
20             sub import {
21 4     4   254 my $class = shift;
22 4         6 my $caller = caller;
23 4         6 for my $arg (@_) {
24 6         7 my $type = ref $arg;
25 6 100       9 if ( !$type ) {
    50          
26 5         1595 $MODULES{$class}{$caller}{$arg} = [ 0, undef ];
27             }
28             elsif ( $type eq 'HASH' ) {
29 1         12 while ( my ( $mod, $ver ) = each %$arg ) {
30 2 50       5 Carp::croak("module '$mod': '$ver' is not a valid version string")
31             if !version::is_lax($ver);
32 2         71 $MODULES{$class}{$caller}{$mod} = [ $ver, undef ];
33             }
34             }
35             else {
36 0         0 Carp::croak("arguments to 'recommended' must be scalars or a hash ref");
37             }
38             }
39             }
40              
41             sub has {
42 12     12 1 829 my ( $class, $mod, $ver ) = @_;
43 12         18 my $caller = caller;
44 12         27 my $spec = $MODULES{$class}{$caller}{$mod};
45              
46 12 100       36 return 0 unless $spec; # explicit to ensure a value even in list context
47              
48 8 100       14 if ( defined $ver ) {
49 1 50       4 Carp::croak("module '$mod': '$ver' is not a valid version string")
50             if !version::is_lax($ver);
51             }
52             else {
53             # shortcut if default already checked
54 7 50       54 return $spec->[1] if defined $spec->[1];
55 7         10 $ver = $spec->[0];
56             }
57              
58             # don't call with a version; we want this to die only on compile failure
59 8         34 Module::Runtime::use_package_optimistically($mod);
60              
61 8   100     19412 my $ok = $INC{ Module::Runtime::module_notional_filename($mod) }
62             && $mod->VERSION >= version->new($ver);
63              
64 8 100       319 return $spec->[1] = $ok ? 1 : 0;
65             }
66              
67             1;
68              
69              
70             # vim: ts=4 sts=4 sw=4 et:
71              
72             __END__