File Coverage

blib/lib/Path/Class/Entity.pm
Criterion Covered Total %
statement 51 52 98.0
branch 11 14 78.5
condition 3 3 100.0
subroutine 20 21 95.2
pod 0 13 0.0
total 85 103 82.5


line stmt bran cond sub pod time code
1 7     7   4572 use strict;
  7         26  
  7         430  
2              
3             package Path::Class::Entity;
4             {
5             $Path::Class::Entity::VERSION = '0.35';
6             }
7              
8 7     7   41 use File::Spec 3.26;
  7         261  
  7         177  
9 7     7   7429 use File::stat ();
  7         73467  
  7         171  
10 7     7   64 use Cwd;
  7         13  
  7         475  
11 7     7   35 use Carp();
  7         24  
  7         315  
12              
13             use overload
14             (
15 7         59 q[""] => 'stringify',
16             'bool' => 'boolify',
17             fallback => 1,
18 7     7   36 );
  7         14  
19              
20             sub new {
21 592     592 0 924 my $from = shift;
22 592 100       1729 my ($class, $fs_class) = (ref($from)
23             ? (ref $from, $from->{file_spec_class})
24             : ($from, $Path::Class::Foreign));
25 592         2997 return bless {file_spec_class => $fs_class}, $class;
26             }
27              
28 27     27 0 98 sub is_dir { 0 }
29              
30             sub _spec_class {
31 73     73   141 my ($class, $type) = @_;
32              
33 73 50       681 die "Invalid system type '$type'" unless ($type) = $type =~ /^(\w+)$/; # Untaint
34 73         177 my $spec = "File::Spec::$type";
35             ## no critic
36 73 50       4637 eval "require $spec; 1" or die $@;
37 73         361 return $spec;
38             }
39              
40             sub new_foreign {
41 14     14 0 770 my ($class, $type) = (shift, shift);
42 14         51 local $Path::Class::Foreign = $class->_spec_class($type);
43 14         70 return $class->new(@_);
44             }
45              
46 2018 100 100 2018   18796 sub _spec { (ref($_[0]) && $_[0]->{file_spec_class}) || 'File::Spec' }
47              
48 28     28 0 198 sub boolify { 1 }
49            
50             sub is_absolute {
51             # 5.6.0 has a bug with regexes and stringification that's ticked by
52             # file_name_is_absolute(). Help it along with an explicit stringify().
53 69     69 0 431 $_[0]->_spec->file_name_is_absolute($_[0]->stringify)
54             }
55              
56 4     4 0 32 sub is_relative { ! $_[0]->is_absolute }
57              
58             sub cleanup {
59 27     27 0 32 my $self = shift;
60 27         52 my $cleaned = $self->new( $self->_spec->canonpath("$self") );
61 27         180 %$self = %$cleaned;
62 27         102 return $self;
63             }
64              
65             sub resolve {
66 3     3 0 15 my $self = shift;
67 3 50       25 Carp::croak($! . " $self") unless -e $self; # No such file or directory
68 3         16 my $cleaned = $self->new( scalar Cwd::realpath($self->stringify) );
69              
70             # realpath() always returns absolute path, kind of annoying
71 3 100       31 $cleaned = $cleaned->relative if $self->is_relative;
72              
73 3         34 %$self = %$cleaned;
74 3         9574 return $self;
75             }
76              
77             sub absolute {
78 13     13 0 78 my $self = shift;
79 13 100       36 return $self if $self->is_absolute;
80 6         20 return $self->new($self->_spec->rel2abs($self->stringify, @_));
81             }
82              
83             sub relative {
84 13     13 0 68 my $self = shift;
85 13         34 return $self->new($self->_spec->abs2rel($self->stringify, @_));
86             }
87              
88 2     2 0 657 sub stat { File::stat::stat("$_[0]") }
89 0     0 0 0 sub lstat { File::stat::lstat("$_[0]") }
90              
91 29     29 0 2824 sub PRUNE { return \&PRUNE; }
92              
93             1;
94             __END__