File Coverage

blib/lib/File/Temp/Rename.pm
Criterion Covered Total %
statement 37 37 100.0
branch 7 12 58.3
condition 3 3 100.0
subroutine 9 9 100.0
pod 1 1 100.0
total 57 62 91.9


line stmt bran cond sub pod time code
1             package File::Temp::Rename;
2             {
3             $File::Temp::Rename::VERSION = '0.02';
4             }
5             # ABSTRACT: Create a temporary file object for output, and rename it when done.
6              
7 1     1   64822 use strict;
  1         2  
  1         34  
8 1     1   5 use warnings;
  1         1  
  1         23  
9 1     1   6 use Carp;
  1         6  
  1         74  
10 1     1   1290 use File::Temp;
  1         27864  
  1         97  
11 1     1   1097 use File::Copy qw/move/;
  1         2605  
  1         68  
12 1     1   7 use Scalar::Util qw/refaddr/;
  1         2  
  1         47  
13 1     1   6 use base qw/File::Temp/;
  1         2  
  1         340  
14              
15             our %obj2original;
16              
17             sub new {
18 3     3 1 1030 my $class = shift;
19 3         11 my %opt = @_;
20 3 50       14 my $filename = delete $opt{FILE} or croak "need FILE argument";
21 3         5 my $clobber = delete $opt{CLOBBER};
22              
23 3 100 100     48 if (-e $filename and ! $clobber){
24 1         5 return;
25             }
26              
27 2 50       20 my $self = $class->SUPER::new(TEMPLATE => "$filename.tmpXXXXX", UNLINK => 0) or croak "couldn't create tmp file";
28 2         1221 my $tmp_filename = $self->filename;
29              
30 2         17 my $addr = refaddr($self);
31 2         5 $obj2original{$addr} = $filename;
32 2         9 return $self;
33             }
34              
35             sub DESTROY{
36 2     2   775 my $self = shift;
37 2 50       20 my $filename = delete $obj2original{refaddr($self)} or die "$self does not have appropriate entry in obj2original? bug, please report";
38 2         8 my $tmp_filename = $self->filename;
39            
40 2 50       38 $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
41              
42 2 50       64 move($tmp_filename, $filename) or croak "couldn't rename $tmp_filename to $filename";
43             }
44              
45             1;
46              
47             __END__