blib/lib/Async/Group.pm

filter: Statementstmt 100% 47 / 47 Branchbran 60.0% 6 / 10 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 9 / 9 Podpod 80.0% 4 / 5 totaltotal 93.4% 71 / 76 CC 10 SCAR 11.0
CC
10
Cov
93%
run
5.0
callDone
4.0
printEvent
2.1
CRAP 3.0
↑ Summary
1 ############################################################
2 #
3 # $Header: /mnt/barrayar/d06/home/domi/Tools/perlDev/Async_Group/RCS/Group.pm,v 1.2 1998/11/20 12:31:02 domi Exp $
4 #
5 # $Source: /mnt/barrayar/d06/home/domi/Tools/perlDev/Async_Group/RCS/Group.pm,v $
6 # $Revision: 1.2 $
7 # $Locker: $
8 #
9 ############################################################
10  
11 package Async::Group ;
12  
13 1 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 #use AutoLoader 'AUTOLOAD' ;
15  
16 1 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use vars qw($VERSION) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 $VERSION = substr q$Revision: 1.2 $, 10;
19  
20 # see loadspecs for other names
21 sub new
22 1 ▶ {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
23 1 my $type = shift ;
24 1 my $self = {} ;
25 1 my %args = @_ ;
26  
27 1 $self->{name}='' ;
28 1 foreach (qw/name test/)
29 {
30 2 $self->{$_}= $args{$_} ;
31 }
32
33 1 bless $self,$type ;
34 }
35  
36 sub getCbRef
37 1 ▶ {
Subroutine getCbRef: called CC=1 SCAR=0.0
Pod documented
38 1 my $self = shift;
39 1 ▶ return sub{$self->callDone(@_)} ;
Subroutine __ANON__: called CC=1 SCAR=0.0
40 }
41  
42 sub printEvent
43 6 ▶ {
Subroutine printEvent: called CC=2 SCAR=7.2
Pod undocumented
44 6 my $self = shift ;
45 6 ▶ warn "$self->{name} ",shift if $self->{test} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"test"} 0 6 6
46 }
47  
48 # Call a set of asynchronous functions which MUST have their set of user
49 # callbacks.. Note that the user call-back invoked when the function MUST
50 # call the asyncDone function with a result.
51 #
52 # When all function calls are over (i.e. all call-back were performed)
53 # all the returned results are logically 'anded' and the resulting result
54 # is passed to the main user call-back function
55 sub run
56 1 ▶ {
Subroutine run: called CC=5 SCAR=16.2
Pod documented
57 1 my $self = shift ;
58  
59 # 'set' => [ sub { } ,... ]
60 # 'callback' => 'method_name'
61 1 my %args = @_ ;
62  
63 1 foreach (qw/set callback/)
64 {
65 croak( "No $_ passed to Async::Group::run\n") unless
66 2 ▶ defined $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$_} 0 2 2
67 }
68
69 croak( "$self->name:Async::Group: set parameter is not an array ref\n")
70 1 ▶ unless ref($args{set}) eq 'ARRAY' ;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"set"} eq "ARRAY" 0 1 1
71  
72 # initialize
73 1 $self->{result} = 1 ;
74 1 $self->{out} = '' ;
75  
76 # compute nb of asynchronous calls that will be done
77 1 $self->{onGoing} = scalar (@{$args{set}}) ;
78  
79 # make up some log message
80 $self->printEvent("asynCall called for ".
81 1 $self->{onGoing}." calls");
82  
83 # store what to do when the asyncGroup is all done
84 1 $self->{callback} = $args{'callback'} ;
85  
86 # call them
87 1 foreach my $func ( @{$args{set}} )
88 {
89 4 &$func ;
90 }
91 }
92  
93 # expects asyncGroup id and a result as parameter
94 sub callDone
95 4 ▶ {
Subroutine callDone: called CC=4 SCAR=13.9
Pod documented
96 4 my $self= shift ;
97 4 my $result = shift ;
98 4 my $str = shift ;
99  
100 # no more info can be passed back, since we don't know what to do with
101 # the results until all applied function are finished
102 #store results
103 4 ▶ $self->{out} .= $str if defined $str ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $str 4 0 4
104 4 ▶ $self->{result} &&= $result ;
Condition100%3/3
$self->{"result"} &&= $result
!ll&&!rl&&r
112
MC/DC100%2/2
$self->{"result"} &&= $result
$self->{"result"} $result
Truth table100%3/3
A &&= B
A= $self->{"result"}B= $result
ABresult
0X0
100
111
105 $self->printEvent("Async::Group call done ($result), ".
106 -- $self->{onGoing}
107 4 ." left to do\n");
108
109 4 ▶ unless ($self->{onGoing})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"onGoing"}) 1 3 4
110 {
111 $self->printEvent
112 ("Async::Group finished, global result is $self->{result}\n"
113 1 .$self->{out}) ;
114 1 my $cb = $self->{'callback'} ;
115 1 &$cb($self->{result},$self->{out});
116 }
117 }
118  
119 1 1;
120  
121 __END__
↑ Summary