Process/Launcher.pm

filter: Statementstmt 22.2% 32 / 144 Branchbran 5.3% 3 / 56 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 47.6% 10 / 21 Podpod 50.0% 6 / 12 totaltotal 21.3% 51 / 239 CC 33 SCAR 31.3
CC
33
Cov
17%
execute
56.0
execute
56.0
run3
20.0
CRAP 22.9
« Process/YAML.pm ↑ Summary Process/Storable.pm »
1 package Process::Launcher;
2  
3 3 ▶use 5.00503;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Params::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Process ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Process::Serializable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use vars qw{$VERSION @ISA @EXPORT};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.2
12 3 $VERSION = '0.30';
13 3 @ISA = qw{Exporter};
14 3 @EXPORT = qw{run run3 serialized};
15  
16 # Preload the heavyish Process::Storable module
17 # (if prefork is available)
18 0 ▶ eval "use prefork 'Process::Storable';";
Subroutine BEGIN: called CC=1 SCAR=1.2
19 }
20  
21  
22  
23  
24  
25 #####################################################################
26 # Interface Functions
27  
28 0 ▶sub run() {
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
29 0 ▶ my $class = load(shift @ARGV);
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
30  
31 # Create the object
32 0 my $object = $class->new( @ARGV );
33 0 ▶ unless ( $object ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($object) 0 0 0
unless ($object) 0 0 0
34 0 fail("$class->new returned false");
35 }
36  
37 # Run it
38 0 execute($object);
39  
40 0 exit(0);
41 }
42  
43 0 ▶sub run3() {
Subroutine run3: not called CC=4 SCAR=30.0
Pod documented
44 0 ▶ my $class = load(shift @ARGV);
Subroutine run3: not called CC=4 SCAR=30.0
Pod documented
45  
46 # Load the params from STDIN
47 0 my @params = ();
48 SCOPE: {
49 # Implementation recycled from Config::Tiny
50 0 local $/;
51 0 my $input = <STDIN>;
52 0 foreach ( split /(?:\015{1,2}\012|\015|\012)/, $input ) {
53 0 ▶ if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\s*([^=]+?)\s*=\s*(.*?)\s*$/) 0 0 0
if (/^\s*([^=]+?)\s*=\s*(.*?)\s*$/) 0 0 0
54 0 push @params, $1, $2;
55 0 next;
56 }
57 0 fail("Input did not match the correct format");
58 }
59 }
60  
61 # Create the process
62 0 my $object = $class->new( @params );
63 0 ▶ unless ( $object ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($object) 0 0 0
unless ($object) 0 0 0
64 0 fail("$class->new returned false");
65 }
66  
67 # Run it
68 0 execute($object);
69  
70 0 exit(0);
71 }
72  
73 0 ▶sub serialized() {
Subroutine serialized: not called CC=4 SCAR=30.0
Pod documented
74 0 ▶ my $class = load(shift @ARGV);
Subroutine serialized: not called CC=4 SCAR=30.0
Pod documented
75 0 ▶ unless ( $class->isa('Process::Serializable') ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($class->isa("Process::Serializable")) 0 0 0
unless ($class->isa("Process::Serializable")) 0 0 0
76 0 fail("$class is not a Process::Serializable subclass");
77 }
78  
79 # Deserialize the object
80 0 my $input = shift @ARGV;
81 0 ▶ my $object = $class->deserialize( $input or \*STDIN );
Condition0%0/4
$input || \*STDIN
l!l
00
$input || \*STDIN
l!l
00
MC/DC0%0/1
$input || \*STDIN
$input
MC/DC0%0/1
$input || \*STDIN
$input
Truth table0%0/2
A
A= $input
Aresult
11
00
Truth table0%0/2
A
A= $input
Aresult
11
00
82 0 ▶ unless ( $object ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($object) 0 0 0
unless ($object) 0 0 0
83 0 fail("Failed to deserialize STDIN to a $class");
84 }
85  
86 # Run it
87 0 execute($object);
88  
89 # Return the object after execution
90 0 $object->serialize(\*STDOUT);
91  
92 0 exit(0);
93 }
94  
95  
96  
97  
98  
99 #####################################################################
100 # Support Functions
101  
102 0 ▶sub execute($) {
Subroutine execute: not called CC=7 SCAR=40.3
Pod undocumented
103 0 ▶ my $object = shift;
Subroutine execute: not called CC=7 SCAR=40.3
Pod undocumented
104 0 my $class = ref($object);
105 0 ▶ if ( $object->isa('Process::Backgroundable') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($object->isa("Process::Backgroundable")) 0 0 0
if ($object->isa("Process::Backgroundable")) 0 0 0
106 0 close STDIN;
107 0 my $pid = fork();
108 0 ▶ exit(0) if $pid;
Branch: 2 branches
BranchTrueFalseTotal
if $pid 0 0 0
if $pid 0 0 0
109 }
110  
111 # Prepare the Process
112 0 my $rv = eval { $object->prepare };
113 0 ▶ fail("$class->prepare died: $@") if $@;
Branch: 2 branches
BranchTrueFalseTotal
if $@ 0 0 0
if $@ 0 0 0
114 0 ▶ fail("$class->prepare returned false") unless $rv;
Branch: 2 branches
BranchTrueFalseTotal
unless $rv 0 0 0
unless $rv 0 0 0
115  
116 # Run the process
117 0 $rv = eval { $object->run };
118 0 ▶ fail("$class->run died: $@") if $@;
Branch: 2 branches
BranchTrueFalseTotal
if $@ 0 0 0
if $@ 0 0 0
119 0 ▶ fail("$class->run returned false") unless $rv;
Branch: 2 branches
BranchTrueFalseTotal
unless $rv 0 0 0
unless $rv 0 0 0
120  
121 0 print "OK\n";
122 }
123  
124 1 ▶sub load($) {
Subroutine load: called CC=4 SCAR=22.0
Pod undocumented
125 0 ▶ my $class = shift;
Subroutine load: not called CC=4 SCAR=22.0
Pod undocumented
126 0 ▶ unless ( Params::Util::_CLASS($class) ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (&Params::Util::_CLASS($class)) 0 1 1
unless (&Params::Util::_CLASS($class)) 0 0 0
127 0 fail("Did not provide a valid class as first argument");
128 }
129 0 eval "require $class";
130 0 ▶ fail("Error loading $class: $@") if $@;
Branch: 2 branches
BranchTrueFalseTotal
if $@ 0 1 1
if $@ 0 0 0
131 0 ▶ unless ( $class->isa('Process') ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($class->isa("Process")) 0 1 1
unless ($class->isa("Process")) 0 0 0
132 0 fail("$class is not a Process class");
133 }
134 0 $class;
135 }
136  
137 0 ▶sub fail($) {
Subroutine fail: not called CC=1 SCAR=6.9
Pod undocumented
138 0 ▶ my $message = shift;
Subroutine fail: not called CC=1 SCAR=6.9
Pod undocumented
139 0 $message =~ s/\n$//;
140 0 print "FAIL - $message\n";
141 0 exit(0);
142 }
143  
144 3 1;
145  
146 __END__
« Process/YAML.pm ↑ Summary Process/Storable.pm »