lib/IPC/Nosh.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« script/nosh.pl ↑ Summary lib/IPC/Nosh/Mux.pm »
1 1 ▶use Object::Pad ':experimental(:all)';
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package IPC::Nosh;
4  
5 class IPC::Nosh;
6  
7 1 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 our $VERSION = "0.01.4";
10  
11 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use vars '@EXPORT';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 @EXPORT = qw(run);
15  
16 1 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use IPC::Run3;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 0 ▶use IPC::Nosh::Mux;
Subroutine BEGIN: called CC=1 SCAR=2.6
19 use IO::Handle::Common;
20 use Syntax::Keyword::Try;
21 use Syntax::Keyword::Dynamically;
22  
23 const our @run_cb_global_allow => qw(success ipcfail error fail nonzero exit);
24  
25 const our @run_cb_allow => (qw(in out err));
26 const our @run_arg_allow => ( @run_cb_allow, qw'autoflush autochomp' )
27 ; #, @run_cb_global_allow )
28  
29 # name => [ coderef, ... ]
30 field $global_cb = {};
31  
32 field $global_autochomp : param(autochomp) //= 0;
33 field $global_autoflush : param(autoflush) //= 0;
34  
35 field $in_aref = [];
36 field $out_aref = [];
37 field $err_aref = [];
38  
39 field $cmd : param : reader;
40 field $in; #: param;
41 field $out; #: param;
42 field $err; #: param;
43  
44 field $status : reader;
45 field $oserr : reader;
46  
47 field $tied = {};
48  
49 field $run_arg_href = {};
50  
51 ADJUST : params ( %arg) {
52 $run_arg_href = \%arg;
53  
54 my %tieopt = (
55 autochomp => $global_autochomp,
56 autoflush => $global_autoflush,
57 );
58  
59 $self->add_cb( \%arg, $global_cb,
60 filter => \@IPC::Nosh::run_cb_global_allow );
61  
62 foreach my ( $name, $aref )
63 ( 'in', $in_aref, 'out', $out_aref, 'err', $err_aref )
64 {
65 # my %handle_cb = map { [$name => { ( $_ isa ARRAY ? $_ : [$_] ) ) } }
66 # %arg{@IPC::Nosh::Mux::cb_handle_allow};
67  
68 my %on = (); #( $arg{on} isa 'HASH' ? $arg{on}->%* : () );
69  
70 # $self->add_cb( \%arg, \%on, filter => \@IPC::Nosh::run_cb_allow );
71  
72 if ( ( $name eq 'in' ) && ( $arg{in} isa 'HASH' ) ) {
73 @on{qw'line eof'} = $arg{in}->@{qw'line eof'};
74 }
75 else {
76 # dmsg \%arg, $arg{$name};
77 $on{line} = $arg{$name};
78 }
79  
80 $$tied{$name} = tie @$aref, 'IPC::Nosh::Mux', %tieopt, on => \%on;
81 }
82  
83 };
84  
85 method add_cb( $in, $dest, %opt ) {
86 $$dest{ $$_[0] } = $$_[1]
87 for map { [ $_ => ( $$in{$_} isa ARRAY ? $_ : [ $$in{$_} ] ) ] }
88 ( $opt{filter} && $opt{filter} isa ARRAY ? $opt{filter}->@* : keys %$in );
89 }
90  
91 method $run ($cmd) {
92 try {
93 my $ipcfail = run3( $cmd, $in_aref, $out_aref, $err_aref );
94  
95 # dmsg $ipcfail;
96  
97 ( $status, $oserr ) = ( $?, $! );
98  
99 if ( $ipcfail != 1 ) {
100 $_->( $self, ret => $ipcfail, cmd => $cmd, arg => $run_arg_href )
101 for $$global_cb{ipcfail}->@*;
102 }
103  
104 }
105 catch ($e) {
106 error($e);
107 $status = 500;
108 dmsg $self;
109 }
110  
111 # Success
112 if ( $status && $status == 0 ) {
113 $_->($status) for $global_cb->{success}->@*;
114 }
115 else { # Failure (TODO: look into why some exit codes are over 255)
116 $_->(
117  
118 $self,
119 exit => {
120 status_raw => $status,
121 status => $status,
122 os_errno => $oserr
123 },
124 args => [ $cmd, $in, $out, $err ]
125 ) for $$global_cb{ipcfail}->@*;
126 }
127  
128 $self;
129 }
130  
131 sub run ( $cmd, %arg ) {
132 my $nosh = IPC::Nosh->new(
133 cmd => $cmd,
134 %arg{@run_arg_allow}
135 );
136  
137 $nosh->$run($cmd);
138 $nosh;
139 }
140  
141 method in {
142 $$tied{in};
143 }
144  
145 method out {
146 $$tied{out};
147 }
148  
149 method err {
150 $$tied{err};
151 }
152  
153 __END__
« script/nosh.pl ↑ Summary lib/IPC/Nosh/Mux.pm »