| 1 | package Process::Delegatable; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.00503; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use File::Temp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use IPC::Run3 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Probe::Perl (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Storable (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Process::Storable (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use vars qw{$VERSION @ISA @PERLCMD}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 6 | $VERSION = '0.30'; | |||||||||||||||||||||||||||
| 14 | 6 | @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 | 6 | @PERLCMD = ( Probe::Perl->find_perl_interpreter ); | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | sub delegate { | ||||||||||||||||||||||||||
|
Subroutine
delegate: called CC=9 SCAR=22.8
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 25 | 3 | my $class = ref($self); | |||||||||||||||||||||||||||
| 26 | 3 | ▶ | my @perl = @_ ? @_ : @PERLCMD; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # We always pass the object by file | ||||||||||||||||||||||||||||
| 29 | 3 | my ($stdin, $filein ) = File::Temp::tempfile(); | |||||||||||||||||||||||||||
| 30 | 3 | $self->serialize($stdin); | |||||||||||||||||||||||||||
| 31 | 3 | close($stdin); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Dump the object to the input filehandle, | ||||||||||||||||||||||||||||
| 34 | # and recover it afterwards | ||||||||||||||||||||||||||||
| 35 | 3 | my ($stdout, $fileout) = File::Temp::tempfile(); | |||||||||||||||||||||||||||
| 36 | # my ($stderr, $fileerr) = File::Temp::tempfile(); | ||||||||||||||||||||||||||||
| 37 | 3 | 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 | my $cmd = [ @perl, '-MProcess::Launcher', '-e serialized', $class, $filein ]; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Fire the command | ||||||||||||||||||||||||||||
| 47 | # print STDERR "# " . join(' ', @$cmd) . " < $filein > $fileout\n"; | ||||||||||||||||||||||||||||
| 48 | 3 | my $ok = IPC::Run3::run3( $cmd, \undef, $fileout, \undef ); | |||||||||||||||||||||||||||
| 49 | 3 | ▶ | if ( ! $ok or $? != 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $ok or $? != 0
MC/DC0%0/2
not $ok or $? != 0
not $ok
$? != 0
Truth table33%1/3
not(A) or B
A= not $okB= $? != 0
| |||||||||||||||||||||||||||||
| 50 | # Failed | ||||||||||||||||||||||||||||
| 51 | 0 | $self->{errstr} = "Failed to execute delegated Process"; | |||||||||||||||||||||||||||
| 52 | # $self->{stderr} = $extras; | ||||||||||||||||||||||||||||
| 53 | 0 | return 1; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Get the first line with content of the response, which will be an OK/FAIL | ||||||||||||||||||||||||||||
| 57 | 3 | local *STDOUT2; | |||||||||||||||||||||||||||
| 58 | 3 | ▶ | open( STDOUT2, $fileout ) or die "open($fileout): $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | # open( STDERR2, $fileerr ) or die "open($fileerr): $!"; | ||||||||||||||||||||||||||||
| 60 | 3 | my $result; | |||||||||||||||||||||||||||
| 61 | # my $extras; | ||||||||||||||||||||||||||||
| 62 | 3 | while ( 1 ) { | |||||||||||||||||||||||||||
| 63 | 3 | $result = <STDOUT2>; | |||||||||||||||||||||||||||
| 64 | 3 | ▶ | $result = "FAIL No output returned\n" unless defined $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 3 | ▶ | next unless $result =~ /\S/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 3 | $result =~ s/[\012\015]+\z//; | |||||||||||||||||||||||||||
| 67 | 3 | last; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | # while ( 1 ) { | ||||||||||||||||||||||||||||
| 70 | # $extras = <STDERR2>; | ||||||||||||||||||||||||||||
| 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 | ▶ | if ( $result eq 'OK' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | # Looks good, deserialize the data | ||||||||||||||||||||||||||||
| 78 | 2 | my $complete = $class->deserialize( \*STDOUT2 ); | |||||||||||||||||||||||||||
| 79 | 2 | %$self = %$complete; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Clean up and return | ||||||||||||||||||||||||||||
| 82 | 2 | close( STDOUT2 ); | |||||||||||||||||||||||||||
| 83 | 2 | close( STDERR2 ); | |||||||||||||||||||||||||||
| 84 | 2 | return 1; | |||||||||||||||||||||||||||
| 85 | } else { | ||||||||||||||||||||||||||||
| 86 | # Just clean up | ||||||||||||||||||||||||||||
| 87 | 1 | close( STDOUT2 ); | |||||||||||||||||||||||||||
| 88 | 1 | close( STDERR2 ); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Is it an error? | ||||||||||||||||||||||||||||
| 92 | 1 | ▶ | if ( $result =~ s/^FAIL// ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | # Failed | ||||||||||||||||||||||||||||
| 94 | 1 | $self->{errstr} = $result; | |||||||||||||||||||||||||||
| 95 | # $self->{stderr} = $extras; | ||||||||||||||||||||||||||||
| 96 | 1 | return 1; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # err... | ||||||||||||||||||||||||||||
| 100 | 0 | die "Unknown delegate response $result"; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 6 | 1; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||