File Coverage

blib/lib/Process/Launcher.pm
Criterion Covered Total %
statement 32 86 37.2
branch 3 28 10.7
condition 0 2 0.0
subroutine 10 15 66.6
pod 3 6 50.0
total 48 137 35.0


line stmt bran cond sub pod time code
1             package Process::Launcher;
2              
3 3     3   58800 use 5.00503;
  3         10  
  3         147  
4 3     3   16 use strict;
  3         6  
  3         84  
5 3     3   14 use Exporter ();
  3         4  
  3         37  
6 3     3   1674 use Params::Util ();
  3         14682  
  3         60  
7 3     3   1164 use Process ();
  3         7  
  3         52  
8 3     3   1136 use Process::Serializable ();
  3         6  
  3         61  
9              
10 3     3   16 use vars qw{$VERSION @ISA @EXPORT};
  3         4  
  3         249  
11             BEGIN {
12 3     3   5 $VERSION = '0.30';
13 3         47 @ISA = qw{Exporter};
14 3         9 @EXPORT = qw{run run3 serialized};
15              
16             # Preload the heavyish Process::Storable module
17             # (if prefork is available)
18 3     3   165 eval "use prefork 'Process::Storable';";
  3         4028  
  0         0  
  0         0  
19             }
20              
21              
22              
23              
24              
25             #####################################################################
26             # Interface Functions
27              
28             sub run() {
29 0     0 1 0 my $class = load(shift @ARGV);
30              
31             # Create the object
32 0         0 my $object = $class->new( @ARGV );
33 0 0       0 unless ( $object ) {
34 0         0 fail("$class->new returned false");
35             }
36              
37             # Run it
38 0         0 execute($object);
39              
40 0         0 exit(0);
41             }
42              
43             sub run3() {
44 0     0 1 0 my $class = load(shift @ARGV);
45              
46             # Load the params from STDIN
47 0         0 my @params = ();
48 0         0 SCOPE: {
49             # Implementation recycled from Config::Tiny
50 0         0 local $/;
51 0         0 my $input = ;
52 0         0 foreach ( split /(?:\015{1,2}\012|\015|\012)/, $input ) {
53 0 0       0 if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) {
54 0         0 push @params, $1, $2;
55 0         0 next;
56             }
57 0         0 fail("Input did not match the correct format");
58             }
59             }
60              
61             # Create the process
62 0         0 my $object = $class->new( @params );
63 0 0       0 unless ( $object ) {
64 0         0 fail("$class->new returned false");
65             }
66              
67             # Run it
68 0         0 execute($object);
69              
70 0         0 exit(0);
71             }
72              
73             sub serialized() {
74 0     0 1 0 my $class = load(shift @ARGV);
75 0 0       0 unless ( $class->isa('Process::Serializable') ) {
76 0         0 fail("$class is not a Process::Serializable subclass");
77             }
78              
79             # Deserialize the object
80 0         0 my $input = shift @ARGV;
81 0   0     0 my $object = $class->deserialize( $input or \*STDIN );
82 0 0       0 unless ( $object ) {
83 0         0 fail("Failed to deserialize STDIN to a $class");
84             }
85              
86             # Run it
87 0         0 execute($object);
88              
89             # Return the object after execution
90 0         0 $object->serialize(\*STDOUT);
91              
92 0         0 exit(0);
93             }
94              
95              
96              
97              
98              
99             #####################################################################
100             # Support Functions
101              
102             sub execute($) {
103 0     0 0 0 my $object = shift;
104 0         0 my $class = ref($object);
105 0 0       0 if ( $object->isa('Process::Backgroundable') ) {
106 0         0 close STDIN;
107 0         0 my $pid = fork();
108 0 0       0 exit(0) if $pid;
109             }
110              
111             # Prepare the Process
112 0         0 my $rv = eval { $object->prepare };
  0         0  
113 0 0       0 fail("$class->prepare died: $@") if $@;
114 0 0       0 fail("$class->prepare returned false") unless $rv;
115              
116             # Run the process
117 0         0 $rv = eval { $object->run };
  0         0  
118 0 0       0 fail("$class->run died: $@") if $@;
119 0 0       0 fail("$class->run returned false") unless $rv;
120              
121 0         0 print "OK\n";
122             }
123              
124             sub load($) {
125 1     1 0 1752 my $class = shift;
126 1 50       27 unless ( Params::Util::_CLASS($class) ) {
127 0         0 fail("Did not provide a valid class as first argument");
128             }
129 1         52 eval "require $class";
130 1 50       4 fail("Error loading $class: $@") if $@;
131 1 50       11 unless ( $class->isa('Process') ) {
132 0         0 fail("$class is not a Process class");
133             }
134 1         3 $class;
135             }
136              
137             sub fail($) {
138 0     0 0   my $message = shift;
139 0           $message =~ s/\n$//;
140 0           print "FAIL - $message\n";
141 0           exit(0);
142             }
143              
144             1;
145              
146             __END__