| 1 | package Guard::Timer; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use Exporter 'import'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | our $VERSION = '1.0.1'; # VERSION | |||||||||||
| 7 | # ABSTRACT: a scope guard that keeps time | ||||||||||||
| 8 | |||||||||||||
| 9 | |||||||||||||
| 10 | 2 | our @EXPORT = our @EXPORT_OK = qw/ timer_guard /; | |||||||||||
| 11 | |||||||||||||
| 12 | 2 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 2 | ▶ | use Time::HiRes qw/ gettimeofday tv_interval /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 2 | ▶ | use Guard; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | |||||||||||||
| 17 | 6 | ▶ | sub timer_guard(&;$) { ## no critic(ProhibitSubroutinePrototypes) | ||||||||||
|
Subroutine
timer_guard: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||
| 18 | 6 | my ($subref, $decimal_points) = @_; | |||||||||||
| 19 | 6 | ▶ | $decimal_points ||= 3; | ||||||||||
|
Condition50%1/2
$decimal_points ||= 3
MC/DC0%0/1
$decimal_points ||= 3
$decimal_points
Truth table50%1/2
A
A= $decimal_points
| |||||||||||||
| 20 | 6 | ▶ | $decimal_points =~ /\A\d+\Z/ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 21 | or croak("timer_guard: Number of decimal points isn't an integer"); | ||||||||||||
| 22 | |||||||||||||
| 23 | 6 | my $t0 = [ gettimeofday() ]; | |||||||||||
| 24 | |||||||||||||
| 25 | 6 | ▶ | return guard { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | 6 | my $duration = sprintf( "%.${decimal_points}f", tv_interval( $t0 ) ); | |||||||||||
| 27 | 6 | $subref->($duration); | |||||||||||
| 28 | 6 | }; | |||||||||||
| 29 | } | ||||||||||||
| 30 | |||||||||||||
| 31 | 2 | 1; | |||||||||||
| 32 | |||||||||||||
| 33 | __END__ | ||||||||||||