File Coverage

blib/lib/File/Touch.pm
Criterion Covered Total %
statement 67 90 74.4
branch 14 46 30.4
condition 8 21 38.1
subroutine 10 10 100.0
pod 0 2 0.0
total 99 169 58.5


line stmt bran cond sub pod time code
1             package File::Touch;
2             $File::Touch::VERSION = '0.11_03'; # TRIAL
3              
4 2     2   76700 $File::Touch::VERSION = '0.1103';use 5.006;
  2         15  
5 2     2   11 use warnings;
  2         3  
  2         54  
6 2     2   9 use strict;
  2         3  
  2         237  
7              
8             require Exporter;
9             our @ISA = qw(Exporter);
10             our @EXPORT = qw(touch);
11              
12 2     2   15 use Carp;
  2         13  
  2         153  
13 2     2   1003 use IO::File;
  2         17375  
  2         219  
14 2     2   1144 use File::stat;
  2         16518  
  2         10  
15 2     2   121 use Fcntl;
  2         4  
  2         569  
16              
17             BEGIN {
18 2     2   7 eval {
19 2         1150 require Time::HiRes;
20 2         2614 Time::HiRes->import(qw/ time utime /);
21             };
22 2 50       300 if (not $@) {
23 2         1404 Time::HiRes->VERSION(1.9764);
24             }
25             }
26              
27             my $SYSOPEN_MODE = O_WRONLY|O_CREAT;
28             eval {
29             $SYSOPEN_MODE |= O_NONBLOCK;
30             };
31             if($@) {
32             # OK, we don't have O_NONBLOCK:
33             # probably running on Windows.
34             }
35             eval {
36             $SYSOPEN_MODE |= O_NOCTTY;
37             };
38             if($@) {
39             # OK, we don't have O_NOCTTY:
40             # probably running on Windows.
41             }
42              
43             sub new
44             {
45 3     3 0 3843 my ($caller, %arg) = @_;
46 3         8 my $caller_is_obj = ref($caller);
47 3   33     17 my $class = $caller_is_obj || $caller;
48 3         9 my $self = bless{}, $class;
49              
50 3   100     11 my $atime_only = $arg{atime_only} || 0; # If nonzero, change only the access time.
51 3   100     10 my $mtime_only = $arg{mtime_only} || 0; # If nonzero, change only the modification time.
52 3   50     14 my $no_create = $arg{no_create} || 0; # If nonzero, don't create if not already there.
53 3         5 my $reference = $arg{reference}; # If defined, use this file's times instead of current time.
54 3         6 my $time = $arg{time}; # If defined, use this time instead of current time.
55 3         5 my $atime = $arg{atime}; # If defined, use this time for access time instead of current time.
56 3         5 my $mtime = $arg{mtime}; # If defined, use this time for modification time instead of current time.
57              
58 3 50 66     13 if ($atime_only && $mtime_only){
59 0         0 croak("Incorrect usage: 'atime_only' and 'mtime_only' are both set - they are mutually exclusive.");
60             }
61              
62 3 50       7 if (defined $time) {
63 0 0 0     0 if ((defined $atime) || (defined $mtime)) {
64 0         0 croak("Incorrect usage: 'time' should not be used with either ",
65             "'atime' or 'mtime' - ambiguous.");
66             }
67 0 0       0 $atime = $time unless $mtime_only;
68 0 0       0 $mtime = $time unless $atime_only;
69             }
70              
71 3 50       7 if (defined $reference) {
72 0 0 0     0 if ((defined $time) || (defined $atime) || (defined $mtime)) {
      0        
73 0         0 croak("Incorrect usage: 'reference' should not be used with 'time', 'atime' or 'mtime' - ambiguous.");
74             }
75 0 0       0 if (-e $reference) {
76 0 0       0 my $sb = stat($reference) or croak("Could not stat ($reference): $!");
77 0 0       0 $atime = $sb->atime unless $mtime_only;
78 0 0       0 $mtime = $sb->mtime unless $atime_only;
79             }
80             else {
81 0         0 croak("Reference file ($reference) does not exist");
82             }
83             }
84              
85 3         12 $self->{_atime} = $atime;
86 3         4 $self->{_mtime} = $mtime;
87 3         5 $self->{_no_create} = $no_create;
88 3         7 $self->{_atime_only} = $atime_only;
89 3         5 $self->{_mtime_only} = $mtime_only;
90              
91 3         10 return $self;
92             }
93              
94             sub touch
95             {
96 3     3 0 890 my ($caller, @files) = @_;
97 3         7 my $caller_is_obj = ref($caller);
98 3         14 my $self;
99              
100 3 50       11 if ($caller_is_obj){
101 3         5 $self = $caller;
102             }
103             else {
104 0         0 unshift @files, $caller;
105 0         0 $self->{_atime} = undef;
106 0         0 $self->{_mtime} = undef;
107 0         0 $self->{_no_create} = 0;
108 0         0 $self->{_atime_only} = 0;
109 0         0 $self->{_mtime_only} = 0;
110             }
111              
112 3         4 my $count = 0;
113              
114 3         7 foreach my $file (@files) {
115 3         12 my $time = time();
116 3         5 my ($atime,$mtime);
117            
118 3 50       62 if (-e $file) {
119 3 50       18 my $sb = stat($file) or croak("Could not stat ($file): $!");
120 3         605 $atime = $sb->atime;
121 3         62 $mtime = $sb->mtime;
122             }
123             else {
124 0 0       0 unless ($self->{_no_create}) {
125 0 0       0 sysopen my $fh,$file,$SYSOPEN_MODE or croak("Can't create $file : $!");
126 0 0       0 close $fh or croak("Can't close $file : $!");
127 0         0 $atime = $time;
128 0         0 $mtime = $time;
129             }
130             }
131 3 100       38 unless ($self->{_mtime_only}) {
132 2         5 $atime = $time;
133 2 50       6 $atime = $self->{_atime} if (defined $self->{_atime});
134             }
135 3 100       7 unless ($self->{_atime_only}) {
136 2         3 $mtime = $time;
137 2 50       7 $mtime = $self->{_mtime} if (defined $self->{_mtime});
138             }
139 3 50       74 if (utime($atime,$mtime,$file)) {
140 3         12 $count++;
141             }
142             }
143 3         17 return $count;
144             }
145              
146             1;
147              
148             __END__