File Coverage

blib/lib/Module/CheckLatestVersion.pm
Criterion Covered Total %
statement 20 52 38.4
branch 0 22 0.0
condition 0 25 0.0
subroutine 7 8 87.5
pod 1 1 100.0
total 28 108 25.9


line stmt bran cond sub pod time code
1             package Module::CheckLatestVersion;
2              
3 1     1   209976 use 5.010001;
  1         3  
4 1     1   4 use strict;
  1         1  
  1         22  
5 1     1   3 use warnings;
  1         1  
  1         48  
6 1     1   1357 use Log::ger;
  1         61  
  1         4  
7 1     1   535 use Log::ger::Format::MultilevelLog (); # for scan prereqs
  1         4954  
  1         21  
8 1     1   368 use Log::ger::Format 'MultilevelLog';
  1         779  
  1         6  
9              
10 1     1   2085 use Exporter qw(import);
  1         2  
  1         462  
11              
12             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
13             our $DATE = '2026-04-14'; # DATE
14             our $DIST = 'Module-CheckLatestVersion'; # DIST
15             our $VERSION = '0.009'; # 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 0 0 0       if (defined($opts->{do_check}) && !$opts->{do_check}) {
32 0           log('debug', "Skipping checking version of module $mod ...");
33 0           return;
34             }
35              
36 0           require Cache::File::Simple;
37 0           my $cachekey = __PACKAGE__ . '|' . $mod;
38 0           log($opts->{log_level}, "Checking version of module $mod from cache ...");
39 0           my $res = Cache::File::Simple::cache($cachekey);
40 0 0         unless ($res) {
41             # cache miss
42 0           log($opts->{log_level}, "Checking version of module $mod (cache miss) ...");
43 0           require Module::CheckVersion;
44 0           $res = Module::CheckVersion::check_module_version(module => $mod);
45             }
46              
47 0 0         if ($res->[0] != 200) {
48 0           warn "Cannot check latest version of module $mod: $res->[0] - $res->[1]";
49 0           return;
50             }
51              
52 0 0         if ($res->[2]{is_latest_version}) {
53 0           log($opts->{log_level}, "Module $mod (installed version: $res->[2]{installed_version}) is latest version ($res->[2]{latest_version})");
54             # cache only positive result AND when version is defined
55 0 0 0       if (defined($res->[2]{installed_version}) && defined($res->[2]{latest_version})) {
56 0           log($opts->{log_level}, "Caching version check result ...");
57 0           Cache::File::Simple::cache($cachekey, $res);
58             }
59             } else {
60             my $msg = "Module $mod (installed version: " .
61             (defined($res->[2]{installed_version}) ? $res->[2]{installed_version} : "undef") .
62             ") is not the latest version (" .
63 0 0         (defined($res->[2]{latest_version}) ? $res->[2]{latest_version} : "undef") .
    0          
64             ").";
65 0 0 0       if ($opts->{die} && defined $res->[2]{latest_version}) {
66 0           $msg .= " Please update to the latest version first.";
67 0           die $msg;
68             } else {
69 0           $msg .= " Please consider updating to the latest version.";
70 0           warn $msg;
71             }
72             }
73             }
74              
75             1;
76             # ABSTRACT: Warn/die when a module is not the latest version
77              
78             __END__