File Coverage

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


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