File Coverage

inc/My/Module/Recommend/Any.pm
Criterion Covered Total %
statement 28 41 68.2
branch 1 8 12.5
condition 1 3 33.3
subroutine 10 13 76.9
pod 4 4 100.0
total 44 69 63.7


line stmt bran cond sub pod time code
1             package My::Module::Recommend::Any;
2              
3 1     1   21 use 5.006;
  1         3  
4              
5 1     1   4 use strict;
  1         1  
  1         13  
6 1     1   2 use warnings;
  1         1  
  1         26  
7              
8 1     1   3 use Carp;
  1         1  
  1         62  
9 1     1   3 use Exporter;
  1         14  
  1         45  
10              
11             # Beause the Perl 5.6 Exporter does not export import().
12             BEGIN {
13 1     1   56 *import = \&Exporter::import;
14             }
15             our $VERSION = '0.024';
16              
17             our @EXPORT_OK = qw{ __any };
18              
19 1     1   5 use constant RECOMMEND_TEMPLATE_SINGLE => " * %s is not available.\n";
  1         1  
  1         66  
20 1     1   3 use constant RECOMMEND_TEMPLATE_MULTI => " * None of %s is available.\n";
  1         1  
  1         387  
21              
22             sub __any {
23 1     1   2 my ( @args ) = @_;
24 1         3 return __PACKAGE__->new( @args );
25             }
26              
27             sub new {
28 1     1 1 2 my ( $class, @modules ) = @_;
29 1 50       4 @modules > 1
30             or croak 'Must specify at least one module and a message';
31 1         1 my $msg = pop @modules;
32 1         7 $msg =~ s/ ^\s* / /smxg;
33              
34 1   33     33 return bless {
35             modules => \@modules,
36             message => $msg,
37             }, ref $class || $class;
38             }
39              
40             sub check {
41 0     0 1   my ( $self ) = @_;
42 0           my @missing;
43 0           foreach my $m ( $self->modules() ) {
44 0 0         eval "require $m; 1"
45             and return;
46 0           push @missing, $m;
47             }
48 0           return @missing;
49             }
50              
51             sub modules {
52 0     0 1   my ( $self ) = @_;
53 0           return @{ $self->{modules} };
  0            
54             }
55              
56             sub recommend {
57 0     0 1   my ( $self ) = @_;
58 0 0         my @missing = $self->check()
59             or return;
60 0 0         my $tplt = @missing > 1 ?
61             $self->RECOMMEND_TEMPLATE_MULTI() :
62             $self->RECOMMEND_TEMPLATE_SINGLE();
63             return sprintf( $tplt, join ', ', @missing ) .
64 0           $self->{message};
65             }
66              
67             1;
68              
69             __END__