File Coverage

blib/lib/IPC/ForkPipe.pm
Criterion Covered Total %
statement 26 26 100.0
branch 10 16 62.5
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 43 49 87.7


line stmt bran cond sub pod time code
1             package IPC::ForkPipe;
2              
3 4     4   539305 use 5.008008;
  4         19  
4 4     4   31 use strict;
  4         8  
  4         168  
5 4     4   21 use warnings;
  4         11  
  4         1952  
6              
7             require Exporter;
8              
9             our @ISA = qw(Exporter);
10             our %EXPORT_TAGS = ( 'all' => [ qw(
11             pipe_to_fork pipe_from_fork
12             ) ] );
13              
14             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
15              
16             our @EXPORT = qw(
17             pipe_to_fork pipe_from_fork
18             );
19              
20             our $VERSION = '0.02';
21              
22              
23             # Code ganked from http://perldoc.perl.org/perlfork.html
24              
25             # simulate open(FOO, "|-")
26             sub pipe_to_fork ($) {
27 2     2 1 475818 my $parent = shift;
28 2 50       162 pipe my $child, $parent or die;
29 2         5796 my $pid = fork();
30 2 50       284 return unless defined $pid;
31 2 100       118 if ($pid) {
32 1         56 close $child;
33             }
34             else {
35 1         59 close $parent;
36 1 50       116 open(STDIN, "<&=" . fileno($child)) or die;
37             }
38 2         207 return $pid;
39             }
40              
41             # simulate open(FOO, "-|")
42             sub pipe_from_fork ($) {
43 3     3 1 628491 my $parent = shift;
44 3 50       171 pipe $parent, my $child or die;
45 3         14076 my $pid = fork();
46 3 50       484 return unless defined $pid;
47 3 100       243 if ($pid) {
48 2         90 close $child;
49             }
50             else {
51 1         71 close $parent;
52 1 50       138 open(STDOUT, ">&=" . fileno($child)) or die;
53             }
54 3         365 return $pid;
55             }
56              
57             1;
58             __END__