blib/lib/Sys/AlarmCall.pm

filter: Statementstmt 78.9% 45 / 57 Branchbran 53.3% 16 / 30 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 66.9% 69 / 103 CC 18 SCAR 34.8
CC
18
Cov
68%
alarm_call
39.6
BEGIN
1.0
BEGIN
1.0
CRAP 32.6
↑ Summary
1 package Sys::AlarmCall;
2 1 require Exporter;
3 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 @ISA = qw(Exporter);
5 1 @EXPORT = qw(alarm_call);
6  
7 1 ▶use vars qw($SCALAR_ERROR $ARRAY_ERROR $TIMEOUT);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Documentation in pod format after __END__ token. See Perl
12 # man pages to convert pod format to man, html and other formats.
13  
14 1 $Sys::AlarmCall::VERSION = 1.2;
15  
16 1 my @ARG_STRINGS = (
17 '$_[0]',
18 '$_[0],$_[1]',
19 '$_[0],$_[1],$_[2]',
20 '$_[0],$_[1],$_[2],$_[3]',
21 '$_[0],$_[1],$_[2],$_[3],$_[4]',
22 '$_[0],$_[1],$_[2],$_[3],$_[4],$_[5]'
23 );
24  
25 1 $SCALAR_ERROR = 'ERROR ';
26 1 $ARRAY_ERROR = 'ERROR';
27 1 $TIMEOUT = 'TIMEOUT';
28  
29 1 ▶sub _alarm_sig_handler {
Subroutine _alarm_sig_handler: called CC=1 SCAR=0.0
30 1 die "Sys::AlarmCall::alarm_call went off\n";
31 }
32  
33 2 ▶sub alarm_call {
Subroutine alarm_call: called CC=18 SCAR=36.8
Pod documented
34 # usage('INTEGER(>,0)','FUNCTION','LIST_OF_ARGUMENTS');
35 2 my $timeout = shift;
36 2 my $sub = shift;
37 2 ▶ $timeout >= 1 ||
Branch: 1 branch
BranchTrueFalseTotal
$timeout >= 1 or croak("Sys::AlarmCall::alarm_call: Fatal error - timeout argument must be positive\n") 2 0 2
38 croak("Sys::AlarmCall::alarm_call: Fatal error - timeout argument must be positive\n");
39 2 my($old_handler,$old_alarm,$wantarray,$remaining_alarm);
40 2 my($eval,$alarmed);
41  
42 2 $old_alarm = alarm(0);
43 2 ▶ if ( $old_alarm && ($timeout > $old_alarm) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_alarm and $timeout > $old_alarm) 0 2 2
Condition33%1/3
$old_alarm and $timeout > $old_alarm
!ll&&!rl&&r
200
MC/DC0%0/2
$old_alarm and $timeout > $old_alarm
$old_alarm $timeout > $old_alarm
Truth table33%1/3
A and B
A= $old_alarmB= $timeout > $old_alarm
ABresult
0X0
100
111
44 0 $timeout = $old_alarm;
45 }
46  
47 2 $wantarray = wantarray;
48 2 $old_handler = $SIG{'ALRM'};
49 2 $SIG{'ALRM'} = "Sys::AlarmCall::_alarm_sig_handler";
50 2 ▶ if (defined(ref($sub)) && (ref($sub) eq 'CODE') ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined ref $sub and ref $sub eq 'CODE') { } 0 2 2
elsif ($sub =~ /^->/) { } 0 2 2
elsif ($#_ < @ARG_STRINGS) { } 2 0 2
Condition33%1/3
defined ref $sub and ref $sub eq "CODE"
!ll&&!rl&&r
020
MC/DC0%0/2
defined ref $sub and ref $sub eq "CODE"
defined ref $sub ref $sub eq "CODE"
Truth table33%1/3
A and B
A= defined ref $subB= ref $sub eq "CODE"
ABresult
0X0
100
111
51 0 unshift(@_,$sub);
52 0 $sub = '&{shift(@_)}(@_);';
53 } elsif ($sub =~ m/^->/) {
54 0 unshift(@_,$sub);
55 0 $sub = 'shift(@_)' . $sub . '(@_);';
56 } elsif ($#_ < @ARG_STRINGS) {
57 ;#This is because many perl inbuilt functions won't compile
58 ;#if you just say func(@_) - they need to be given the
59 ;#proper number of arguments.
60 2 $sub .= '(' . $ARG_STRINGS[$#_] . ')' ;
61 } else {
62 0 $sub .= '(@_)';
63 }
64  
65 2 my ($callpack) = caller;
66 2 $eval = 'alarm(' . $timeout . ");\npackage " . $callpack . ";\n" . $sub;
67 2 my(@results,$results);
68 2 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 0 2 2
69 0 @results = eval $eval;
70 } else {
71 2 $results = eval $eval;
72 }
73 2 $remaining_alarm = alarm(0);
74 2 $alarmed = ($@ eq "Sys::AlarmCall::alarm_call went off\n");
75 2 ▶ if ($alarmed) {$remaining_alarm = 0}
Branch: 1 branch
BranchTrueFalseTotal
if ($alarmed) 1 1 2
76  
77 2 ▶ $SIG{'ALRM'} = $old_handler ? $old_handler : 'DEFAULT';
Branch: 1 branch
BranchTrueFalseTotal
$old_handler ? : 1 1 2
78  
79 2 ▶ if ( $old_alarm ) {;#There was a previous alarm pending
Branch: 1 branch
BranchTrueFalseTotal
if ($old_alarm) 0 2 2
80 0 $old_alarm = $old_alarm - $timeout + $remaining_alarm;
81 0 ▶ if ($old_alarm > 0) {;#Reset it, excluding the elapsed time (at least)
Branch: 1 branch
BranchTrueFalseTotal
if ($old_alarm > 0) { } 0 0 0
82 0 alarm($old_alarm);
83 } else {;#It should have gone off already - so set it off
84 0 kill 'ALRM',$$;
85 }
86 }
87  
88 ;#Three things to think about:
89 ;#1. Did the eval fail due to some compile error or die call?
90 ;#2. Did the eval get timed out?
91 ;#3. Do we return an array or scalar?
92  
93 2 ▶ if ($alarmed) {return $wantarray ? ($TIMEOUT) : $TIMEOUT}
Branch: 3 branches
BranchTrueFalseTotal
if ($alarmed) { } 1 1 2
elsif ($@) { } 0 1 1
$wantarray ? : 0 1 1
94 0 ▶ elsif ($@) {return $wantarray ? ($ARRAY_ERROR,$@) : $SCALAR_ERROR . $@}
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
95 1 ▶ $wantarray ? @results : $results;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 1 1
96 }
97  
98  
99 1 1;
100 __END__
↑ Summary