File Coverage

blib/lib/Module/CheckLatestVersion.pm
Criterion Covered Total %
statement 23 50 46.0
branch 0 18 0.0
condition 0 16 0.0
subroutine 8 9 88.8
pod 1 1 100.0
total 32 94 34.0


line stmt bran cond sub pod time code
1             package Module::CheckLatestVersion;
2              
3 1     1   227686 use 5.010001;
  1         3  
4 1     1   5 use strict;
  1         2  
  1         21  
5 1     1   6 use warnings;
  1         2  
  1         50  
6 1     1   1412 use Log::ger;
  1         41  
  1         5  
7 1     1   665 use Log::ger::Format::MultilevelLog (); # for scan prereqs
  1         5919  
  1         21  
8 1     1   383 use Log::ger::Format 'MultilevelLog';
  1         830  
  1         7  
9              
10 1     1   2121 use Exporter qw(import);
  1         1  
  1         116  
11              
12             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
13             our $DATE = '2026-04-14'; # DATE
14             our $DIST = 'Module-CheckLatestVersion'; # DIST
15             our $VERSION = '0.007'; # VERSION
16              
17             ## no critic: Modules::ProhibitAutomaticExportation
18             our @EXPORT = qw(check_latest_version);
19              
20             sub check_latest_version {
21 0 0   0 1   my $opts = ref $_[0] eq 'HASH' ? {%{ shift()} } : {};
  0            
22 0 0         my $mod = shift; $mod = caller() unless $mod;
  0            
23 0   0       $opts->{die} //= $ENV{PERL_MODULE_CHECKLATESTVERSION_OPT_DIE};
24 0   0       $opts->{log_level} //= 'debug';
25             $opts->{do_check} //= 0 if
26             $ENV{HARNESS_ACTIVE} ||
27             $ENV{RELEASE_TESTING} ||
28             $ENV{AUTOMATED_TESTING} ||
29 0 0 0       $ENV{PERL_MODULE_CHECKLATESTVERSION_SKIP};
      0        
      0        
      0        
30              
31 1     1   4 no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
  1         2  
  1         328  
32              
33 0           require Cache::File::Simple;
34 0           my $cachekey = __PACKAGE__ . '|' . $mod;
35 0           log($opts->{log_level}, "Checking version of module $mod from cache ...");
36 0           my $res = Cache::File::Simple::cache($cachekey);
37 0 0         unless ($res) {
38             # cache miss
39 0           log($opts->{log_level}, "Checking version of module $mod (cache miss) ...");
40 0           require Module::CheckVersion;
41 0           $res = Module::CheckVersion::check_module_version(module => $mod);
42             }
43              
44 0 0         if ($res->[0] != 200) {
45 0           warn "Cannot check latest version of module $mod: $res->[0] - $res->[1]";
46 0           return;
47             }
48              
49 0 0         if ($res->[2]{is_latest_version}) {
50 0           log($opts->{log_level}, "Module $mod is latest version ($res->[2]{latest_version}), caching result ...");
51             # cache only positive result
52 0           Cache::File::Simple::cache($cachekey, $res);
53             } else {
54             my $msg = "Module $mod (installed version: " .
55             (defined($res->[2]{installed_version}) ? $res->[2]{installed_version} : "undef") .
56             ") is not the latest version (" .
57 0 0         (defined($res->[2]{latest_version}) ? $res->[2]{latest_version} : "undef") .
    0          
58             ").";
59 0 0         if ($opts->{die}) {
60 0           $msg .= " Please update to the latest version first.";
61 0           die $msg;
62             } else {
63 0           $msg .= " Please consider updating to the latest version.";
64 0           warn $msg;
65             }
66             }
67             }
68              
69             1;
70             # ABSTRACT: Warn/die when a module is not the latest version
71              
72             __END__