File Coverage

blib/lib/Every.pm
Criterion Covered Total %
statement 35 43 81.4
branch 12 16 75.0
condition 2 3 66.6
subroutine 7 7 100.0
pod 1 1 100.0
total 57 70 81.4


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package Every;
4              
5 1     1   9172 use Devel::Callsite;
  1         25633  
  1         116  
6 1     1   13 use strict;
  1         2  
  1         41  
7 1     1   6 use warnings;
  1         7  
  1         73  
8              
9             our $VERSION = '0.08';
10             $VERSION = eval $VERSION;
11              
12 1     1   5 use Exporter;
  1         2  
  1         150  
13             our @ISA = qw(Exporter);
14             our @EXPORT = qw(every);
15              
16 1     1   6 use Scalar::Util qw(looks_like_number);
  1         2  
  1         561  
17              
18             my %counters;
19             my %time_counters;
20              
21             # Arg must be defined and be a positive number
22             sub _check_arg
23             {
24 205     205   237 my $arg = $_[0];
25              
26 205 50       363 if (! defined($arg)) {
27 0         0 require Carp;
28 0         0 Carp::croak('Argument missing or undefined');
29             }
30 205 50       473 if (! Scalar::Util::looks_like_number($arg)) {
31 0         0 require Carp;
32 0         0 Carp::croak('Argument is not numeric');
33             }
34 205 50       405 if ($arg <= 0) {
35 0         0 require Carp;
36 0         0 Carp::croak('Argument must be greater than zero');
37             }
38             }
39              
40             sub every
41             {
42 205     205 1 3001265 my ($div, @id) = @_;
43              
44 205         703 my $site = callsite() . '/' . context();
45            
46 205 100 66     1013 if (defined($div) && ($div =~ /^sec/)) { # Allows 'seconds', 'secs', etc.
47 4         18 my $now = time(); # Capture current time
48              
49 4         11 $div = shift(@id);
50 4         10 _check_arg($div); # Validate arg
51              
52 4         29 my $key = join('_', caller(), $div, $site, @id); # Hash key
53              
54 4 100       19 if (my $then = $time_counters{$key}) {
55 3         13 my $diff = $now - ($then + $div);
56 3 100       22 return ($diff >= 0) ? ($time_counters{$key} = $now) : 0;
57             } else {
58             # First time called
59 1         3 $time_counters{$key} = $now;
60 1         3 return;
61             }
62             }
63              
64 201         298 _check_arg($div);
65 201 50       315 if (int($div) != $div) { # Non-timer arg should also be an integer
66 0         0 require Carp;
67 0         0 Carp::croak('Argument is not an integer');
68             }
69              
70 201         628 my $key = join('_', caller(), $div, $site, @id); # Hash key
71 201 100       755 return !(++$counters{$key} % $div) ? 1 : 0;
72             }
73              
74             1;
75              
76             __END__