File Coverage

blib/lib/Module/CheckLatestVersion.pm
Criterion Covered Total %
statement 12 34 35.2
branch 0 18 0.0
condition 0 9 0.0
subroutine 4 5 80.0
pod 1 1 100.0
total 17 67 25.3


line stmt bran cond sub pod time code
1             package Module::CheckLatestVersion;
2              
3 1     1   227937 use strict;
  1         1  
  1         32  
4 1     1   4 use warnings;
  1         1  
  1         59  
5              
6 1     1   4 use Exporter qw(import);
  1         6  
  1         104  
7              
8             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
9             our $DATE = '2026-04-08'; # DATE
10             our $DIST = 'Module-CheckLatestVersion'; # DIST
11             our $VERSION = '0.001'; # VERSION
12              
13             ## no critic: Modules::ProhibitAutomaticExportation
14             our @EXPORT = qw(check_latest_version);
15              
16             sub check_latest_version {
17             return if
18             $ENV{HARNESS_ACTIVE} ||
19             $ENV{RELEASE_TESTING} ||
20 0 0 0 0 1   $ENV{AUTOMATED_TESTING};
      0        
21              
22 1     1   5 no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
  1         1  
  1         387  
23              
24 0 0         my $opts = ref $_[0] eq 'HASH' ? shift : {};
25 0 0         my $mod = shift; $mod = caller() unless $mod;
  0            
26 0   0       $opts->{die} //= $ENV{PERL_MODULE_CHECKLATESTVERSION_DIE};
27              
28 0           require Cache::File::Simple;
29 0           my $cachekey = __PACKAGE__ . '|' . $mod;
30 0           my $res = Cache::File::Simple::cache($cachekey);
31 0 0         unless ($res) {
32             # cache miss
33 0           require Module::CheckVersion;
34 0           $res = Module::CheckVersion::check_module_version(module => $mod);
35 0           Cache::File::Simple::cache($cachekey, $res);
36              
37             }
38              
39 0 0         if ($res->[0] != 200) {
40 0           warn "Cannot check latest version of module $mod: $res->[0] - $res->[1]";
41 0           return;
42             }
43              
44 0 0         unless ($res->[2]{is_latest_version}) {
45             my $msg = "Module $mod (installed version: " .
46             (defined($res->[2]{installed_version}) ? $res->[2]{installed_version} : "undef") .
47             ") is not the latest version (" .
48 0 0         (defined($res->[2]{latest_version}) ? $res->[2]{latest_version} : "undef") .
    0          
49             ").";
50 0 0         if ($opts->{die}) {
51 0           $msg .= " Please update to the latest version first.";
52 0           die $msg;
53             } else {
54 0           $msg .= " Please consider updateing to the latest version.";
55 0           warn $msg;
56             }
57             }
58             }
59              
60             1;
61             # ABSTRACT: Warn/die when a module is not the latest version
62              
63             __END__