File Coverage

blib/lib/IPC/Command/Multiplex.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package IPC::Command::Multiplex;
2              
3 1     1   850 use strict;
  1         3  
  1         43  
4 1     1   6 use warnings FATAL => 'all';
  1         2  
  1         52  
5 1     1   34 use 5.008001;
  1         3  
  1         44  
6 1     1   5 use Exporter qw(import);
  1         3  
  1         60  
7              
8             our @EXPORT = qw(multiplex);
9              
10 1     1   538 use POE qw(Wheel::Run);
  0            
  0            
11              
12             our $VERSION = '0.008001';
13              
14             sub multiplex {
15             my %args = @_;
16             my %kids;
17             POE::Session->create(
18             inline_states => {
19             _start => sub {
20             foreach my $cmd (@{$args{run}}) {
21             my $kid = POE::Wheel::Run->new(
22             Program => $cmd,
23             StdoutEvent => 'stdout_line',
24             );
25             $_[KERNEL]->sig_child($kid->PID, 'child_signal');
26             $kids{$kid->PID} = $kid;
27             }
28             },
29             stdout_line => sub {
30             $args{callback}->($_[ARG0]);
31             },
32             child_signal => sub { delete $kids{$_[ARG1]} },
33             }
34             );
35             POE::Kernel->run;
36             }
37              
38             =head1 NAME
39              
40             IPC::Command::Multiplex - run commands in parallel
41              
42             =head1 SYNOPSIS
43              
44             multiplex(
45             run => [
46             [ 'command1', 'arg1', 'arg2', 'arg3' ],
47             [ 'command2', 'arg1', 'arg2', 'arg3' ],
48             ...
49             ],
50             callback => sub {
51             chomp(my $line = shift);
52             # do something with $line here
53             }
54             );
55              
56             =head1 DESCRIPTION
57              
58             A simple way to run multiple commands (forking for each one) and get each
59             line returned from them to a callback as they get sent.
60              
61             Useful for aggregating log analysis and similar.
62              
63             Currently using POE - this should be considered an implementation detail and
64             may change - if this detail "leaks", i.e. you have to care about it when
65             using this module, please file a bug.
66              
67             Yes, this code is horribly new and could do with lots more examples. Try it
68             out, email to complain, or send me some!
69              
70             =head1 AUTHOR
71              
72             Matt S Trout (mst) -
73              
74             =head1 CONTRIBUTORS
75              
76             None as yet. Maybe this module is perfect ... (insert laughter here)
77              
78             =head1 COPYRIGHT
79              
80             (c) 2010 the IPC::Command::Multiplex L and L as
81             listed above
82              
83             =head1 LICENSE
84              
85             This library is free software under the same terms as perl itself
86              
87             =cut
88              
89             1;