| 1 | # | ||||||||||
| 2 | # Copyright (c) 2013-2020 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 | Chj::xIO - some IO utilities | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use Chj::xIO qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | capture_stdout capture_stdout_ | ||||||||||
| 18 | capture_stderr capture_stderr_ | ||||||||||
| 19 | 1 | ); | |||||||||
| 20 | |||||||||||
| 21 | is capture_stdout { print "Hi!" }, "Hi!"; | ||||||||||
| 22 | is substr(capture_stderr { warn "nah" }, 0,3), "nah"; | ||||||||||
| 23 | |||||||||||
| 24 | # if you want to avoid the '&' prototype: | ||||||||||
| 25 | is capture_stdout_(sub { print "Hi!" }), "Hi!"; | ||||||||||
| 26 | |||||||||||
| 27 | 1 | ▶ | use Chj::xIO qw(with_output_to_file); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | my $res = with_output_to_file(".xIO-test-out", sub { print "Hi"; 123 }); | ||||||||||
| 29 | is $res, 123; | ||||||||||
| 30 | is do { open my $in, "<", ".xIO-test-out"; local $/; <$in> }, "Hi"; | ||||||||||
| 31 | |||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||
| 33 | |||||||||||
| 34 | Oh, there's Capture::Tiny ! Even uses the same names. TODO: move to | ||||||||||
| 35 | that. Although, Capture::Tiny might be using 'dup', which would not be | ||||||||||
| 36 | thread safe. | ||||||||||
| 37 | |||||||||||
| 38 | =head1 NOTE | ||||||||||
| 39 | |||||||||||
| 40 | This is alpha software! Read the status section in the package README | ||||||||||
| 41 | or on the L<website|http://functional-perl.org/>. | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | |||||||||||
| 45 | package Chj::xIO; | ||||||||||
| 46 | 86 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 86 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 86 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 86 | ▶ | use Exporter "import"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | |||||||||||
| 51 | 86 | our @EXPORT = qw(); | |||||||||
| 52 | 86 | our @EXPORT_OK = qw( | |||||||||
| 53 | capture_stdout capture_stdout_ | ||||||||||
| 54 | capture_stderr capture_stderr_ | ||||||||||
| 55 | with_output_to_file | ||||||||||
| 56 | ); | ||||||||||
| 57 | 86 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||
| 58 | |||||||||||
| 59 | 86 | ▶ | use FP::Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 60 | |||||||||||
| 61 | 72 | ▶ | sub capture_stdout_ { | ||||||||
|
Subroutine
capture_stdout_: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||
| 62 | 72 | my ($thunk) = @_; | |||||||||
| 63 | 72 | my $buf = ""; | |||||||||
| 64 | 72 | ▶ | open my $out, ">", \$buf or die $!; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | { | ||||||||||
| 66 | # XX threadsafe or not? | ||||||||||
| 67 | 72 | local *STDOUT = $out; | |||||||||
| 68 | 72 | &$thunk(); # dropping results | |||||||||
| 69 | } | ||||||||||
| 70 | 72 | ▶ | close $out or die $!; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 72 | $buf | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 45 | ▶ | sub capture_stdout (&) { | ||||||||
|
Subroutine
capture_stdout: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 75 | 45 | capture_stdout_(@_) | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | # stupid COPY-PASTE | ||||||||||
| 79 | |||||||||||
| 80 | 1 | ▶ | sub capture_stderr_ { | ||||||||
|
Subroutine
capture_stderr_: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||
| 81 | 1 | my ($thunk) = @_; | |||||||||
| 82 | 1 | my $buf = ""; | |||||||||
| 83 | 1 | ▶ | open my $out, ">", \$buf or die $!; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | { | ||||||||||
| 85 | # XX threadsafe or not? | ||||||||||
| 86 | 1 | local *STDERR = $out; | |||||||||
| 87 | 1 | &$thunk(); # dropping results | |||||||||
| 88 | } | ||||||||||
| 89 | 1 | ▶ | close $out or die $!; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 1 | $buf | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 1 | ▶ | sub capture_stderr (&) { | ||||||||
|
Subroutine
capture_stderr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 94 | 1 | capture_stderr_(@_) | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 1 | ▶ | sub with_output_to_file { | ||||||||
|
Subroutine
with_output_to_file: called CC=8 SCAR=22.9
Pod
undocumented
| |||||||||||
| 98 | 1 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 1 | my ($file, $thunk) = @_; | |||||||||
| 100 | 1 | my $wantarray = wantarray; ## no critic | |||||||||
| 101 | 1 | my @res; | |||||||||
| 102 | 1 | ▶ | open my $out, ">", $file | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | or fp_croak "with_output_to_file: open '$file': $!"; | ||||||||||
| 104 | 1 | ▶ | binmode $out, ":encoding(UTF-8)" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | or fp_croak "with_output_to_file: binmode '$file': $!"; | ||||||||||
| 106 | { | ||||||||||
| 107 | 1 | local *STDOUT = $out; | |||||||||
| 108 | 1 | ▶ | if (defined $wantarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 1 | ▶ | if ($wantarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 0 | @res = &$thunk(); | |||||||||
| 111 | } else { | ||||||||||
| 112 | 1 | @res = scalar &$thunk(); | |||||||||
| 113 | } | ||||||||||
| 114 | } else { | ||||||||||
| 115 | 0 | &$thunk(); | |||||||||
| 116 | } | ||||||||||
| 117 | } | ||||||||||
| 118 | 1 | ▶ | close $out or fp_croak "with_output_to_file: close '$file': $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 1 | ▶ | $wantarray ? @res : $res[0] | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 86 | 1 | |||||||||