ProcessPool/Task.pm

filter: Statementstmt 100% 63 / 63 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 7 totaltotal 90.9% 90 / 99 SCAR 16.1
CC
5
Cov
97%
result
2.1
decode
2.0
execute
2.0
CRAP 5.0
« scratch.pl ↑ Summary ProcessPool/Pipeline.pm »
1 package AnyEvent::ProcessPool::Task;
2 # ABSTRACT: A serializable work unit
3 $AnyEvent::ProcessPool::Task::VERSION = '0.06';
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Class::Load 'load_class';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 use Data::Dump::Streamer;
9 use MIME::Base64;
10 use Try::Catch;
11  
12 use constant READY => 1;
13 use constant DONE => 2;
14 use constant FAIL => 4;
15  
16 sub new {
17 36 ▶ my ($class, $code, $args) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
18 36 bless [READY, [$code, $args]], $class;
19 }
20  
21 9 ▶sub done { $_[0][0] & DONE }
Subroutine done: called CC=1 SCAR=0.0
Pod undocumented
22 32 ▶sub failed { $_[0][0] & FAIL }
Subroutine failed: called CC=1 SCAR=0.0
Pod undocumented
23  
24 sub result {
25 34 ▶ return $_[0][1] if $_[0][0] & DONE;
Subroutine result: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0][0] & 2 32 0 32
26 2 return;
27 }
28  
29 sub execute {
30 6 ▶ my $self = shift;
Subroutine execute: called CC=2 SCAR=6.9
Pod undocumented
31  
32 try {
33 6 ▶ my ($work, $args) = @{$self->[1]};
Subroutine __ANON__: called CC=2 SCAR=6.9
34  
35 6 ▶ if (ref $work eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $work eq 'CODE') { } 3 1 4
36 3 $self->[1] = $work->(@$args);
37 2 $self->[0] = DONE;
38 }
39 else {
40 1 my $class = load_class($work);
41 1 $self->[1] = $class->new(@$args)->run;
42 1 $self->[0] = DONE;
43 }
44 }
45 catch {
46 1 ▶ $self->[0] = DONE | FAIL;
Subroutine __ANON__: called CC=1 SCAR=0.0
47 1 $self->[1] = $_;
48 6 };
49  
50 6 ▶ return $self->[0] & FAIL ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->[0] & 4 ? : 1 3 4
51 }
52  
53 sub encode {
54 31 ▶ my $self = shift;
Subroutine encode: called CC=1 SCAR=0.0
Pod undocumented
55 31 my $data = Dump($self)->Purity(1)->Declare(1)->Indent(0)->Out;
56 31 encode_base64($data, '');
57 }
58  
59 sub decode {
60 31 ▶ my $class = shift;
Subroutine decode: called CC=2 SCAR=6.9
Pod undocumented
61 31 my $data = decode_base64($_[0]);
62 24 ▶ my $self = eval "do{ $data }";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
63 31 ▶ croak "task decode error: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 31 31
64 31 return $self;
65 }
66  
67 1;
68  
69 __END__
« scratch.pl ↑ Summary ProcessPool/Pipeline.pm »