File Coverage

blib/lib/Guard/Timer.pm
Criterion Covered Total %
statement 25 25 100.0
branch 1 2 50.0
condition 1 2 50.0
subroutine 8 8 100.0
pod 1 1 100.0
total 36 38 94.7


line stmt bran cond sub pod time code
1             package Guard::Timer;
2              
3 2     2   54830 use strict;
  2         12  
  2         46  
4 2     2   8 use warnings;
  2         3  
  2         44  
5 2     2   8 use Exporter 'import';
  2         2  
  2         103  
6             our $VERSION = '1.0.0'; # VERSION
7             # ABSTRACT: a scope guard that keeps time
8              
9              
10             our @EXPORT = our @EXPORT_OK = qw/ timer_guard /;
11              
12 2     2   14 use Carp;
  2         4  
  2         143  
13 2     2   10 use Time::HiRes qw/ gettimeofday tv_interval /;
  2         3  
  2         10  
14 2     2   874 use Guard;
  2         789  
  2         296  
15              
16              
17             sub timer_guard(&;$) { ## no critic(ProhibitSubroutinePrototypes)
18 6     6 1 82 my ($subref, $decimal_points) = @_;
19 6   50     13 $decimal_points ||= 3;
20 6 50       29 $decimal_points =~ /\A\d+\Z/
21             or croak("timer_guard: Number of decimal points isn't an integer");
22              
23 6         20 my $t0 = [ gettimeofday() ];
24              
25             return guard {
26 6     6   10038 my $duration = sprintf( "%.${decimal_points}f", tv_interval( $t0 ) );
27 6         138 $subref->($duration);
28 6         56 };
29             }
30              
31             1;
32              
33             __END__