File Coverage

blib/lib/File/Util/Copy.pm
Criterion Covered Total %
statement 17 41 41.4
branch 0 16 0.0
condition 0 4 0.0
subroutine 6 8 75.0
pod 2 2 100.0
total 25 71 35.2


line stmt bran cond sub pod time code
1             package File::Util::Copy;
2              
3 1     1   382170 use 5.010001;
  1         4  
4 1     1   4 use strict;
  1         1  
  1         20  
5 1     1   3 use warnings;
  1         6  
  1         49  
6 1     1   1405 use Log::ger;
  1         40  
  1         6  
7              
8 1     1   173 use Exporter 'import';
  1         1  
  1         25  
9 1     1   452 use File::Copy ();
  1         4071  
  1         374  
10              
11             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12             our $DATE = '2024-11-22'; # DATE
13             our $DIST = 'File-Util-Copy'; # DIST
14             our $VERSION = '0.002'; # VERSION
15              
16             our @EXPORT_OK = qw(
17             copy_noclobber
18             copy_warnclobber
19             );
20              
21             sub copy_noclobber {
22 0 0   0 1   my $opts = ref $_[0] eq 'HASH' ? shift : {};
23 0   0       $opts->{pattern} //= " (%02d)";
24              
25 0           my ($from, $to) = @_;
26 0 0         my $ext; $to =~ s/(\.\w+)\z// and $ext = $1;
  0            
27              
28             # XXX handle when to is a filehandle ref/blob, which File::Copy supports
29              
30 0           my $i = 0;
31 0           my $to_final;
32 0           while (1) {
33 0 0         if ($i) {
34 0           my $suffix = sprintf $opts->{pattern}, $i;
35 0 0         $to_final = $ext ? "$to$suffix$ext" : "$to$suffix";
36             } else {
37 0           $to_final = $to;
38             }
39 0           lstat $to_final;
40 0 0         last unless -e _;
41 0           $i++;
42             }
43 0           File::Copy::copy($from, $to_final);
44             }
45              
46             sub copy_warnclobber {
47 0 0   0 1   my $opts = ref $_[0] eq 'HASH' ? shift : {};
48 0   0       $opts->{log} //= 0;
49              
50 0           my ($from, $to) = @_;
51              
52             # XXX handle when to is a filehandle ref/blob, which File::Copy supports
53 0 0         if (-e $to) {
54 0 0         if ($opts->{log}) {
55 0           log_warn "copy_warnclobber(`$from`, `$to`): Target already exists, renaming anyway ...";
56             } else {
57 0           warn "copy_warnclobber(`$from`, `$to`): Target already exists, renaming anyway ...\n";
58             }
59             }
60              
61 0           File::Copy::copy($from, $to);
62             }
63              
64             1;
65             # ABSTRACT: Utilities related to copying files
66              
67             __END__