File Coverage

blib/lib/Path/Class/File/Stat.pm
Criterion Covered Total %
statement 39 44 88.6
branch 10 16 62.5
condition 9 12 75.0
subroutine 7 7 100.0
pod 4 4 100.0
total 69 83 83.1


line stmt bran cond sub pod time code
1             package Path::Class::File::Stat;
2 1     1   71921 use strict;
  1         3  
  1         33  
3 1     1   6 use warnings;
  1         3  
  1         34  
4 1     1   17 use base qw( Path::Class::File );
  1         2  
  1         606  
5              
6             our $VERSION = '0.05';
7              
8             my $debug = $ENV{PERL_DEBUG} || 0;
9              
10             sub new {
11 1     1 1 713 my $self = shift->SUPER::new(@_);
12 1         355 $self->{_stat} = $self->stat;
13 1         128 return $self;
14             }
15              
16             sub use_md5 {
17 1     1 1 3 my $self = shift;
18 1 50       5 if ( exists $self->{_md5} ) {
19 0 0       0 $debug and warn "_md5 exists: $self->{_md5}";
20 0         0 return $self->{_md5};
21             }
22 1         5 require Digest::MD5;
23 1         11 $self->{_md5} = Digest::MD5::md5_hex( $self->slurp );
24 1         203 return $self->{_md5};
25             }
26              
27             sub changed {
28 5     5 1 4965 my $self = shift;
29 5         11 my ( $old_sig, $new_sig, $sig_changed, $io_changed, $mtime_changed,
30             $size_changed );
31 5 100       26 if ( exists $self->{_md5} ) {
32 2         13 $old_sig = $self->{_md5};
33 2         11 $new_sig = Digest::MD5::md5_hex( $self->slurp );
34 2 50       921 $debug and warn "old_sig=$old_sig new_sig=$new_sig";
35 2         7 $sig_changed = $old_sig ne $new_sig;
36             }
37 5         23 my $new_stat = $self->stat;
38 5         1438 my $old_stat = $self->{_stat};
39              
40 5   33     122 $io_changed = ( $old_stat->dev ne $new_stat->dev
41             && $old_stat->ino ne $new_stat->ino );
42 5         163 $mtime_changed = $old_stat->mtime ne $new_stat->mtime;
43 5         156 $size_changed = $old_stat->size ne $new_stat->size;
44              
45 5 50       62 if ($debug) {
46 0         0 require Data::Dump;
47 0         0 Data::Dump::dump($new_stat);
48 0         0 Data::Dump::dump($old_stat);
49              
50             #Data::Dump::dump($self);
51             }
52              
53 5 100 66     62 if ( $io_changed || $mtime_changed || $size_changed || $sig_changed ) {
      100        
      100        
54 4 50       15 $debug and warn "$self is not the file it once was\n";
55 4 100       11 $self->{_md5} = $new_sig if $sig_changed;
56 4         15 return $self->restat;
57             }
58 1         27 return 0;
59             }
60              
61             sub restat {
62 5     5 1 1190 my $self = shift;
63 5         8 my $old = $self->{_stat};
64 5         18 $self->{_stat} = $self->stat;
65 5         1000 return $old;
66             }
67              
68             1;
69              
70             __END__