| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2014-2021 Christian Jaeger, copying@christianjaeger.ch | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||||||||||||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||||||||||||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||||||||||||||||||||
| 7 | # bundled with this file. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | FP::IOStream | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | use FP::IOStream ':all'; # xdirectory_items, xdirectory_paths | ||||||||||||||||||||||||||||
| 17 | use FP::Stream; # stream_map | ||||||||||||||||||||||||||||
| 18 | use FP::List ':all'; # first | ||||||||||||||||||||||||||||
| 19 | my $paths = stream_map sub { my ($item) = @_; "$base/$item" }, | ||||||||||||||||||||||||||||
| 20 | xdirectory_items $base; | ||||||||||||||||||||||||||||
| 21 | # which is the same as: my $paths = xdirectory_paths $base; | ||||||||||||||||||||||||||||
| 22 | my $firstpath = first $paths; | ||||||||||||||||||||||||||||
| 23 | # ... | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Lazy IO (well, input), by reading items lazily as stream items. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | (It's arguable whether that is a good idea; Haskell uses different | ||||||||||||||||||||||||||||
| 30 | approaches nowadays. But it's still a nice way to do things if you're | ||||||||||||||||||||||||||||
| 31 | careful.) | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 NOTE | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | This is alpha software! Read the status section in the package README | ||||||||||||||||||||||||||||
| 36 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | package FP::IOStream; | ||||||||||||||||||||||||||||
| 41 | 25 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 25 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 25 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 25 | ▶ | use Exporter "import"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 25 | our @EXPORT = qw(); | |||||||||||||||||||||||||||
| 47 | 25 | our @EXPORT_OK = qw(maybeIO_to_stream | |||||||||||||||||||||||||||
| 48 | fh_to_stream | ||||||||||||||||||||||||||||
| 49 | perhaps_directory_items | ||||||||||||||||||||||||||||
| 50 | perhaps_directory_paths | ||||||||||||||||||||||||||||
| 51 | xdirectory_items | ||||||||||||||||||||||||||||
| 52 | xdirectory_paths | ||||||||||||||||||||||||||||
| 53 | xfile_lines xfile_lines0 xfile_lines0chop xfile_lines_chomp | ||||||||||||||||||||||||||||
| 54 | xfile_chars | ||||||||||||||||||||||||||||
| 55 | fh_to_lines | ||||||||||||||||||||||||||||
| 56 | fh_to_chunks | ||||||||||||||||||||||||||||
| 57 | timestream | ||||||||||||||||||||||||||||
| 58 | xstream_print | ||||||||||||||||||||||||||||
| 59 | xstream_to_file | ||||||||||||||||||||||||||||
| 60 | xfile_replace_lines | ||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||
| 62 | 25 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 25 | ▶ | use FP::Lazy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 25 | ▶ | use Chj::xopendir qw(perhaps_opendir); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | 25 | ▶ | use FP::List ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | 25 | ▶ | use FP::Stream qw(stream_map weaken Weakened); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 25 | ▶ | use FP::PureArray qw(array_to_purearray); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 25 | ▶ | use FP::Array_sort; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | 25 | ▶ | use FP::Ops 'the_method'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | 25 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 25 | ▶ | use Chj::singlequote ":all"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 25 | ▶ | use Chj::xopen qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | xopen_read | ||||||||||||||||||||||||||||
| 75 | xopen_write | ||||||||||||||||||||||||||||
| 76 | xopen_append | ||||||||||||||||||||||||||||
| 77 | xopen_update | ||||||||||||||||||||||||||||
| 78 | possibly_fh_to_fh | ||||||||||||||||||||||||||||
| 79 | glob_to_fh | ||||||||||||||||||||||||||||
| 80 | 25 | ); | |||||||||||||||||||||||||||
| 81 | 25 | ▶ | use Chj::xtmpfile qw(xtmpfile); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | 25 | ▶ | use FP::Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # XX use this for the definitions further below instead of re-coding | ||||||||||||||||||||||||||||
| 85 | # it each time? | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub maybeIO_to_stream { | ||||||||||||||||||||||||||
|
Subroutine
maybeIO_to_stream: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 87 | 0 | my ($maybeIO, $maybe_close) = @_; | |||||||||||||||||||||||||||
| 88 | 0 | my $next; | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | $next = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 90 | 0 | my $next = $next; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | lazyT { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if (defined(my $v = &$maybeIO())) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | cons($v, &$next) | |||||||||||||||||||||||||||
| 94 | } else { | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if (defined $maybe_close) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | &$maybe_close() | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | null | ||||||||||||||||||||||||||||
| 99 | 0 | } | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 0 | "FP::List::List" | |||||||||||||||||||||||||||
| 102 | 0 | }; | |||||||||||||||||||||||||||
| 103 | 0 | &{ Weakened $next} | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub _perhaps_opendir_stream { | ||||||||||||||||||||||||||
|
Subroutine
_perhaps_opendir_stream: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | my ($path) = @_; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | if (my ($d) = perhaps_opendir $path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | my $next; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | $next = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 112 | 0 | my $next = $next; | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | lazyT { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if (defined(my $item = $d->xnread)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | cons $item, &$next | |||||||||||||||||||||||||||
| 116 | } else { | ||||||||||||||||||||||||||||
| 117 | 0 | $d->xclose; | |||||||||||||||||||||||||||
| 118 | 0 | null | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 0 | "FP::List::List" | |||||||||||||||||||||||||||
| 122 | 0 | }; | |||||||||||||||||||||||||||
| 123 | 0 | &{ Weakened $next} | |||||||||||||||||||||||||||
| 124 | } else { | ||||||||||||||||||||||||||||
| 125 | () | ||||||||||||||||||||||||||||
| 126 | 0 | } | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub _perhaps_opendir_stream_sorted { | ||||||||||||||||||||||||||
|
Subroutine
_perhaps_opendir_stream_sorted: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | my ($path, $cmp) = @_; | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | if (my ($d) = perhaps_opendir $path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | my $items = array_sort [$d->xnread], $cmp; | |||||||||||||||||||||||||||
| 134 | 0 | $d->xclose; | |||||||||||||||||||||||||||
| 135 | 0 | array_to_purearray $items | |||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||
| 137 | () | ||||||||||||||||||||||||||||
| 138 | 0 | } | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub perhaps_directory_items { | ||||||||||||||||||||||||||
|
Subroutine
perhaps_directory_items: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ >= 1 and @_ <= 2
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1
@_ <= 2
Truth table0%0/3
A and B
A= @_ >= 1B= @_ <= 2
| |||||||||||||||||||||||||||||
| 143 | 0 | my ($path, $maybe_cmp) = @_; | |||||||||||||||||||||||||||
| 144 | 0 | ▶ | if ($maybe_cmp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | _perhaps_opendir_stream_sorted $path, $maybe_cmp; | |||||||||||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||||||||||
| 147 | 0 | _perhaps_opendir_stream $path; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub perhaps_directory_paths { | ||||||||||||||||||||||||||
|
Subroutine
perhaps_directory_paths: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ >= 1 and @_ <= 2
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1
@_ <= 2
Truth table0%0/3
A and B
A= @_ >= 1B= @_ <= 2
| |||||||||||||||||||||||||||||
| 153 | 0 | my ($base, $maybe_cmp) = @_; | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | $base .= "/" unless $base =~ /\/\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if (my ($s) = perhaps_directory_items $base, $maybe_cmp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | $s->map( | ||||||||||||||||||||||||||||
| 157 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 158 | 0 | my ($item) = @_; | |||||||||||||||||||||||||||
| 159 | 0 | "$base$item" | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | ) | ||||||||||||||||||||||||||||
| 162 | 0 | } else { | |||||||||||||||||||||||||||
| 163 | () | ||||||||||||||||||||||||||||
| 164 | 0 | } | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | sub xdirectory_items { | ||||||||||||||||||||||||||
|
Subroutine
xdirectory_items: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ >= 1 and @_ <= 2
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1
@_ <= 2
Truth table0%0/3
A and B
A= @_ >= 1B= @_ <= 2
| |||||||||||||||||||||||||||||
| 169 | 0 | my ($path, $maybe_cmp) = @_; | |||||||||||||||||||||||||||
| 170 | 0 | ▶ | if (my ($s) = perhaps_directory_items($path, $maybe_cmp)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | $s | |||||||||||||||||||||||||||
| 172 | } else { | ||||||||||||||||||||||||||||
| 173 | 0 | croak "xdirectory_items(" . singlequote_many(@_) . "): $!"; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub xdirectory_paths { | ||||||||||||||||||||||||||
|
Subroutine
xdirectory_paths: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ >= 1 and @_ <= 2
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1
@_ <= 2
Truth table0%0/3
A and B
A= @_ >= 1B= @_ <= 2
| |||||||||||||||||||||||||||||
| 179 | 0 | my ($path, $maybe_cmp) = @_; | |||||||||||||||||||||||||||
| 180 | 0 | ▶ | if (my ($s) = perhaps_directory_paths($path, $maybe_cmp)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | $s | |||||||||||||||||||||||||||
| 182 | } else { | ||||||||||||||||||||||||||||
| 183 | 0 | croak "xdirectory_paths(" . singlequote_many(@_) . "): $!"; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 19 | ▶ | sub fh_to_stream { | ||||||||||||||||||||||||||
|
Subroutine
fh_to_stream: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 188 | 19 | ▶ | @_ == 3 or fp_croak_arity 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 19 | my ($fh, $read, $close) = @_; | |||||||||||||||||||||||||||
| 190 | 19 | my $next; | |||||||||||||||||||||||||||
| 191 | 59 | ▶ | $next = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 192 | 59 | my $next = $next; | |||||||||||||||||||||||||||
| 193 | 59 | ▶ | lazyT { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 194 | 59 | ▶ | if (defined(my $item = &$read($fh))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 40 | cons $item, &$next | |||||||||||||||||||||||||||
| 196 | } else { | ||||||||||||||||||||||||||||
| 197 | 19 | &$close($fh); | |||||||||||||||||||||||||||
| 198 | 19 | null | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | 59 | "FP::List::List" | |||||||||||||||||||||||||||
| 202 | 19 | }; | |||||||||||||||||||||||||||
| 203 | 19 | &{ Weakened $next} | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # And (all?, no, can't proxy 'xopen' for both in and out) some of the | ||||||||||||||||||||||||||||
| 207 | # Chj::xopen functions: | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 125 | ▶ | sub make_open_stream { | ||||||||||||||||||||||||||
|
Subroutine
make_open_stream: called CC=2 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 210 | 125 | my ($open, $read, $maybe_close) = @_; | |||||||||||||||||||||||||||
| 211 | 125 | ▶ | my $close = $maybe_close // the_method("xclose"); | ||||||||||||||||||||||||||
|
Condition33%1/3
$maybe_close // the_method("xclose")
MC/DC0%0/2
$maybe_close // the_method("xclose")
$maybe_close
the_method("xclose")
Truth table33%1/3
A // B
A= $maybe_closeB= the_method("xclose")
| |||||||||||||||||||||||||||||
| 212 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 213 | 0 | ▶ | @_ == 1 or @_ == 2 or fp_croak_arity("1 or 2"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 1 or @_ == 2
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1
@_ == 2
Truth table0%0/3
A or B
A= @_ == 1B= @_ == 2
| |||||||||||||||||||||||||||||
| 214 | 0 | my ($path, $maybe_encoding) = @_; | |||||||||||||||||||||||||||
| 215 | 0 | my $fh = &$open($path); | |||||||||||||||||||||||||||
| 216 | 0 | ▶ | if ($maybe_encoding) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 0 | ▶ | binmode($fh, ":encoding($maybe_encoding)") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | or croak "binmode for :encoding($maybe_encoding): $!"; | ||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 0 | fh_to_stream($fh, $read, $close) | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | 125 | } | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | sub xfile_lines; | ||||||||||||||||||||||||||||
| 225 | 25 | *xfile_lines = make_open_stream(\&xopen_read, the_method("xreadline")); | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | sub xfile_lines0; | ||||||||||||||||||||||||||||
| 228 | 25 | *xfile_lines0 = make_open_stream(\&xopen_read, the_method("xreadline0")); | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | sub xfile_lines0chop; | ||||||||||||||||||||||||||||
| 231 | 25 | *xfile_lines0chop | |||||||||||||||||||||||||||
| 232 | = make_open_stream(\&xopen_read, the_method("xreadline0_chop")); | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | sub xfile_lines_chomp; | ||||||||||||||||||||||||||||
| 235 | 25 | *xfile_lines_chomp | |||||||||||||||||||||||||||
| 236 | = make_open_stream(\&xopen_read, the_method("xreadline_chomp")); | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | sub xfile_chars; | ||||||||||||||||||||||||||||
| 239 | 25 | *xfile_chars = make_open_stream(\&xopen_read, the_method("xreadchar")); | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | # Clojure calls this line-seq | ||||||||||||||||||||||||||||
| 242 | # (http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/line-seq) | ||||||||||||||||||||||||||||
| 243 | 0 | ▶ | sub fh_to_lines { | ||||||||||||||||||||||||||
|
Subroutine
fh_to_lines: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 244 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 0 | my ($fh) = @_; | |||||||||||||||||||||||||||
| 246 | 0 | fh_to_stream(possibly_fh_to_fh($fh), the_method("xreadline"), | |||||||||||||||||||||||||||
| 247 | the_method("xclose")) | ||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # read filehandle in chunks, although the chunk size, even of the | ||||||||||||||||||||||||||||
| 251 | # chunks before the last one, is only guaranteed to be non-zero, not | ||||||||||||||||||||||||||||
| 252 | # bufiz (since only xsysreadcompletely would guarantee to fill size, | ||||||||||||||||||||||||||||
| 253 | # but would die on mid-chunk EOF) | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 19 | ▶ | sub fh_to_chunks { | ||||||||||||||||||||||||||
|
Subroutine
fh_to_chunks: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 256 | 19 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 19 | my ($fh, $bufsiz) = @_; | |||||||||||||||||||||||||||
| 258 | fh_to_stream( | ||||||||||||||||||||||||||||
| 259 | possibly_fh_to_fh($fh), | ||||||||||||||||||||||||||||
| 260 | 59 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 261 | 59 | my $buf; | |||||||||||||||||||||||||||
| 262 | 59 | my $n = $fh->xsysread($buf, $bufsiz); | |||||||||||||||||||||||||||
| 263 | 59 | ▶ | $n == 0 ? undef : $buf | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | }, | ||||||||||||||||||||||||||||
| 265 | 19 | the_method("xclose") | |||||||||||||||||||||||||||
| 266 | ); | ||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | # A stream of floating-point unix timestamps representing the time | ||||||||||||||||||||||||||||
| 270 | # when each cell is being forced. Optional argument in seconds | ||||||||||||||||||||||||||||
| 271 | # (floating point) to sleep before returning the next element. | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 0 | ▶ | sub timestream { | ||||||||||||||||||||||||||
|
Subroutine
timestream: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 274 | 0 | ▶ | @_ >= 0 and @_ <= 1 or fp_croak_arity "0-1"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ >= 0 and @_ <= 1
MC/DC0%0/2
@_ >= 0 and @_ <= 1
@_ >= 0
@_ <= 1
Truth table0%0/3
A and B
A= @_ >= 0B= @_ <= 1
| |||||||||||||||||||||||||||||
| 275 | 0 | my ($maybe_sleep) = @_; | |||||||||||||||||||||||||||
| 276 | 0 | require Time::HiRes; | |||||||||||||||||||||||||||
| 277 | 0 | my $lp; | |||||||||||||||||||||||||||
| 278 | 0 | ▶ | $lp = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 279 | 0 | ▶ | lazyT { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 280 | 0 | ▶ | Time::HiRes::sleep($maybe_sleep) if $maybe_sleep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 0 | cons(Time::HiRes::time(), &$lp()) | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | 0 | "FP::List::Pair" | |||||||||||||||||||||||||||
| 284 | 0 | }; | |||||||||||||||||||||||||||
| 285 | 0 | Weakened($lp)->(); | |||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 0 | ▶ | sub xstream_print { | ||||||||||||||||||||||||||
|
Subroutine
xstream_print: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 289 | 0 | ▶ | @_ == 1 or @_ == 2 or fp_croak_arity "1 or 2"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 1 or @_ == 2
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1
@_ == 2
Truth table0%0/3
A or B
A= @_ == 1B= @_ == 2
| |||||||||||||||||||||||||||||
| 290 | 0 | my ($s, $maybe_fh) = @_; | |||||||||||||||||||||||||||
| 291 | 0 | ▶ | my $fh = $maybe_fh // glob_to_fh(*STDOUT); | ||||||||||||||||||||||||||
|
Condition0%0/3
$maybe_fh // glob_to_fh(*STDOUT)
MC/DC0%0/2
$maybe_fh // glob_to_fh(*STDOUT)
$maybe_fh
glob_to_fh(*STDOUT)
Truth table0%0/3
A // B
A= $maybe_fhB= glob_to_fh(*STDOUT)
| |||||||||||||||||||||||||||||
| 292 | 0 | weaken $_[0]; | |||||||||||||||||||||||||||
| 293 | $s->for_each( | ||||||||||||||||||||||||||||
| 294 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 295 | 0 | ▶ | print $fh $_[0] or croak "xstream_print: writing to $fh: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | 0 | ); | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 0 | ▶ | sub xstream_to_file { | ||||||||||||||||||||||||||
|
Subroutine
xstream_to_file: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 301 | 0 | ▶ | @_ == 2 or @_ == 3 or fp_croak_arity "2 or 3"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 2 or @_ == 3
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2
@_ == 3
Truth table0%0/3
A or B
A= @_ == 2B= @_ == 3
| |||||||||||||||||||||||||||||
| 302 | 0 | my ($s, $path, $maybe_mode) = @_; | |||||||||||||||||||||||||||
| 303 | 0 | my $out = xtmpfile $path; | |||||||||||||||||||||||||||
| 304 | 0 | weaken $_[0]; | |||||||||||||||||||||||||||
| 305 | 0 | xstream_print($s, $out); | |||||||||||||||||||||||||||
| 306 | 0 | $out->xclose; | |||||||||||||||||||||||||||
| 307 | 0 | $out->xputback($maybe_mode); | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # read and write back a file, passing its lines as a stream to the | ||||||||||||||||||||||||||||
| 311 | # given function; written to temp file that's renamed into place upon | ||||||||||||||||||||||||||||
| 312 | # successful completion. | ||||||||||||||||||||||||||||
| 313 | 0 | ▶ | sub xfile_replace_lines { | ||||||||||||||||||||||||||
|
Subroutine
xfile_replace_lines: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 314 | 0 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 0 | my ($path, $fn) = @_; | |||||||||||||||||||||||||||
| 316 | 0 | xstream_to_file &$fn(xfile_lines $path), $path; | |||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 25 | 1 | |||||||||||||||||||||||||||