| 1 | # | ||||||||||||||||||
| 2 | # Copyright (c) 2015-2022 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 | # Depends: (cp) | ||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | Chj::xIOUtil - exception-throwing I/O utilities | ||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | =head1 NOTE | ||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | This is alpha software! Read the status section in the package README | ||||||||||||||||||
| 24 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | =cut | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | package Chj::xIOUtil; | ||||||||||||||||||
| 29 | 23 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 30 | 23 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 31 | 23 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 32 | 23 | ▶ | use Exporter "import"; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | 23 | our @EXPORT = qw(); | |||||||||||||||||
| 35 | 23 | our @EXPORT_OK = qw( | |||||||||||||||||
| 36 | stdin_utf8 stdout_utf8 stderr_utf8 xgetfile_utf8 xputfile_utf8 | ||||||||||||||||||
| 37 | xputfile_bytes xcopyfile_utf8 xprint_object xcopyfile); | ||||||||||||||||||
| 38 | 23 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | 23 | ▶ | use Chj::xopen ":all"; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | # ^ well, this voids the purpose of Chj::xIO (to avoid Chj::IO::*) | ||||||||||||||||||
| 43 | 23 | ▶ | use FP::Lazy; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 44 | 23 | ▶ | use FP::List; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 45 | 23 | ▶ | use Chj::xperlfunc qw(xxsystem_safe xprint xgetfile_utf8); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 46 | 23 | ▶ | use FP::Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | 0 | ▶ | sub stdin_utf8() { | ||||||||||||||||
|
Subroutine
stdin_utf8: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 49 | 0 | my $fh = *STDIN{IO}; | |||||||||||||||||
| 50 | 0 | ▶ | binmode $fh, ":encoding(UTF-8)" or die $!; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 51 | 0 | $fh | |||||||||||||||||
| 52 | } | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | 0 | ▶ | sub stdout_utf8() { | ||||||||||||||||
|
Subroutine
stdout_utf8: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 55 | 0 | my $fh = *STDOUT{IO}; | |||||||||||||||||
| 56 | 0 | ▶ | binmode $fh, ":encoding(UTF-8)" or die $!; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 57 | 0 | $fh | |||||||||||||||||
| 58 | } | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | 0 | ▶ | sub stderr_utf8() { | ||||||||||||||||
|
Subroutine
stderr_utf8: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 61 | 0 | my $fh = *STDERR{IO}; | |||||||||||||||||
| 62 | 0 | ▶ | binmode $fh, ":encoding(UTF-8)" or die $!; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 63 | 0 | $fh | |||||||||||||||||
| 64 | } | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | # print, not write, i.e. flatten nested structures out, but don't | ||||||||||||||||||
| 67 | # print parens for lists etc., just print the contained basic types. | ||||||||||||||||||
| 68 | 0 | ▶ | sub xprint_object { | ||||||||||||||||
|
Subroutine
xprint_object: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||
| 69 | 0 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 70 | 0 | my ($fh, $v) = @_; | |||||||||||||||||
| 71 | 0 | ▶ | if (ref $v) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 72 | 0 | ▶ | if (ref($v) eq "ARRAY") { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 73 | 0 | xprint_object($fh, $_) for @$v; | |||||||||||||||||
| 74 | } elsif (is_pair $v) { | ||||||||||||||||||
| 75 | 0 | xprint_object($fh, car $v); | |||||||||||||||||
| 76 | 0 | xprint_object($fh, cdr $v); | |||||||||||||||||
| 77 | } elsif (is_promise $v) { | ||||||||||||||||||
| 78 | 0 | xprint_object($fh, force $v) | |||||||||||||||||
| 79 | } else { | ||||||||||||||||||
| 80 | 0 | die "don't know how to print a " . ref($v) . " ('$v')"; | |||||||||||||||||
| 81 | } | ||||||||||||||||||
| 82 | } else { | ||||||||||||||||||
| 83 | 0 | xprint $fh, $v | |||||||||||||||||
| 84 | } | ||||||||||||||||||
| 85 | } | ||||||||||||||||||
| 86 | |||||||||||||||||||
| 87 | 0 | ▶ | sub xputfile_utf8 { | ||||||||||||||||
|
Subroutine
xputfile_utf8: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||
| 88 | 0 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 89 | 0 | my ($path, $str) = @_; | |||||||||||||||||
| 90 | 0 | my $out = xopen_write($path); | |||||||||||||||||
| 91 | 0 | ▶ | binmode $out, ":encoding(UTF-8)" or die "binmode"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 92 | 0 | xprint_object($out, $str); | |||||||||||||||||
| 93 | 0 | $out->xclose; | |||||||||||||||||
| 94 | } | ||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | 0 | ▶ | sub xputfile_bytes { | ||||||||||||||||
|
Subroutine
xputfile_bytes: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||
| 97 | 0 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 98 | 0 | my ($path, $str) = @_; | |||||||||||||||||
| 99 | 0 | my $out = xopen_write($path); | |||||||||||||||||
| 100 | 0 | ▶ | binmode $out or die "binmode"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 101 | 0 | xprint_object($out, $str); | |||||||||||||||||
| 102 | 0 | $out->xclose; | |||||||||||||||||
| 103 | } | ||||||||||||||||||
| 104 | |||||||||||||||||||
| 105 | 0 | ▶ | sub xcopyfile_utf8 { | ||||||||||||||||
|
Subroutine
xcopyfile_utf8: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 106 | 0 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 107 | 0 | my ($src, $dest) = @_; | |||||||||||||||||
| 108 | 0 | xputfile_utf8($dest, xgetfile_utf8($src)); | |||||||||||||||||
| 109 | } | ||||||||||||||||||
| 110 | |||||||||||||||||||
| 111 | 0 | ▶ | sub xcopyfile { | ||||||||||||||||
|
Subroutine
xcopyfile: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 112 | 0 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 113 | 0 | my ($src, $dest) = @_; | |||||||||||||||||
| 114 | |||||||||||||||||||
| 115 | # yes, giving up here. XX write something else or just use | ||||||||||||||||||
| 116 | # Filecopy or whatever from CPAN. | ||||||||||||||||||
| 117 | 0 | xxsystem_safe "cp", "-a", "--", $src, $dest | |||||||||||||||||
| 118 | } | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | 23 | 1 | |||||||||||||||||