line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- mode: perl; coding: utf-8 -*- |
2
|
|
|
|
|
|
|
package YATT::Util::Finalizer; |
3
|
12
|
|
|
12
|
|
47273
|
use strict; |
|
12
|
|
|
|
|
18
|
|
|
12
|
|
|
|
|
327
|
|
4
|
12
|
|
|
12
|
|
38
|
use warnings FATAL => qw(all); |
|
12
|
|
|
|
|
10
|
|
|
12
|
|
|
|
|
337
|
|
5
|
|
|
|
|
|
|
|
6
|
12
|
|
|
12
|
|
40
|
use base qw(Exporter); |
|
12
|
|
|
|
|
14
|
|
|
12
|
|
|
|
|
971
|
|
7
|
|
|
|
|
|
|
BEGIN { |
8
|
12
|
|
|
12
|
|
26
|
our @EXPORT = qw(with_select capture finally); |
9
|
12
|
|
|
|
|
3014
|
our @EXPORT_OK = @EXPORT; |
10
|
|
|
|
|
|
|
} |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
162
|
|
|
162
|
0
|
371
|
my ($pack, $code) = splice @_, 0, 2; |
14
|
162
|
|
|
|
|
484
|
bless [$code, @_], $pack; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub finally (&@) { |
18
|
162
|
|
|
162
|
0
|
945
|
__PACKAGE__->new(@_); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub DESTROY { |
22
|
162
|
|
|
162
|
|
278
|
my $self = shift; |
23
|
162
|
100
|
|
|
|
442
|
if ($@) { |
24
|
|
|
|
|
|
|
# XXX: This can be hard to catch. |
25
|
32
|
50
|
|
|
|
120
|
syswrite STDOUT, "\n\n[$@]" if $ENV{DEBUG_ERROR}; |
26
|
|
|
|
|
|
|
} |
27
|
162
|
50
|
|
|
|
454
|
my $code = $self->[0] or return; |
28
|
162
|
|
|
|
|
586
|
$code->(@$self[1 .. $#$self]); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub cancel { |
32
|
0
|
|
|
0
|
0
|
0
|
undef shift->[0]; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub with_select { |
36
|
|
|
|
|
|
|
# newfh, body, [strref] |
37
|
162
|
|
|
162
|
0
|
206
|
my $strref; |
38
|
162
|
50
|
|
|
|
503
|
unless (defined $_[0]) { |
39
|
162
|
|
33
|
|
|
423
|
$strref = $_[2] || do {my $str = ""; \$str}; |
40
|
162
|
50
|
|
3
|
|
2652
|
open $_[0], '>', $strref or die "Can't open strref: $!"; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
17
|
|
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
my $finalizer = finally { |
43
|
162
|
|
|
162
|
|
1421
|
select($_[0]); |
44
|
162
|
|
|
|
|
3506
|
} select; |
45
|
162
|
|
|
|
|
310
|
select($_[0]); |
46
|
162
|
|
|
|
|
518
|
$_[1]->($finalizer); |
47
|
130
|
50
|
|
|
|
772
|
defined $strref && $$strref; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub capture (&@) { |
51
|
162
|
|
|
162
|
0
|
537
|
with_select my ($fh), $_[0]; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
1; |