line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::MultiTemp; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: manage a hash of temporary files |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
137994
|
use v5.14; |
|
1
|
|
|
|
|
15
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
615
|
use Moo; |
|
1
|
|
|
|
|
11605
|
|
|
1
|
|
|
|
|
6
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
1514
|
use Fcntl qw/ LOCK_EX /; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
48
|
|
10
|
1
|
|
|
1
|
|
7
|
use List::Util 1.29 qw/ pairs /; |
|
1
|
|
|
|
|
16
|
|
|
1
|
|
|
|
|
93
|
|
11
|
1
|
|
|
1
|
|
8
|
use Path::Tiny qw/ path /; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
12
|
1
|
|
|
1
|
|
472
|
use PerlX::Maybe qw/ maybe /; |
|
1
|
|
|
|
|
2500
|
|
|
1
|
|
|
|
|
4
|
|
13
|
1
|
|
|
1
|
|
64
|
use Scalar::Util qw/ openhandle /; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
54
|
|
14
|
1
|
|
|
1
|
|
454
|
use Types::Common::String qw/ SimpleStr /; |
|
1
|
|
|
|
|
119222
|
|
|
1
|
|
|
|
|
13
|
|
15
|
1
|
|
|
1
|
|
630
|
use Types::Standard qw/ Bool CodeRef FileHandle HashRef StrMatch /; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
16
|
1
|
|
|
1
|
|
1725
|
use Types::Path::Tiny qw/ Dir File /; |
|
1
|
|
|
|
|
23048
|
|
|
1
|
|
|
|
|
9
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# RECOMMEND PREREQ: Type::Tiny::XS |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
1
|
|
995
|
use namespace::autoclean; |
|
1
|
|
|
|
|
14341
|
|
|
1
|
|
|
|
|
5
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION = 'v0.1.5'; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has template => ( |
26
|
|
|
|
|
|
|
is => 'ro', |
27
|
|
|
|
|
|
|
isa => StrMatch[ qr/XXXX/ ], |
28
|
|
|
|
|
|
|
predicate => 1, |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
has suffix => ( |
33
|
|
|
|
|
|
|
is => 'ro', |
34
|
|
|
|
|
|
|
isa => SimpleStr, |
35
|
|
|
|
|
|
|
predicate => 1, |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
has dir => ( |
40
|
|
|
|
|
|
|
is => 'ro', |
41
|
|
|
|
|
|
|
isa => Dir, |
42
|
|
|
|
|
|
|
coerce => \&path, |
43
|
|
|
|
|
|
|
predicate => 1, |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has unlink => ( |
48
|
|
|
|
|
|
|
is => 'ro', |
49
|
|
|
|
|
|
|
isa => Bool, |
50
|
|
|
|
|
|
|
default => 1, |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has init => ( |
55
|
|
|
|
|
|
|
is => 'ro', |
56
|
|
|
|
|
|
|
isa => CodeRef, |
57
|
|
|
|
|
|
|
predicate => 1, |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
has _files => ( |
61
|
|
|
|
|
|
|
is => 'ro', |
62
|
|
|
|
|
|
|
isa => HashRef [ File ], |
63
|
1
|
|
|
1
|
|
47
|
builder => sub { return {} }, |
64
|
|
|
|
|
|
|
init_arg => undef, |
65
|
|
|
|
|
|
|
); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
has _file_handles => ( |
68
|
|
|
|
|
|
|
is => 'ro', |
69
|
|
|
|
|
|
|
isa => HashRef [ FileHandle ], |
70
|
1
|
|
|
1
|
|
6928
|
builder => sub { return {} }, |
71
|
|
|
|
|
|
|
init_arg => undef, |
72
|
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _get_tempfile_args { |
75
|
3
|
|
|
3
|
|
7
|
my ($self, $key ) = @_; |
76
|
|
|
|
|
|
|
|
77
|
3
|
|
|
|
|
4
|
my $template; |
78
|
|
|
|
|
|
|
|
79
|
3
|
50
|
|
|
|
10
|
if ( $self->has_template ) { |
80
|
3
|
|
|
|
|
16
|
$template = $self->template =~ s/KEY/${key}/r; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
return ( |
84
|
3
|
50
|
|
|
|
51
|
maybe TEMPLATE => $template, |
|
|
50
|
|
|
|
|
|
85
|
|
|
|
|
|
|
maybe SUFFIX => $self->has_suffix ? $self->suffix : undef, |
86
|
|
|
|
|
|
|
maybe DIR => $self->has_dir ? $self->dir->stringify : undef, |
87
|
|
|
|
|
|
|
UNLINK => $self->unlink, |
88
|
|
|
|
|
|
|
); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _get_open_file_handle { |
93
|
8
|
|
|
8
|
|
21
|
my ($self, $key, $file, $init) = @_; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
8
|
|
|
|
|
21
|
my $fhs = $self->_file_handles; |
97
|
8
|
100
|
|
|
|
39
|
if ( my $fh = openhandle( $fhs->{$key} ) ) { |
98
|
3
|
|
|
|
|
68
|
return $fh; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# get file only if we do not have it (otherwise recursion) |
102
|
5
|
|
66
|
|
|
23
|
$file //= $self->file( $key, $init ); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# filehandle is no longer be open, so overwrite it |
105
|
5
|
|
|
|
|
22
|
my $fh = ( $fhs->{$key} = $file->opena_raw( { locked => 0 } ) ); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# Path::Tiny locking does not seem to release locks properly, so we need to control locks manually |
108
|
5
|
|
|
|
|
710
|
flock( $fh, LOCK_EX ); |
109
|
5
|
|
|
|
|
35
|
return $fh; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub file { |
114
|
5
|
|
|
5
|
1
|
12
|
my ($self, $key, $init) = @_; |
115
|
|
|
|
|
|
|
|
116
|
5
|
|
|
|
|
13
|
my $files = $self->_files; |
117
|
|
|
|
|
|
|
|
118
|
5
|
100
|
|
|
|
17
|
if ( my $file = $files->{$key} ) { |
119
|
2
|
|
|
|
|
12
|
return $file; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
3
|
|
33
|
|
|
15
|
my $file = $files->{$key} //= Path::Tiny->tempfile( $self->_get_tempfile_args($key) ); |
123
|
3
|
|
|
|
|
20140
|
my $fh = $self->_get_open_file_handle( $key, $file, $init ); |
124
|
3
|
50
|
33
|
|
|
26
|
if ( $init //= $self->init ) { |
125
|
3
|
|
|
|
|
11
|
$init->( $key, $file, $fh ); |
126
|
|
|
|
|
|
|
} |
127
|
3
|
|
|
|
|
3785
|
return $file; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub file_handle { |
132
|
5
|
|
|
5
|
1
|
829
|
my ($self, $key, $init) = @_; |
133
|
5
|
|
|
|
|
14
|
return $self->_get_open_file_handle( $key, undef, $init ); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub keys { |
138
|
1
|
|
|
1
|
1
|
4
|
my ($self) = @_; |
139
|
1
|
|
|
|
|
9
|
my $files = $self->_files; |
140
|
1
|
|
|
|
|
1
|
return [ keys %{ $files } ]; |
|
1
|
|
|
|
|
6
|
|
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub files { |
145
|
1
|
|
|
1
|
1
|
5610
|
my ($self) = @_; |
146
|
1
|
|
|
|
|
4
|
my $files = $self->_files; |
147
|
1
|
|
|
|
|
3
|
return [ values %{ $files } ]; |
|
1
|
|
|
|
|
6
|
|
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub close { |
152
|
2
|
|
|
2
|
1
|
6
|
my ($self) = @_; |
153
|
2
|
|
|
|
|
6
|
my $fhs = $self->_file_handles; |
154
|
2
|
|
|
|
|
4
|
for my $kv ( pairs %{ $fhs } ) { |
|
2
|
|
|
|
|
31
|
|
155
|
3
|
|
|
|
|
5
|
my ( $key, $fh ) = @{ $kv }; |
|
3
|
|
|
|
|
15
|
|
156
|
3
|
50
|
|
|
|
76
|
close($fh) if $fh; |
157
|
3
|
|
|
|
|
14
|
delete $fhs->{$key}; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub DEMOLISH { |
162
|
1
|
|
|
1
|
0
|
3439
|
my ($self, $is_global) = @_; |
163
|
1
|
50
|
|
|
|
5
|
$self->close unless $is_global; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
1; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
__END__ |