| 1 | package Hook::Output::Tiny; | ||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 9 | our $VERSION = '1.02'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 10 | # Auto generate the stdout() and stderr() methods, and their private | ||||||||||||||||||||||||||||
| 11 | # helper counterparts | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 9 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 9 | for ('stdout', 'stderr') { | |||||||||||||||||||||||||||
| 16 | 18 | my $sub_name = $_; # We need to make a copy | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Public | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 30 | ▶ | *$_ = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 21 | 30 | my ($self) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 30 | ▶ | if (! wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 2 | warn "Calling $sub_name() in non-list context is deprecated!\n"; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | return defined $self->{$sub_name}{data} | ||||||||||||||||||||||||||||
| 27 | ? split /\n/, $self->{$sub_name}{data} | ||||||||||||||||||||||||||||
| 28 | 30 | ▶ | : @{[ () ]}; # Empty list | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 18 | }; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Private | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 18 | my $private_sub_name = "_$sub_name"; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 24 | ▶ | *$private_sub_name = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 36 | 24 | my ($self) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 24 | my $HANDLE = uc $sub_name; | |||||||||||||||||||||||||||
| 39 | 24 | ▶ | open $self->{$sub_name}{handle}, '>&', $HANDLE | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | or croak("can't hook " . uc $sub_name . ": $!"); | ||||||||||||||||||||||||||||
| 41 | 24 | close $HANDLE; | |||||||||||||||||||||||||||
| 42 | 24 | ▶ | open $HANDLE, '>>', \$self->{$sub_name}{data} or croak($!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 18 | }; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 12 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 12 | my %struct = map { $_ => {_struct()} } qw(stderr stdout); | |||||||||||||||||||||||||||
| 48 | 12 | return bless \%struct, $_[0]; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 18 | ▶ | sub hook { | ||||||||||||||||||||||||||
|
Subroutine
hook: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 18 | my ($self, $handle) = @_; | |||||||||||||||||||||||||||
| 52 | 18 | ▶ | $_ eq 'stderr' ? $self->_stderr : $self->_stdout for _handles($handle); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 19 | ▶ | sub unhook { | ||||||||||||||||||||||||||
|
Subroutine
unhook: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 19 | my ($self, $handle) = @_; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 19 | for (_handles($handle)) { | |||||||||||||||||||||||||||
| 58 | 9 | ▶ | no strict 'refs'; # To allow a string as STDOUT/STDERR bareword handles | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 26 | close uc $_; | |||||||||||||||||||||||||||
| 60 | 26 | ▶ | open uc $_, '>&', $self->{$_}{handle} or croak($!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Commenting out include() and exclude(). They're to be used to filter the | ||||||||||||||||||||||||||||
| 65 | # output. They have no docs nor tests yet. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | #sub include { | ||||||||||||||||||||||||||||
| 68 | # my ($self, $include) = @_; | ||||||||||||||||||||||||||||
| 69 | # | ||||||||||||||||||||||||||||
| 70 | # if (defined $include) { | ||||||||||||||||||||||||||||
| 71 | # if (ref $include ne 'ARRAY') { | ||||||||||||||||||||||||||||
| 72 | # croak("include() requires an array of regex objects sent in"); | ||||||||||||||||||||||||||||
| 73 | # } | ||||||||||||||||||||||||||||
| 74 | # if (! defined $include->[0]) { | ||||||||||||||||||||||||||||
| 75 | # croak("include() requires at least one regex object within the array reference"); | ||||||||||||||||||||||||||||
| 76 | # } | ||||||||||||||||||||||||||||
| 77 | # for (@$include) { | ||||||||||||||||||||||||||||
| 78 | # if (ref $_ ne 'REGEX') { | ||||||||||||||||||||||||||||
| 79 | # croak("include()'s array reference must only contain regex objects"); | ||||||||||||||||||||||||||||
| 80 | # } | ||||||||||||||||||||||||||||
| 81 | # } | ||||||||||||||||||||||||||||
| 82 | # $self->{include} = $include; | ||||||||||||||||||||||||||||
| 83 | # } | ||||||||||||||||||||||||||||
| 84 | # | ||||||||||||||||||||||||||||
| 85 | # return $self->{include} // []; | ||||||||||||||||||||||||||||
| 86 | #} | ||||||||||||||||||||||||||||
| 87 | #sub exclude { | ||||||||||||||||||||||||||||
| 88 | # my ($self, $exclude) = @_; | ||||||||||||||||||||||||||||
| 89 | # | ||||||||||||||||||||||||||||
| 90 | # if (defined $exclude) { | ||||||||||||||||||||||||||||
| 91 | # if (ref $exclude ne 'ARRAY') { | ||||||||||||||||||||||||||||
| 92 | # croak("exclude() requires an array of regex objects sent in"); | ||||||||||||||||||||||||||||
| 93 | # } | ||||||||||||||||||||||||||||
| 94 | # if (! defined $exclude->[0]) { | ||||||||||||||||||||||||||||
| 95 | # croak("exclude() requires at least one regex object within the array reference"); | ||||||||||||||||||||||||||||
| 96 | # } | ||||||||||||||||||||||||||||
| 97 | # for (@$exclude) { | ||||||||||||||||||||||||||||
| 98 | # if (ref $_ ne 'REGEX') { | ||||||||||||||||||||||||||||
| 99 | # croak("exclude()'s array reference must only contain regex objects"); | ||||||||||||||||||||||||||||
| 100 | # } | ||||||||||||||||||||||||||||
| 101 | # } | ||||||||||||||||||||||||||||
| 102 | # $self->{exclude} = $exclude; | ||||||||||||||||||||||||||||
| 103 | # } | ||||||||||||||||||||||||||||
| 104 | # | ||||||||||||||||||||||||||||
| 105 | # return $self->{exclude} // []; | ||||||||||||||||||||||||||||
| 106 | #} | ||||||||||||||||||||||||||||
| 107 | 8 | ▶ | sub flush { | ||||||||||||||||||||||||||
|
Subroutine
flush: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 8 | my ($self, $handle) = @_; | |||||||||||||||||||||||||||
| 109 | 8 | delete $self->{$_}{data} for _handles($handle); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 6 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 6 | my ($self, $fn, $handle) = @_; | |||||||||||||||||||||||||||
| 113 | 6 | ▶ | if ($fn eq 'stderr' || $fn eq 'stdout'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$fn eq "stderr" or $fn eq "stdout"
MC/DC50%1/2
$fn eq "stderr" or $fn eq "stdout"
$fn eq "stderr"
$fn eq "stdout"
Truth table66%2/3
A or B
A= $fn eq "stderr"B= $fn eq "stdout"
| |||||||||||||||||||||||||||||
| 114 | 1 | croak("write() requires a file name sent in before the handle\n"); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 5 | for (_handles($handle)){ | |||||||||||||||||||||||||||
| 118 | 4 | ▶ | open my $wfh, '>>', $fn or croak($!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 4 | print $wfh $self->{$_}{data}; | |||||||||||||||||||||||||||
| 120 | 4 | close $wfh; | |||||||||||||||||||||||||||
| 121 | 4 | $self->flush($_); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 24 | ▶ | sub _struct { | ||||||||||||||||||||||||||
|
Subroutine
_struct: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 125 | 24 | return (handle => *fh, data => ''); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 50 | ▶ | sub _handles { | ||||||||||||||||||||||||||
|
Subroutine
_handles: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 128 | 50 | my ($handle) = @_; | |||||||||||||||||||||||||||
| 129 | 50 | my $sub = (caller(1))[3]; | |||||||||||||||||||||||||||
| 130 | 50 | ▶ | _check_param($sub, $handle) if $handle; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 46 | ▶ | return $handle ? ($handle) : qw(stderr stdout); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 34 | ▶ | sub _check_param { | ||||||||||||||||||||||||||
|
Subroutine
_check_param: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 134 | # validates the $handle param | ||||||||||||||||||||||||||||
| 135 | 34 | my ($sub, $handle) = @_; | |||||||||||||||||||||||||||
| 136 | 34 | ▶ | if (! grep {$handle eq $_} qw(stderr stdout)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 4 | croak( | |||||||||||||||||||||||||||
| 138 | "$sub() either takes 'stderr', 'stdout' or no params\n" . | ||||||||||||||||||||||||||||
| 139 | "You supplied '$handle'\n" | ||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 9 | 1; | |||||||||||||||||||||||||||
| 145 | __END__ | ||||||||||||||||||||||||||||