| 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
| |||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$old_alarm and $timeout > $old_alarm
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
| |||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined ref $sub and ref $sub eq "CODE"
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"
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||
| 77 | 2 | ▶ | $SIG{'ALRM'} = $old_handler ? $old_handler : 'DEFAULT'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||
| 79 | 2 | ▶ | if ( $old_alarm ) {;#There was a previous alarm pending | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | elsif ($@) {return $wantarray ? ($ARRAY_ERROR,$@) : $SCALAR_ERROR . $@} | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | $wantarray ? @results : $results; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||
| 99 | 1 | 1; | |||||||||||||||||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||||||||||||||||||