line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test2::Plugin::IOEvents::Tie; |
2
|
3
|
|
|
3
|
|
21
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
86
|
|
3
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
125
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.001001'; |
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
15
|
use Test2::API qw/context/; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
153
|
|
8
|
3
|
|
|
3
|
|
18
|
use Carp qw/croak/; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
672
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub TIEHANDLE { |
11
|
6
|
|
|
6
|
|
14
|
my $class = shift; |
12
|
6
|
|
|
|
|
19
|
my ($name, $fn, $fh, $inode) = @_; |
13
|
|
|
|
|
|
|
|
14
|
6
|
50
|
33
|
|
|
26
|
unless ($fn && $fh) { |
15
|
6
|
50
|
|
|
|
28
|
if ($fn) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
16
|
0
|
|
|
|
|
0
|
open($fh, '>&', $fn); |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
elsif ($name eq 'STDOUT') { |
19
|
3
|
|
|
|
|
9
|
$fn = fileno(STDOUT); |
20
|
3
|
|
|
|
|
30
|
(undef, $inode) = stat(STDOUT); |
21
|
3
|
|
|
|
|
78
|
open($fh, '>&', STDOUT); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
elsif ($name eq 'STDERR') { |
24
|
3
|
|
|
|
|
8
|
$fn = fileno(STDERR); |
25
|
3
|
|
|
|
|
34
|
(undef, $inode) = stat(STDERR); |
26
|
3
|
|
|
|
|
69
|
open($fh, '>&', STDERR); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
6
|
|
|
|
|
56
|
return bless([$name, $fn, $fh, $inode], $class); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub OPEN { |
35
|
3
|
|
|
3
|
|
23
|
no warnings 'uninitialized'; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
2984
|
|
36
|
|
|
|
|
|
|
|
37
|
1
|
50
|
|
1
|
|
3755
|
if ($_[0]->[0] eq 'STDOUT') { |
|
|
0
|
|
|
|
|
|
38
|
1
|
|
|
|
|
21
|
untie(*STDOUT); |
39
|
1
|
50
|
|
|
|
34
|
return open(STDOUT, $_[1], @_ > 2 ? $_[2] : ()); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
elsif ($_[0]->[0] eq 'STDERR') { |
42
|
0
|
|
|
|
|
0
|
untie(*STDERR); |
43
|
0
|
0
|
|
|
|
0
|
return open(STDERR, $_[1], @_ > 2 ? $_[2] : ()); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
0
|
return; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub _check_for_change { |
50
|
10
|
100
|
|
10
|
|
63
|
if ($_[0]->[0] eq 'STDOUT') { |
|
|
50
|
|
|
|
|
|
51
|
6
|
|
|
|
|
91
|
my (undef, $inode) = stat(STDOUT); |
52
|
6
|
100
|
|
|
|
66
|
if ($inode ne $_[0]->[3]) { |
53
|
1
|
|
|
|
|
134
|
untie(*STDOUT); |
54
|
1
|
|
|
|
|
8
|
return 1; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
elsif ($_[0]->[0] eq 'STDERR') { |
58
|
4
|
|
|
|
|
37
|
my (undef, $inode) = stat(STDERR); |
59
|
4
|
50
|
|
|
|
21
|
if ($inode ne $_[0]->[3]) { |
60
|
0
|
|
|
|
|
0
|
untie(*STDERR); |
61
|
0
|
|
|
|
|
0
|
return 1; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
9
|
|
|
|
|
24
|
return 0; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub PRINT { |
69
|
9
|
|
|
9
|
|
5430
|
my (undef, @args) = @_; |
70
|
|
|
|
|
|
|
|
71
|
9
|
|
|
|
|
108
|
my $name = $_[0]->[0]; |
72
|
9
|
100
|
|
|
|
135
|
if ($_[0]->_check_for_change()) { |
73
|
1
|
50
|
|
|
|
15
|
if ($name eq 'STDOUT') { |
|
|
0
|
|
|
|
|
|
74
|
1
|
|
|
|
|
78
|
return print STDOUT @args; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
elsif ($name eq 'STDERR') { |
77
|
0
|
|
|
|
|
0
|
return print STDERR @args; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
8
|
50
|
|
|
|
31
|
my $output = defined($,) ? join( $,, @args) : join('', @args); |
82
|
|
|
|
|
|
|
|
83
|
8
|
50
|
|
|
|
23
|
return unless length($output); |
84
|
|
|
|
|
|
|
|
85
|
8
|
|
|
|
|
27
|
my $ctx = context(); |
86
|
8
|
100
|
|
|
|
766
|
$ctx->send_ev2_and_release( |
87
|
|
|
|
|
|
|
info => [ |
88
|
|
|
|
|
|
|
{tag => $_[0]->[0], details => $output, $_[0]->[0] eq 'STDERR' ? (debug => 1) : ()}, |
89
|
|
|
|
|
|
|
] |
90
|
|
|
|
|
|
|
); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub FILENO { |
94
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
95
|
0
|
|
|
|
|
0
|
return $self->[1]; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub PRINTF { |
99
|
0
|
|
|
0
|
|
0
|
my (undef, @list) = @_; |
100
|
0
|
|
|
|
|
0
|
my $name = $_[0]->[0]; |
101
|
0
|
0
|
|
|
|
0
|
if ($_[0]->_check_for_change()) { |
102
|
0
|
0
|
|
|
|
0
|
if ($name eq 'STDOUT') { |
|
|
0
|
|
|
|
|
|
103
|
0
|
|
|
|
|
0
|
return printf STDOUT @list; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
elsif ($name eq 'STDERR') { |
106
|
0
|
|
|
|
|
0
|
return printf STDERR @list; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
0
|
my $self = shift; |
111
|
0
|
|
|
|
|
0
|
my $format = shift @list; |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
0
|
my $output = sprintf($format, @list); |
114
|
0
|
0
|
|
|
|
0
|
return unless length($output); |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
0
|
my $ctx = context(); |
117
|
0
|
0
|
|
|
|
0
|
$ctx->send_ev2_and_release( |
118
|
|
|
|
|
|
|
info => [ |
119
|
|
|
|
|
|
|
{tag => $name, details => $output, $name eq 'STDERR' ? (debug => 1) : ()}, |
120
|
|
|
|
|
|
|
] |
121
|
|
|
|
|
|
|
); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub CLOSE { |
125
|
0
|
0
|
|
0
|
|
0
|
if ($_[0]->[0] eq 'STDOUT') { |
|
|
0
|
|
|
|
|
|
126
|
0
|
|
|
|
|
0
|
untie(*STDOUT); |
127
|
0
|
|
|
|
|
0
|
return close(STDOUT); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
elsif ($_[0]->[0] eq 'STDERR') { |
130
|
0
|
|
|
|
|
0
|
untie(*STDERR); |
131
|
0
|
|
|
|
|
0
|
return close(STDERR); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub WRITE { |
136
|
1
|
|
|
1
|
|
10413
|
my (undef, $buf, $len, $offset) = @_; |
137
|
1
|
|
|
|
|
2
|
my $fh; |
138
|
1
|
|
|
|
|
3
|
my $name = $_[0]->[0]; |
139
|
1
|
50
|
|
|
|
3
|
if ($_[0]->_check_for_change()) { |
140
|
0
|
0
|
|
|
|
0
|
if ($name eq 'STDOUT') { |
|
|
0
|
|
|
|
|
|
141
|
0
|
|
|
|
|
0
|
$fh = \*STDOUT; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
elsif ($name eq 'STDERR') { |
144
|
0
|
|
|
|
|
0
|
$fh = \*STDERR; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
else { |
148
|
1
|
|
|
|
|
3
|
$fh = $_[0]->[2]; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
1
|
50
|
|
|
|
3
|
return syswrite($fh, $buf) if @_ == 2; |
152
|
1
|
50
|
|
|
|
15
|
return syswrite($fh, $buf, $len) if @_ == 3; |
153
|
0
|
|
|
|
|
|
return syswrite($fh, $buf, $len, $offset); |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub BINMODE { |
157
|
0
|
|
|
0
|
|
|
my $fh; |
158
|
0
|
|
|
|
|
|
my $name = $_[0]->[0]; |
159
|
0
|
0
|
|
|
|
|
if ($_[0]->_check_for_change()) { |
160
|
0
|
0
|
|
|
|
|
if ($name eq 'STDOUT') { |
|
|
0
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
$fh = \*STDOUT; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
elsif ($name eq 'STDERR') { |
164
|
0
|
|
|
|
|
|
$fh = \*STDERR; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
else { |
168
|
0
|
|
|
|
|
|
$fh = $_[0]->[2]; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
0
|
0
|
|
|
|
|
return binmode($fh) unless @_ > 1; |
172
|
0
|
|
|
|
|
|
return binmode($fh, $_[1]); |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub autoflush { |
176
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
177
|
|
|
|
|
|
|
|
178
|
0
|
0
|
|
|
|
|
if (@_) { |
179
|
0
|
|
|
|
|
|
my ($val) = @_; |
180
|
0
|
|
|
|
|
|
$self->[2]->autoflush($val); |
181
|
0
|
|
|
|
|
|
$self->[3] = $val; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
0
|
|
|
|
|
|
return $self->[3]; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
1; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
__END__ |