Fu/File/Temp.pm

filter: Statementstmt 83.5% 71 / 85 Branchbran 54.5% 12 / 22 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 92.3% 12 / 13 Podpod 100% 5 / 5 totaltotal 73.5% 103 / 140 CC 15 SCAR 16.7
CC
15
Cov
74%
_validate
11.8
DESTROY
3.2
new
3.2
CRAP 5.3
« Fu/Base.pm ↑ Summary Fu.pm »
1 package File::Fu::File::Temp;
2 13 $VERSION = v0.0.8;
3  
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 File::Fu::File::Temp - temporary files
11  
12 =head1 SYNOPSIS
13  
14 use File::Fu;
15 my $handle = File::Fu->temp_file;
16  
17 =cut
18  
19 13 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 # XXX should be File::Fu::Handle;
21 #use base 'File::Temp';
22  
23 =head2 new
24  
25 The directory argument is required, followed by an optional template
26 argument and/or flags. The template may contain some number of 'X'
27 characters. If it does not, ten of them will be appended.
28  
29 my $handle = File::Fu::File::Temp->new($dir, 'foo');
30 my $file = $handle->name;
31  
32 By default, the file will be deleted when the handle goes out of scope.
33 Optionally, it may be deleted immediately after creation or just not
34 deleted.
35  
36 my $handle = File::Fu::File::Temp->new($dir, 'foo', -secure);
37  
38 my $handle = File::Fu::File::Temp->new($dir, -noclean);
39 # also $handle->noclean;
40  
41 =over
42  
43 =item -secure
44  
45 Delete the named file (if the OS supports it) immediately after opening.
46  
47 Calling name() on this sort of handle throws an error.
48  
49 =item -nocleanup
50  
51 Don't attempt to remove the file when the $handle goes out of scope.
52  
53 =back
54  
55 =cut
56  
57 {
58 13 my %argmap = (
59 secure => [],
60 nocleanup => [UNLINK => 0],
61 );
62 4 ▶sub new {
Subroutine new: called CC=3 SCAR=11.5
Pod documented
63 4 my $proto = shift;
64 4 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
65 4 my ($dir, $send, $opt) = $class->_validate(\%argmap, @_);
66  
67 4 my ($self, $fn);
68 4 ▶ if($opt->{secure}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'secure'}) { } 0 4 4
69 0 $self = File::Temp::tempfile(@$send);
70 0 $class .= '::HasNoFileName';
71 }
72 else {
73 4 ($self, $fn) = File::Temp::tempfile(@$send);
74 4 ${*$self} = $dir->file_class->new($fn);
75 }
76 4 %{*$self} = %$opt;
77 4 bless($self, $class);
78 4 return($self);
79 }} # end subroutine new definition
80 ########################################################################
81  
82 =for internal head2 _validate
83 my ($dir, $send, $opt) = $class->_validate(\%map, @_);
84  
85 =cut
86  
87 13 ▶sub _validate {
Subroutine _validate: called CC=9 SCAR=24.7
88 7 my $class = shift;
89 7 my %argmap = %{shift(@_)};
90 7 my ($dir, @opt) = @_;
91 croak("invalid directory '$dir' ")
92 7 ▶ unless(eval {$dir->can('e')} and $dir->e);
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $dir->can("e") } } and $dir->e 0 7 7
Condition33%1/3
eval { do { $dir->can("e") } } and $dir->e
!ll&&!rl&&r
007
MC/DC0%0/2
eval { do { $dir->can("e") } } and $dir->e
eval { do { $dir->can("e") } } $dir->e
Truth table33%1/3
A and B
A= eval { do { $dir->can("e") } }B= $dir->e
ABresult
0X0
100
111
93  
94 7 my @send;
95 my %opt;
96 7 ▶ for(my $i = 0; $i < @opt; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @opt) 3 7 10
97 3 ▶ ($opt[$i] =~ s/^-//) or next;
Branch: 1 branch
BranchTrueFalseTotal
$opt[$i] =~ s/^-// or next 0 3 3
98 0 my ($key) = splice(@opt, $i, 1); $i--;
99 0 ▶ my $do = $argmap{$key} or croak("invalid argument '$key'");
Branch: 1 branch
BranchTrueFalseTotal
my $do = $argmap{$key} or croak("invalid argument '${key}'") 0 0 0
100 0 push(@send, @$do);
101 0 $opt{$key} = 1;
102 }
103 7 ▶ if(@opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opt) 3 4 7
104 3 my $template = shift(@opt);
105 3 ▶ croak("invalid arguments '@opt'") if(@opt);
Branch: 1 branch
BranchTrueFalseTotal
if @opt 0 3 3
106  
107 3 ▶ $template .= $class->XXX unless($template =~ m/X/);
Branch: 1 branch
BranchTrueFalseTotal
unless $template =~ /X/ 3 0 3
108 # XXX File::Temp specific
109 3 unshift(@send, $template);
110 }
111 7 $opt{auto_delete} = ! delete($opt{nocleanup});
112  
113 7 push(@send, DIR => "$dir");
114  
115 7 return($dir, \@send, \%opt);
116 } # end subroutine _validate definition
117 ########################################################################
118  
119 =head2 name
120  
121 my $file_obj = $handle->name;
122  
123 =cut
124  
125 8 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
126 8 my $self = shift;
127 8 return(${*$self});
128 } # end subroutine name definition
129 ########################################################################
130  
131 =head2 nocleanup
132  
133 Disable autocleanup.
134  
135 $handle->nocleanup;
136  
137 =cut
138  
139 0 ▶sub nocleanup {
Subroutine nocleanup: not called CC=1 SCAR=6.9
Pod documented
140 0 my $self = shift;
141 0 my %opt = %{*$self};
142 0 $opt{auto_delete} = 0;
143 } # end subroutine nocleanup definition
144 ########################################################################
145  
146 =head2 write
147  
148 Write @content to the tempfile and close it.
149  
150 $handle = $handle->write(@content);
151  
152 =cut
153  
154 3 ▶sub write {
Subroutine write: called CC=2 SCAR=7.4
Pod documented
155 3 my $self = shift;
156 3 my (@content) = @_;
157 3 do {
158 1 ▶ local $SIG{__WARN__} = sub { # ugh
Subroutine __ANON__: called CC=2 SCAR=8.5
159 1 my $x = shift;
160 1 local $Carp::Level = 1;
161 1 ▶ if($x =~ m/^print\(\) on closed filehandle/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x =~ /^print\(\) on closed filehandle/) 1 0 1
162 1 croak("write() on closed tempfile");
163 }
164 0 my $file = __FILE__;
165 0 $x =~ s/ at \Q$file\E .*\n//;
166 0 warn Carp::shortmess($x);
167 3 };
168 3 print $self @content;
169 };
170 2 ▶ close($self) or croak("write '" . $_->name . "' failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
close $self or croak(q[write '] . $_->name . "' failed: $!") 2 0 2
171 2 return $self;
172 } # write ##############################################################
173  
174 =head2 do
175  
176 Execute subref with $handle as $_. If you chain this with the
177 constructor, the destructor cleanup will happen immediately after sub
178 has returned.
179  
180 my @x = $handle->do(sub {something($_->name); ...});
181  
182 =cut
183  
184 1 ▶sub do {
Subroutine do: called CC=1 SCAR=0.0
Pod documented
185 1 my $self = shift;
186 1 my ($sub) = @_;
187 1 local $_ = $self;
188 1 return $sub->();
189 } # do #################################################################
190  
191 =head2 DESTROY
192  
193 Called automatically when the handle goes out of scope.
194  
195 $handle->DESTROY;
196  
197 =cut
198  
199 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.8
200 4 my $self = shift;
201 4 my %opt = %{*$self};
202 4 ▶ return if($opt{secure} or ! $opt{auto_delete});
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"secure"} or not $opt{"auto_delete"} 0 4 4
Condition33%1/3
$opt{"secure"} or not $opt{"auto_delete"}
l!l&&r!l&&!r
004
MC/DC0%0/2
$opt{"secure"} or not $opt{"auto_delete"}
$opt{"secure"} not $opt{"auto_delete"}
Truth table33%1/3
A or not(B)
A= $opt{"secure"}B= not $opt{"auto_delete"}
ABresult
1X1
011
000
203 4 $self->name->unlink;
204 } # end subroutine DESTROY definition
205 ########################################################################
206  
207 =head2 XXX
208  
209 Constant representing a chunk of X characters.
210  
211 =cut
212  
213 13 ▶use constant XXX => 'X'x10;
Subroutine BEGIN: called CC=1 SCAR=0.0
214  
215 =head1 AUTHOR
216  
217 Eric Wilhelm @ <ewilhelm at cpan dot org>
218  
219 http://scratchcomputing.com/
220  
221 =head1 BUGS
222  
223 If you found this module on CPAN, please report any bugs or feature
224 requests through the web interface at L<http://rt.cpan.org>. I will be
225 notified, and then you'll automatically be notified of progress on your
226 bug as I make changes.
227  
228 If you pulled this development version from my /svn/, please contact me
229 directly.
230  
231 =head1 COPYRIGHT
232  
233 Copyright (C) 2008 Eric L. Wilhelm, All Rights Reserved.
234  
235 =head1 NO WARRANTY
236  
237 Absolutely, positively NO WARRANTY, neither express or implied, is
238 offered with this software. You use this software at your own risk. In
239 case of loss, no person or entity owes you anything whatsoever. You
240 have been warned.
241  
242 =head1 LICENSE
243  
244 This program is free software; you can redistribute it and/or modify it
245 under the same terms as Perl itself.
246  
247 =cut
248  
249 # vi:ts=2:sw=2:et:sta
250 13 1;
« Fu/Base.pm ↑ Summary Fu.pm »