File Coverage

blib/lib/IPC/ForkPipe.pm
Criterion Covered Total %
statement 27 27 100.0
branch 10 16 62.5
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 42 50 84.0


line stmt bran cond sub pod time code
1             package IPC::ForkPipe;
2              
3 4     4   120204 use 5.008008;
  4         16  
  4         141  
4 4     4   22 use strict;
  4         7  
  4         138  
5 4     4   22 use warnings;
  4         40  
  4         1469  
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.01';
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 0 7083762 my $parent = shift;
28 2 50       214 pipe my $child, $parent or die;
29 2         2004 my $pid = fork();
30 2 50       14165 return unless defined $pid;
31 2 100       8232 if ($pid) {
32 1         12 close $child;
33             }
34             else {
35 1         34 close $parent;
36 1 50       164 open(STDIN, "<&=" . fileno($child)) or die;
37             }
38 2         105 return $pid;
39             }
40              
41             # simulate open(FOO, "-|")
42             sub pipe_from_fork ($) {
43 3     3 0 78 my $parent = shift;
44 3 50       123 pipe $parent, my $child or die;
45 3         15859 my $pid = fork();
46 3 50       339 return unless defined $pid;
47 3 100       214 if ($pid) {
48 2         64 close $child;
49             }
50             else {
51 1         71 close $parent;
52 1 50       256 open(STDOUT, ">&=" . fileno($child)) or die;
53             }
54 3         386 return $pid;
55             }
56              
57             1;
58             __END__