lib/IPC/Nosh/Mux.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.5% 29 / 31 CC 1 SCAR 0.4
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/IPC/Nosh.pm ↑ Summary
1 1 ▶use Object::Pad ':experimental(:all)';
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package IPC::Nosh::Mux;
4  
5 class IPC::Nosh::Mux;
6  
7 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Encode qw'encode decode';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use List::Util qw'any none first all';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Stream::Buffered;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 0 ▶use IO::Handle::Common;
Subroutine BEGIN: called CC=1 SCAR=2.6
15 use IO::Handle::Common::Handle;
16  
17 const our @CB_GLOBAL_ALLOW => qw'line eof';
18  
19 const our %MUX_DEFAULT => (
20 fd => *STDOUT,
21 mode => ">",
22 autochomp => undef,
23 autoflush => undef
24 );
25  
26 field @array;
27  
28 field $charset : param = 'UTF-8';
29 field $fileno : reader(fd) //= *STDOUT;
30 field $mode : param : reader //= $MUX_DEFAULT{mode};
31 field $autochomp : param : reader //= undef;
32 field $autoflush : param : reader //= undef;
33  
34 field $buff : reader //= undef;
35  
36 field $default_handle = IO::Handle::Common::Handle->new(
37 fd => $MUX_DEFAULT{fd},
38 mode => $MUX_DEFAULT{mode}
39 );
40  
41 field $handle : reader = [];
42 field $callback : accessor(on) //= {};
43  
44 ADJUST : params (:$fn //= undef, :$fh //= undef, :$fd //= undef) {
45 my @argref = ( \$fn, \$fh, \$fd );
46 my %handleopt = (
47 mode => $mode,
48 autochomp => $autochomp,
49 autoflush => $autoflush,
50 encoding => 'UTF-8'
51 );
52  
53 foreach my $to_handle ( $fn, $fh, $fd ) {
54 if ($fn) {
55 push @$handle,
56 IO::Handle::Common::Handle->new(
57 fn => $fn,
58 %handleopt
59 );
60 }
61 elsif ($fh) {
62 if ( $fh isa ARRAY && scalar @$fh ) {
63 foreach my $fh (@$fh) {
64 if ( $fh isa HASH )
65 { # allowed keys autochomp, autoflush, ...
66 ...;
67 }
68 elsif ( $fh isa GLOB ) {
69 push @$handle,
70 IO::Handle::Common::Handle->new(
71 fh => $fh,
72 %handleopt
73 );
74 }
75 }
76 }
77  
78 }
79 elsif ($fd) {
80 push @$handle,
81 IO::Handle::Common::Handle->new( fd => $fd, %handleopt );
82 }
83 else {
84 $buff = Stream::Buffered->new();
85  
86 push @$handle,
87 IO::Handle::Common::Handle->new(
88 fh => $buff->rewind,
89 %handleopt
90 );
91 }
92 }
93 };
94  
95 ADJUST : params (:$on //= {}) {
96  
97 # dmsg $on;
98  
99 foreach my ( $e, $val ) (%$on) {
100 if ( none { $e eq $_ } @IPC::Nosh::Mux::CB_GLOBAL_ALLOW ) {
101 error "'$e' is not a valid key for '\$on'";
102 next;
103 }
104  
105 $$callback{$e} //= [];
106  
107 if ( ( ref $val eq 'ARRAY' ) && ( all { ref $_ eq 'CODE' } @$val ) ) {
108 push $$callback{$e}->@*, @$val;
109 }
110 elsif ( ref $val eq 'CODE' ) {
111 push $$callback{$e}->@*, $val;
112 }
113 elsif ($val) {
114  
115 fatal "'$e' must be a CODE or an ARRAY of CODE";
116 }
117 }
118  
119 };
120  
121 ADJUST {
122 if (
123 none { $_ }
124 map { $$callback{$_}->@* }
125 grep { ref $$callback{$_} && ref $$callback{$_} eq 'ARRAY' }
126 keys %$callback,
127 @$handle
128 )
129 {
130 # dmsg $self;
131 push @$handle, $default_handle;
132 }
133  
134 }
135  
136 method mux_default_args : common {
137 %MUX_DEFAULT;
138 }
139  
140 method on_line ( $line, $line_no = undef ) {
141  
142 # TODO: Check sub signature for number of args and pass only those
143 $_->( $line, $line_no, $self ) for $$callback{line}->@*;
144 }
145  
146 method PUSH (@list) {
147 push @array, map {
148 my $line = $_;
149 chomp $line if $autochomp;
150  
151 $_->say($line) for @$handle;
152 $self->on_line($line);
153 $line
154 } @list;
155  
156 $self->FETCHSIZE;
157 }
158  
159 method STORE( $index, $value ) {
160 chomp $value if $autochomp;
161  
162 $_->say($value) for @$handle;
163  
164 $array[$index] = $value;
165 $self->on_line( $value, $index );
166  
167 $index > $self->FETCHSIZE ? undef : $index;
168 }
169  
170 method STORESIZE ($count) {
171 if ( $count > $self->FETCHSIZE .. $count ) {
172 foreach ( $count = $self->FETCHSIZE .. $count ) {
173 $self->STORE( $count, '' );
174 }
175 }
176 elsif ( $count < $self->FETCHSIZE ) {
177 foreach ( 0 .. $self->FETCHSIZE - $count - 2 ) {
178 pop @array;
179 }
180 }
181 }
182  
183 method EXTEND ($count) {
184  
185 }
186  
187 method FETCH ($index) {
188 $array[$index];
189 }
190  
191 method FETCHSIZE {
192 scalar @array;
193 }
194  
195 method CLEAR {
196 @array = ();
197 }
198  
199 method POP {
200 pop @array;
201 }
202  
203 method SHIFT {
204 shift @array;
205 }
206  
207 method UNSHIFT (@list) {
208 unshift @array, @list;
209 $self->FETCHSIZE;
210 }
211  
212 method DELETE ($index) {
213 $self->STORE( $index, undef );
214 }
215  
216 method TIEARRAY : common ( %opt ) {
217 my $self = $class->new(
218 map { ( $_ => $opt{$_} ) }
219 grep { $opt{$_} } qw(on fd sub mode fh fn autochomp autoflush)
220 );
221  
222 $self;
223 }
224  
225 method lines (%opt) {
226 map {
227 $opt{encode} && $opt{encode} ne $charset
228 ? encode( $_, $opt{encode} )
229 : $_
230 } @array;
231 }
232  
233 method lines_utf8 (%opt) {
234 $self->lines( encode => 'UTF-8' );
235 }
236  
237 method joined (%opt) {
238 join( $autochomp ? "" : "\n" ), $self->lines(%opt);
239 }
240  
241 method joined_utf8 (%opt) {
242 $self->joined(%opt);
243 }
« lib/IPC/Nosh.pm ↑ Summary