Quiq/Ipc.pm

filter: Statementstmt 83.3% 50 / 60 Branchbran 41.6% 5 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.6% 11 / 12 Podpod 100% 2 / 2 totaltotal 79.0% 68 / 86 CC 7 SCAR 13.8
CC
7
Cov
76%
pipeTo
12.0
filter
5.1
BEGIN
1.0
CRAP 4.0
« Quiq/JQuery/Tabs.pm ↑ Summary Quiq/Confluence/Page.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Ipc - Interprozesskommunikation
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =cut
14  
15 # -----------------------------------------------------------------------------
16  
17 package Quiq::Ipc;
18 6 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 6 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 6 our $VERSION = '1.240';
25  
26 6 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use Quiq::Shell;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use Quiq::Exit;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use IPC::Open3 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 6 ▶use Quiq::FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 6 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # -----------------------------------------------------------------------------
34  
35 =head1 METHODS
36  
37 =head2 Methods
38  
39 =head3 filter() - Rufe ein Kommando als Filter auf
40  
41 =head4 Synopsis
42  
43 $out = Quiq::Ipc->filter($cmd,$in,@opt);
44 ($out,$err) = Quiq::Ipc->filter($cmd,$in,@opt);
45 $out = Quiq::Ipc->filter($cmd,@opt);
46 ($out,$err) = Quiq::Ipc->filter($cmd,@opt);
47  
48 =head4 Options
49  
50 =over 4
51  
52 =item -ignoreError => $bool (Default: 0)
53  
54 Ignoriere Exitcode von Kommando $cmd. D.h. es wird keine Exception
55 geworfen, wenn das Kommando fehlschlägt.
56  
57 =back
58  
59 =head4 Description
60  
61 Rufe Kommando $cmd als Filter auf. Das Kommando erhält die Daten
62 $in auf stdin und liefert die Daten $out und $err auf stdout
63 bzw. stderr. In Skalarkontext wird nur die Ausgabe auf stdout
64 geliefert.
65  
66 Achtung: Der Aufruf kann zu einem SIGPIPE führen, wenn per
67 Parameter $in Daten an $cmd gesendet werden und das Kommando
68 terminiert, bevor es alle Daten gelesen hat. Insbesondere sollten
69 keine Daten an ein Kommando gesendet werden, das nicht von stdin
70 liest!
71  
72 =cut
73  
74 # -----------------------------------------------------------------------------
75  
76 3 ▶sub filter {
Subroutine filter: called CC=5 SCAR=16.2
Pod documented
77 3 my $class = shift;
78 3 my $cmd = shift;
79 # @_: $in,@opt -or- @opt
80
81 3 my $ignoreError = 0;
82  
83 3 Quiq::Option->extract(\@_,
84 -ignoreError => \$ignoreError,
85 );
86 3 my $in = shift;
87  
88 3 local (*W,*R,*E,$/);
89 3 my $pid = IPC::Open3::open3(\*W,\*R,\*E,$cmd);
90 2 ▶ unless ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 2 2
91 0 $class->throw(
92 'IPC-00001: Kann Filterkommando nicht forken',
93 Cmd => $cmd,
94 );
95 }
96  
97 2 ▶ if (defined $in) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in) 1 1 2
98 1 print W $in;
99 }
100 2 close W;
101  
102 2 my $out = <R>;
103 2 close R;
104  
105 2 my $err = <E>;
106 2 close E;
107  
108 2 waitpid $pid,0;
109 2 ▶ if (!$ignoreError) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignoreError) 2 0 2
110 # FIXME: checkError nach Quiq::Ipc verlagern,
111 # in checkExit umbenennen
112 # Quiq::Shell->checkError($?,$err,$cmd);
113 2 Quiq::Exit->check($?,"$cmd\n$err");
114 }
115  
116 1 ▶ return wantarray? ($out,$err): $out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
117 }
118  
119 # -----------------------------------------------------------------------------
120  
121 =head3 pipeTo() - Pipe Daten an Kommando
122  
123 =head4 Synopsis
124  
125 $class->pipeTo($data,$cmd,@options);
126  
127 =head4 Arguments
128  
129 =over 4
130  
131 =item $data
132  
133 Die Daten, die an das Kommando gepiped werden.
134  
135 =item $cmd
136  
137 Das Kommando, das die Daten von stdin liest und verarbeitet.
138  
139 =back
140  
141 =head4 Options
142  
143 =over 4
144  
145 =item -encoding => $encoding
146  
147 Mit welchem Encoding die Daten geschrieben werden.
148  
149 =item -toStdout => $bool (Default: 0)
150  
151 Pipe die Daten nicht an das Kommando, sondern schreibe sie nach STDOUT.
152 Dies ist nützlich, wenn das Kommando ein Pager ist, der nicht gestartet
153 werden soll, wenn STDOUT kein Terminal ist:
154  
155 Quiq::Ipc->pipeTo($data,'less -R',-toStdout=>!-t);
156  
157 =back
158  
159 =head4 Description
160  
161 Pipe Daten $data an Kommando $cmd.
162  
163 =cut
164  
165 # -----------------------------------------------------------------------------
166  
167 0 ▶sub pipeTo {
Subroutine pipeTo: not called CC=3 SCAR=24.8
Pod documented
168 0 my ($class,$data,$cmd) = splice @_,0,3;
169 # @_: @options
170  
171 0 my $encoding = undef;
172 0 my $toStdout = 0;
173  
174 0 my $opt = $class->parameters(\@_,
175 -encoding => \$encoding,
176 -toStdout => \$toStdout,
177 );
178  
179 0 ▶ my $fh = $toStdout? \*STDOUT: Quiq::FileHandle->new('|-',$cmd);
Branch: 1 branch
BranchTrueFalseTotal
$toStdout ? : 0 0 0
180 0 ▶ if ($encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding) 0 0 0
181 0 $data = Encode::encode($encoding,$data);
182 }
183 0 print $fh $data;
184  
185 0 return;
186 }
187  
188 # -----------------------------------------------------------------------------
189  
190 =head1 VERSION
191  
192 1.240
193  
194 =head1 AUTHOR
195  
196 Frank Seitz, L<http://fseitz.de/>
197  
198 =head1 COPYRIGHT
199  
200 Copyright (C) 2026 Frank Seitz
201  
202 =head1 LICENSE
203  
204 This code is free software; you can redistribute it and/or modify
205 it under the same terms as Perl itself.
206  
207 =cut
208  
209 # -----------------------------------------------------------------------------
210  
211 6 1;
212  
213 # eof
« Quiq/JQuery/Tabs.pm ↑ Summary Quiq/Confluence/Page.pm »