File Coverage

blib/lib/File/Slurper/Temp.pm
Criterion Covered Total %
statement 35 37 94.5
branch 2 4 50.0
condition n/a
subroutine 9 11 81.8
pod 4 4 100.0
total 50 56 89.2


line stmt bran cond sub pod time code
1             package File::Slurper::Temp;
2              
3             our $DATE = '2019-07-21'; # DATE
4             our $VERSION = '0.006'; # VERSION
5              
6 1     1   69281 use strict;
  1         9  
  1         25  
7 1     1   4 use warnings;
  1         1  
  1         41  
8              
9 1     1   5 use Carp 'croak';
  1         2  
  1         39  
10 1     1   5 use File::Slurper ();
  1         2  
  1         18  
11 1     1   554 use File::Temp ();
  1         17885  
  1         30  
12              
13 1     1   7 use Exporter qw(import);
  1         2  
  1         241  
14             our @EXPORT_OK = qw(
15             write_text write_binary
16             write_text_to_tempfile write_binary_to_tempfile
17             );
18              
19             our $FILE_TEMP_TEMPLATE = "XXXXXXXXXX";
20             our $FILE_TEMP_DIR;
21              
22             sub _tempfile {
23 1     1   2 my $target_filename = shift;
24              
25 1         2 my @tfargs;
26 1         2 push @tfargs, $FILE_TEMP_TEMPLATE;
27 1         2 my $dir = $FILE_TEMP_DIR;
28 1 50       4 unless (defined $dir) {
29 1         7 require File::Spec;
30 1         15 (undef, $dir, undef) = File::Spec->splitpath($target_filename);
31             }
32 1         3 push @tfargs, DIR => $dir;
33 1         5 File::Temp::tempfile(@tfargs);
34             }
35              
36             sub write_text {
37 1     1 1 2370 my $filename = shift;
38              
39 1         4 my ($tempfh, $tempname) = _tempfile($filename);
40 1         267 File::Slurper::write_text($tempname, @_);
41 1 50       260 rename $tempname, $filename
42             or croak "Couldn't rename $tempname to $filename: $!";
43              
44 1         21 return;
45             }
46              
47             sub write_binary {
48 0     0 1 0 return write_text(@_[0,1], 'latin-1');
49             }
50              
51             sub write_text_to_tempfile {
52 1     1 1 4365 my ($tempfh, $tempname) = File::Temp::tempfile();
53 1         313 File::Slurper::write_text($tempname, @_);
54 1         166 return $tempname;
55             }
56              
57             sub write_binary_to_tempfile {
58 0     0 1   return write_text_to_tempfile($_[0], 'latin-1');
59             }
60              
61             1;
62             # ABSTRACT: File::Slurper + File::Temp
63              
64             __END__