File Coverage

blib/lib/Process/Delegatable.pm
Criterion Covered Total %
statement 58 61 95.0
branch 8 14 57.1
condition 1 3 33.3
subroutine 10 10 100.0
pod 1 1 100.0
total 78 89 87.6


line stmt bran cond sub pod time code
1             package Process::Delegatable;
2              
3 3     3   2071 use 5.00503;
  3         12  
  3         120  
4 3     3   19 use strict;
  3         5  
  3         104  
5 3     3   3993 use File::Temp ();
  3         58967  
  3         68  
6 3     3   2711 use IPC::Run3 ();
  3         50910  
  3         87  
7 3     3   2907 use Probe::Perl ();
  3         6299  
  3         86  
8 3     3   1210 use Storable ();
  3         3221  
  3         64  
9 3     3   568 use Process::Storable ();
  3         36  
  3         66  
10              
11 3     3   17 use vars qw{$VERSION @ISA @PERLCMD};
  3         4  
  3         288  
12             BEGIN {
13 3     3   7 $VERSION = '0.30';
14 3         54 @ISA = 'Process::Storable';
15              
16             # Contains the command to use to launch perl
17             # Should be the path to the perl current running.
18             # People with special needs should localise this
19             # to add any flags.
20 3         23 @PERLCMD = ( Probe::Perl->find_perl_interpreter );
21             }
22              
23             sub delegate {
24 3     3 1 10883 my $self = shift;
25 3         11 my $class = ref($self);
26 3 50       18 my @perl = @_ ? @_ : @PERLCMD;
27              
28             # We always pass the object by file
29 3         27 my ($stdin, $filein ) = File::Temp::tempfile();
30 3         2054 $self->serialize($stdin);
31 3         548 close($stdin);
32              
33             # Dump the object to the input filehandle,
34             # and recover it afterwards
35 3         16 my ($stdout, $fileout) = File::Temp::tempfile();
36             # my ($stderr, $fileerr) = File::Temp::tempfile();
37 3         1004 close($stdout);
38             # close($stderr);
39              
40             # Generate the command
41             #print STDERR "Process::Delegatable STDIN $filein\n";
42             #print STDERR "Process::Delegatable STDOUT $fileout\n";
43             #print STDERR "Process::Delegatable STDERR $fileerr\n";
44 3         16 my $cmd = [ @perl, '-MProcess::Launcher', '-e serialized', $class, $filein ];
45              
46             # Fire the command
47             # print STDERR "# " . join(' ', @$cmd) . " < $filein > $fileout\n";
48 3         28 my $ok = IPC::Run3::run3( $cmd, \undef, $fileout, \undef );
49 3 50 33     336181 if ( ! $ok or $? != 0 ) {
50             # Failed
51 0         0 $self->{errstr} = "Failed to execute delegated Process";
52             # $self->{stderr} = $extras;
53 0         0 return 1;
54             }
55              
56             # Get the first line with content of the response, which will be an OK/FAIL
57 3         43 local *STDOUT2;
58 3 50       224 open( STDOUT2, $fileout ) or die "open($fileout): $!";
59             # open( STDERR2, $fileerr ) or die "open($fileerr): $!";
60 3         14 my $result;
61             # my $extras;
62 3         9 while ( 1 ) {
63 3         247 $result = ;
64 3 50       25 $result = "FAIL No output returned\n" unless defined $result;
65 3 50       56 next unless $result =~ /\S/;
66 3         156 $result =~ s/[\012\015]+\z//;
67 3         16 last;
68             }
69             # while ( 1 ) {
70             # $extras = ;
71             # $extras = "FAIL No output returned\n" unless defined $extras;
72             # next unless $extras =~ /\S/;
73             # $extras =~ s/[\012\015]+\z//;
74             # last;
75             # }
76 3 100       20 if ( $result eq 'OK' ) {
77             # Looks good, deserialize the data
78 2         109 my $complete = $class->deserialize( \*STDOUT2 );
79 2         29 %$self = %$complete;
80              
81             # Clean up and return
82 2         28 close( STDOUT2 );
83 2         6 close( STDERR2 );
84 2         208 return 1;
85             } else {
86             # Just clean up
87 1         17 close( STDOUT2 );
88 1         12 close( STDERR2 );
89             }
90              
91             # Is it an error?
92 1 50       17 if ( $result =~ s/^FAIL// ) {
93             # Failed
94 1         16 $self->{errstr} = $result;
95             # $self->{stderr} = $extras;
96 1         61 return 1;
97             }
98              
99             # err...
100 0           die "Unknown delegate response $result";
101             }
102              
103             1;
104              
105             __END__