line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hook::Output::Tiny; |
2
|
9
|
|
|
9
|
|
71572
|
use strict; |
|
9
|
|
|
|
|
65
|
|
|
9
|
|
|
|
|
264
|
|
3
|
9
|
|
|
9
|
|
46
|
use warnings; |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
360
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.00'; |
6
|
|
|
|
|
|
|
|
7
|
9
|
|
|
9
|
|
53
|
use Carp qw(croak); |
|
9
|
|
|
|
|
19
|
|
|
9
|
|
|
|
|
903
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
BEGIN { |
10
|
|
|
|
|
|
|
# Auto generate the stdout() and stderr() methods, and their private |
11
|
|
|
|
|
|
|
# helper counterparts |
12
|
|
|
|
|
|
|
|
13
|
9
|
|
|
9
|
|
62
|
no strict 'refs'; |
|
9
|
|
|
|
|
26
|
|
|
9
|
|
|
|
|
2857
|
|
14
|
|
|
|
|
|
|
|
15
|
9
|
|
|
9
|
|
47
|
for ('stdout', 'stderr') { |
16
|
18
|
|
|
|
|
49
|
my $sub_name = $_; # We need to make a copy |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Public |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
*$_ = sub { |
21
|
30
|
|
|
30
|
|
2596
|
my ($self) = @_; |
22
|
|
|
|
|
|
|
|
23
|
30
|
100
|
|
|
|
71
|
if (!wantarray) { |
24
|
2
|
|
|
|
|
11
|
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
|
100
|
|
|
|
164
|
: @{[ () ]}; # Empty list |
|
8
|
|
|
|
|
28
|
|
29
|
18
|
|
|
|
|
121
|
}; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# Private |
32
|
|
|
|
|
|
|
|
33
|
18
|
|
|
|
|
54
|
my $private_sub_name = "_$sub_name"; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
*$private_sub_name = sub { |
36
|
24
|
|
|
24
|
|
3111
|
my ($self) = @_; |
37
|
|
|
|
|
|
|
|
38
|
24
|
|
|
|
|
58
|
my $HANDLE = uc $sub_name; |
39
|
24
|
50
|
|
|
|
718
|
open $self->{$sub_name}{handle}, ">&$HANDLE" |
40
|
|
|
|
|
|
|
or croak("can't hook " . uc $sub_name . ": $!"); |
41
|
24
|
|
|
|
|
207
|
close $HANDLE; |
42
|
24
|
50
|
|
7
|
|
407
|
open $HANDLE, '>>', \$self->{$sub_name}{data} or croak($!); |
|
7
|
|
|
|
|
97
|
|
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
51
|
|
43
|
18
|
|
|
|
|
1681
|
}; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub new { |
48
|
12
|
|
|
12
|
1
|
4329
|
my %struct = map { $_ => {_struct()} } qw(stderr stdout); |
|
24
|
|
|
|
|
67
|
|
49
|
12
|
|
|
|
|
50
|
return bless \%struct, $_[0]; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
sub hook { |
52
|
18
|
|
|
18
|
1
|
12211
|
my ($self, $handle) = @_; |
53
|
18
|
100
|
|
|
|
51
|
$_ eq 'stderr' ? $self->_stderr : $self->_stdout for _handles($handle); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
sub unhook { |
56
|
19
|
|
|
19
|
1
|
2350
|
my ($self, $handle) = @_; |
57
|
|
|
|
|
|
|
|
58
|
19
|
|
|
|
|
63
|
for (_handles($handle)) { |
59
|
9
|
|
|
9
|
|
77
|
no strict 'refs'; # To allow a string as STDOUT/STDERR bareword handles |
|
9
|
|
|
|
|
38
|
|
|
9
|
|
|
|
|
4071
|
|
60
|
26
|
|
|
|
|
101
|
close uc $_; |
61
|
26
|
50
|
|
|
|
581
|
open uc $_, ">&$self->{$_}{handle}" or croak($!); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
sub flush { |
65
|
8
|
|
|
8
|
1
|
1443
|
my ($self, $handle) = @_; |
66
|
8
|
|
|
|
|
20
|
delete $self->{$_}{data} for _handles($handle); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
sub write { |
69
|
6
|
|
|
6
|
1
|
540
|
my ($self, $fn, $handle) = @_; |
70
|
6
|
100
|
66
|
|
|
36
|
if ($fn eq 'stderr' || $fn eq 'stdout'){ |
71
|
1
|
|
|
|
|
70
|
croak("write() requires a file name sent in before the handle\n"); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
5
|
|
|
|
|
14
|
for (_handles($handle)){ |
75
|
4
|
50
|
|
|
|
312
|
open my $wfh, '>>', $fn or croak($!); |
76
|
4
|
|
|
|
|
63
|
print $wfh $self->{$_}{data}; |
77
|
4
|
|
|
|
|
141
|
close $wfh; |
78
|
4
|
|
|
|
|
20
|
$self->flush($_); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
sub _struct { |
82
|
24
|
|
|
24
|
|
161
|
return (handle => *fh, data => ''); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
sub _handles { |
85
|
50
|
|
|
50
|
|
92
|
my ($handle) = @_; |
86
|
50
|
|
|
|
|
306
|
my $sub = (caller(1))[3]; |
87
|
50
|
100
|
|
|
|
205
|
_check_param($sub, $handle) if $handle; |
88
|
46
|
100
|
|
|
|
237
|
return $handle ? ($handle) : qw(stderr stdout); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
sub _check_param { |
91
|
|
|
|
|
|
|
# validates the $handle param |
92
|
34
|
|
|
34
|
|
74
|
my ($sub, $handle) = @_; |
93
|
34
|
100
|
|
|
|
60
|
if (! grep {$handle eq $_} qw(stderr stdout)){ |
|
68
|
|
|
|
|
207
|
|
94
|
4
|
|
|
|
|
415
|
croak( |
95
|
|
|
|
|
|
|
"$sub() either takes 'stderr', 'stdout' or no params\n" . |
96
|
|
|
|
|
|
|
"You supplied '$handle'\n" |
97
|
|
|
|
|
|
|
); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
1; |
102
|
|
|
|
|
|
|
__END__ |