lib/Badger/Filesystem/Path.pm

filter: Statementstmt 79.2% 126 / 159 Branchbran 73.3% 44 / 60 Conditioncond 37.5% 24 / 64 MC/DCmcdc 5.1% 2 / 39 Subroutinesub 73.6% 28 / 38 Podpod 100% 31 / 31 totaltotal 65.2% 255 / 391 CC 55 SCAR 20.3
CC
55
Cov
69%
above
30.0
below
20.0
new
18.4
CRAP 7.6
« lib/Badger/Modules.pm ↑ Summary lib/Badger/Debug.pm »
1 #========================================================================
2 #
3 # Badger::Filesystem::Path
4 #
5 # DESCRIPTION
6 # OO representation of a path in a filesystem, serving as a base class
7 # for file and directories.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@wardley.org>
11 #
12 #========================================================================
13  
14 package Badger::Filesystem::Path;
15  
16 70 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 use Badger::Class
18 70 version => 0.01,
19 debug => 0,
20 base => 'Badger::Filesystem::Base Badger::Exporter',
21 import => 'class',
22 constants => 'HASH ARRAY TRUE',
23 get_methods => 'path name volume directory',
24 utils => 'blessed',
25 as_text => 'path',
26 is_true => 1,
27 constant => {
28 type => 'Path',
29 STAT_PATH => 17, # offset in extended stat fields
30 },
31 exports => {
32 tags => { fields => '@STAT_FIELDS' },
33 },
34 messages => {
35 no_exist => '%s does not exist: %s',
36 bad_stat => '%s cannot be scanned: %s',
37 bad_look => 'No path specified to look %s',
38 missing => 'No %s specified',
39 70 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 70 ▶use Badger::Timestamp;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 70 ▶use Badger::Filesystem;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 70 ▶use Badger::Filesystem::Directory;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45  
46 70 our $FILESYSTEM = 'Badger::Filesystem';
47 70 our $TIMESTAMP = 'Badger::Timestamp';
48 70 our $MATCH_EXT = qr/\.([^\.]+)$/; # TODO: is this filesystem-specific?
49 70 our @VDN_FIELDS = @Badger::Filesystem::Base::VDN_FIELDS;
50 70 our @STAT_FIELDS = qw( device inode mode links user group device_type
51 size atime mtime ctime block_size blocks
52 readable writeable executable owner );
53 70 our $STAT_FIELD = {
54 # In here we'll store the map from stat field name to number
55 # device => 0,
56 # inode => 1,
57 # ...etc...
58 };
59  
60 70 our $TS_FIELD = {
61 # On the left we have the timestamp methods we want to generate as
62 # wrappers around the stat fields listed on the right.
63 created => 'ctime',
64 accessed => 'atime',
65 modified => 'mtime',
66 };
67  
68 # generate methods to access stat fields: mode(), atime(), ctime(), etc.
69 70 my $n = 0;
70  
71 class->methods(
72 map {
73 70 my $m = $n++; # new lexical variable for closure
74 1190 $STAT_FIELD->{ $_ } = $m; # fill in $STAT_FIELD entry
75 56 ▶ $_ => sub { $_[0]->stats->[$m] } # generate subroutine
Subroutine __ANON__: called CC=1 SCAR=0.0
76 1190 }
77 @STAT_FIELDS
78 );
79  
80 # generate accessed(), created() and modified() methods which return
81 # Badger::Timestamp objects for the atime, ctime and mtime stat values
82  
83 class->methods(
84 map {
85 70 my $method = $_; # new lexical variable for closure
86 210 my $stat = $TS_FIELD->{ $_ };
87 210 my $statno = $STAT_FIELD->{ $stat };
88 $method => sub {
89 3 ▶ return $_[0]->{ $method }
Subroutine __ANON__: called CC=2 SCAR=9.2
Condition33%1/3
$_[0]{$method} ||= $Badger::Filesystem::Path::TIMESTAMP->new($_[0]->stats->[$statno])
l!l&&r!l&&!r
030
MC/DC0%0/2
$_[0]{$method} ||= $Badger::Filesystem::Path::TIMESTAMP->new($_[0]->stats->[$statno])
$_[0]{$method} $Badger::Filesystem::Path::TIMESTAMP->new($_[0]->stats->[$statno])
Truth table33%1/3
A ||= B
A= $_[0]{$method}B= $Badger::Filesystem::Path::TIMESTAMP->new($_[0]->stats->[$statno])
ABresult
1X1
011
000
90 ||= $TIMESTAMP->new( $_[0]->stats->[$statno] )
91 }
92 210 }
93 keys %$TS_FIELD
94 );
95  
96  
97 # define some aliases
98 70 *is_dir = \&is_directory;
99 70 *dir = \&directory;
100 70 *vol = \&volume; # goes up to 11
101 70 *ext = \&extension;
102 70 *base_name = \&basename;
103 70 *up = \&parent;
104 70 *meta = \&metadata;
105 70 *canonical = \&absolute;
106 70 *perms = \&permissions;
107  
108  
109 sub new {
110 566 ▶ my $class = shift; $class = ref $class || $class;
Subroutine new: called CC=13 SCAR=29.1
Pod documented
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
15650
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
111 566 my $args;
112  
113 566 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 559 7 566
114 559 ▶ $args = ref $_[0] eq HASH ? shift
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 545 14 559
ref $_[0] eq 'ARRAY' || !ref($_[0]) ? : 14 0 14
Condition66%2/3
ref $_[0] eq "ARRAY" or not ref $_[0]
l!l&&r!l&&!r
1130
MC/DC0%0/2
ref $_[0] eq "ARRAY" or not ref $_[0]
ref $_[0] eq "ARRAY" not ref $_[0]
Truth table66%2/3
A or not(B)
A= ref $_[0] eq "ARRAY"B= not ref $_[0]
ABresult
1X1
011
000
115 : ref $_[0] eq ARRAY || ! ref $_[0] ? { path => shift }
116 : return $class->error_msg( unexpected => arguments => $_[0] => 'hash ref' )
117 }
118 else {
119 7 $args = { @_ };
120 }
121  
122 # allow short aliases for various configuration options, including
123 # those of directory/file subclasses to make life easy for them.
124 566 ▶ $args->{ filesystem } ||= $args->{ fs } if $args->{ fs };
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"fs"} 0 566 566
Condition0%0/3
$args->{"filesystem"} ||= $args->{"fs"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"filesystem"} ||= $args->{"fs"}
$args->{"filesystem"} $args->{"fs"}
Truth table0%0/3
A ||= B
A= $args->{"filesystem"}B= $args->{"fs"}
ABresult
1X1
011
000
125 566 ▶ $args->{ directory } ||= $args->{ dir } if $args->{ dir };
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"dir"} 1 565 566
Condition33%1/3
$args->{"directory"} ||= $args->{"dir"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{"directory"} ||= $args->{"dir"}
$args->{"directory"} $args->{"dir"}
Truth table33%1/3
A ||= B
A= $args->{"directory"}B= $args->{"dir"}
ABresult
1X1
011
000
126 566 ▶ $args->{ volume } ||= $args->{ vol } if $args->{ vol };
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"vol"} 0 566 566
Condition0%0/3
$args->{"volume"} ||= $args->{"vol"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"volume"} ||= $args->{"vol"}
$args->{"volume"} $args->{"vol"}
Truth table0%0/3
A ||= B
A= $args->{"volume"}B= $args->{"vol"}
ABresult
1X1
011
000
127 566 my $self = bless { }, $class;
128  
129 # maintain a reference to the filesystem that created us, if available,
130 # but don't bother if we didn't get one - we can use the default
131 566 ▶ $self->{ filesystem } = $args->{ filesystem } if $args->{ filesystem };
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"filesystem"} 541 25 566
132 566 $self->init($args);
133 }
134  
135 sub init {
136 33 ▶ my ($self, $config) = @_;
Subroutine init: called CC=2 SCAR=7.0
Pod documented
137 33 ▶ my $path = $config->{ path } || return $self->error_msg( missing => 'path' );
Condition50%1/2
$config->{'path'} || (return $self->error_msg('missing', 'path'))
l!l
330
MC/DC0%0/1
$config->{'path'} || (return $self->error_msg('missing', 'path'))
$config->{'path'}
Truth table50%1/2
A
A= $config->{'path'}
Aresult
11
00
138 33 my $fs = $self->filesystem;
139 33 $path = $self->{ path } = $fs->join_directory($path);
140 33 return $self;
141 }
142  
143 sub is_absolute {
144 29 ▶ my $self = shift;
Subroutine is_absolute: called CC=2 SCAR=6.9
Pod documented
145 $self->{ absolute } = $self->filesystem->is_absolute($self->{ path })
146 29 ▶ unless defined $self->{ absolute };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"absolute"} 23 6 29
147 29 return $self->{ absolute };
148 }
149  
150 sub is_relative {
151 2 ▶ shift->is_absolute ? 0 : 1;
Subroutine is_relative: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->is_absolute ? : 1 1 2
152 }
153  
154 sub is_file {
155 0 ▶ my $self = shift;
Subroutine is_file: not called CC=2 SCAR=17.9
Pod documented
156 0 ▶ my $defn = $self->filesystem->definitive_read($self->{ path }) || return;
Condition0%0/2
$self->filesystem->definitive_read($self->{'path'}) || (return)
l!l
00
MC/DC0%0/1
$self->filesystem->definitive_read($self->{'path'}) || (return)
$self->filesystem->definitive_read($self->{'path'})
Truth table0%0/2
A
A= $self->filesystem->definitive_read($self->{'path'})
Aresult
11
00
157 0 return -f $defn;
158 }
159  
160 sub is_directory {
161 0 ▶ my $self = shift;
Subroutine is_directory: not called CC=2 SCAR=17.9
Pod documented
162 0 ▶ my $defn = $self->filesystem->definitive_read($self->{ path }) || return;
Condition0%0/2
$self->filesystem->definitive_read($self->{'path'}) || (return)
l!l
00
MC/DC0%0/1
$self->filesystem->definitive_read($self->{'path'}) || (return)
$self->filesystem->definitive_read($self->{'path'})
Truth table0%0/2
A
A= $self->filesystem->definitive_read($self->{'path'})
Aresult
11
00
163 0 return -d $defn;
164 }
165  
166 sub absolute {
167 21 ▶ my $self = shift;
Subroutine absolute: called CC=2 SCAR=6.9
Pod documented
168 return $self->is_absolute
169 ? $self->{ path }
170 21 ▶ : $self->filesystem->absolute($self->{ path });
Branch: 1 branch
BranchTrueFalseTotal
$self->is_absolute ? : 14 7 21
171 }
172  
173 sub relative {
174 78 ▶ my $self = shift;
Subroutine relative: called CC=2 SCAR=6.9
Pod documented
175 78 my $fs = $self->filesystem;
176 78 my $path = $fs->join_directory(@_);
177 # If the path isn't already absolute then we merge it onto our
178 # directory or path if directory is undefined. By calling the
179 # base() method, we allow the file subclass to return its
180 # parent directory so that things Just Work[tm]
181 # $self->debug("relative path: $path is_absolute?\n");
182 78 ▶ return $fs->is_absolute($path)
Branch: 1 branch
BranchTrueFalseTotal
$fs->is_absolute($path) ? : 3 75 78
183 ? $path
184 : $fs->collapse_directory( $fs->join_directory($self->base, $path) );
185 }
186  
187 sub definitive {
188 4 ▶ my $self = shift;
Subroutine definitive: called CC=3 SCAR=14.2
Pod documented
189 # use the definitive path from the last stat or fetch anew
190 return
191 ($self->{ stats } && $self->{ stats }->[STAT_PATH])
192 4 ▶ || $self->filesystem->definitive($self->{ path });
Condition33%2/6
$self->{'stats'} && $self->{'stats'}[17] || $self->filesystem->definitive($self->{'path'})
l!l&&r!l&&!r
040
$self->{"stats"} and $self->{"stats"}[17]
!ll&&!rl&&r
400
MC/DC0%0/3
$self->{'stats'} && $self->{'stats'}[17] || $self->filesystem->definitive($self->{'path'})
$self->{"stats"} $self->{"stats"}[17] $self->filesystem->definitive($self->{'path'})
Truth table20%1/5
A and B || C
A= $self->{"stats"}B= $self->{"stats"}[17]C= $self->filesystem->definitive($self->{'path'})
ABCresult
11X1
0X11
1011
0X00
1000
193 }
194  
195 sub collapse {
196 0 ▶ my $self = shift;
Subroutine collapse: not called CC=1 SCAR=6.9
Pod documented
197 0 my $fs = $self->filesystem;
198 0 $self->{ directory } = $fs->collapse_directory( $self->{ directory } );
199 0 $self->{ path } = $fs->join_path( @$self{ @VDN_FIELDS } );
200 0 return $self;
201 }
202  
203 sub above {
204 0 ▶ my $self = shift;
Subroutine above: not called CC=5 SCAR=34.0
Pod documented
205 0 my $this = quotemeta $self->collapse->path;
206 0 ▶ my $that = shift || return $self->error_msg( bad_look => 'above' );
Condition0%0/2
shift() || (return $self->error_msg('bad_look', 'above'))
l!l
00
MC/DC0%0/1
shift() || (return $self->error_msg('bad_look', 'above'))
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
207 0 ▶ $that = $self->new("$that") unless blessed $that && $that->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($that) and $that->isa(__PACKAGE__) 0 0 0
Condition0%0/3
builtin::blessed($that) and $that->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($that) and $that->isa(__PACKAGE__)
builtin::blessed($that) $that->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= builtin::blessed($that)B= $that->isa(__PACKAGE__)
ABresult
0X0
100
111
208 0 $that = $that->collapse->path;
209 0 ▶ $self->debug("does $that match /^$this/ ??\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::Path::DEBUG 0 0 0
210 0 $that =~ /^$this/;
211 }
212  
213 sub below {
214 0 ▶ my $self = shift;
Subroutine below: not called CC=4 SCAR=30.0
Pod documented
215 0 ▶ my $that = shift || return $self->error_msg( bad_look => 'above' );
Condition0%0/2
shift() || (return $self->error_msg('bad_look', 'above'))
l!l
00
MC/DC0%0/1
shift() || (return $self->error_msg('bad_look', 'above'))
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
216 0 ▶ $that = $self->new("$that") unless blessed $that && $that->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($that) and $that->isa(__PACKAGE__) 0 0 0
Condition0%0/3
builtin::blessed($that) and $that->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($that) and $that->isa(__PACKAGE__)
builtin::blessed($that) $that->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= builtin::blessed($that)B= $that->isa(__PACKAGE__)
ABresult
0X0
100
111
217 0 $that->above($self);
218 }
219  
220 sub base {
221 3 ▶ my $self = shift;
Subroutine base: called CC=2 SCAR=8.1
Pod documented
222 3 ▶ return $self->{ directory } || $self->{ path };
Condition33%1/3
$self->{'directory'} || $self->{'path'}
l!l&&r!l&&!r
030
MC/DC0%0/2
$self->{'directory'} || $self->{'path'}
$self->{'directory'} $self->{'path'}
Truth table33%1/3
A || B
A= $self->{'directory'}B= $self->{'path'}
ABresult
1X1
011
000
223 }
224  
225 sub parent {
226 85 ▶ my $self = shift;
Subroutine parent: called CC=6 SCAR=18.0
Pod documented
227 85 ▶ my $skip = shift || 0;
Condition100%2/2
shift() || 0
l!l
4837
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
228 my $parent = $self->{ parent }
229 ||= $self->filesystem->directory(
230 85 ▶ $self->{ directory } ||= $self->path_up
Condition66%4/6
$self->{"parent"} ||= $self->filesystem->directory($self->{"directory"} ||= $self->path_up)
l!l&&r!l&&!r
21640
$self->{"directory"} ||= $self->path_up
l!l&&r!l&&!r
44200
MC/DC0%0/2
$self->{"parent"} ||= $self->filesystem->directory($self->{"directory"} ||= $self->path_up)
$self->{"parent"} $self->filesystem->directory($self->{"directory"} ||= $self->path_up)
MC/DC0%0/2
$self->{"directory"} ||= $self->path_up
$self->{"directory"} $self->path_up
Truth table66%2/3
A ||= B
A= $self->{"parent"}B= $self->filesystem->directory($self->{"directory"} ||= $self->path_up)
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $self->{"directory"}B= $self->path_up
ABresult
1X1
011
000
231 );
232  
233 return
234 # don't return parents above the root
235 85 ▶ $self->{ path } eq $parent->{ path } ? $self
Branch: 2 branches
BranchTrueFalseTotal
$self->{'path'} eq $parent->{'path'} ? : 3 82 85
$skip ? : 46 36 82
236 # delegate to parent if there are generations to skip
237 : $skip ? $parent->parent($skip - 1)
238 # otherwise we've found the parent we're looking for
239 : $parent;
240 }
241  
242 sub path_up {
243 20 ▶ my $self = shift;
Subroutine path_up: called CC=5 SCAR=17.0
Pod documented
244 20 my $fs = $self->filesystem;
245 20 my $path = $fs->split_directory($self->{ path });
246  
247 20 ▶ $self->debug("split path [$path] into [", join(', ', @$path), "]\n")
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::Path::DEBUG 0 20 20
248 if $DEBUG;
249  
250 20 ▶ if (@$path > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$path > 1) { } 17 3 20
elsif (@$path == 1) { } 3 0 3
251 # multiple items in path can be relative or absolute - we're not
252 # fussed. e.g. /foo/bar ==> /foo or foo/bar ==> foo
253 17 pop(@$path);
254 }
255 elsif (@$path == 1) {
256 # if there's a single item in a path then it's either a single
257 # relative path item (e.g. 'foo' ==> ['foo']), in which case we
258 # return the current working directory, or it's an empty item
259 # indicating the root directory (e.g. '/' => ['']) in which case we
260 # do nothing, because you can't go up from the root directory.
261 3 ▶ if (length $path->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $path->[0]) 3 0 3
262 3 return $fs->cwd;
263 }
264 0 $self->not_implemented("going up from relative paths");
265 }
266 else {
267 0 $self->error("Invalid path (no elements)\n");
268 }
269  
270 17 return $fs->join_directory($path);
271 }
272  
273 sub exists {
274 0 ▶ shift->stat;
Subroutine exists: not called CC=1 SCAR=6.9
Pod documented
275 }
276  
277 sub must_exist {
278 69 ▶ my $self = shift;
Subroutine must_exist: called CC=4 SCAR=14.8
Pod documented
279  
280 69 ▶ unless ($self->exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->exists) 4 65 69
281 4 ▶ if (@_ && $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $_[0]) { } 4 0 4
Condition33%1/3
@_ and $_[0]
!ll&&!rl&&r
004
MC/DC0%0/2
@_ and $_[0]
@_ $_[0]
Truth table33%1/3
A and B
A= @_B= $_[0]
ABresult
0X0
100
111
282 4 my $flag = shift;
283 # true flag indicates we should attempt to create it
284 4 $self->create(@_); # pass any other args, like dir file permission
285 }
286 else {
287 0 return $self->error_msg( no_exist => $self->type, $self->{ path } );
288 }
289 }
290 69 return $self;
291 }
292  
293 sub create {
294 0 ▶ shift->not_implemented;
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
295 }
296  
297 sub stat {
298 38 ▶ my $self = shift->must_exist;
Subroutine stat: called CC=3 SCAR=11.7
Pod documented
299 my $stats = $self->filesystem->stat_path($self->{ path })
300 38 ▶ || return $self->decline_msg( not_found => file => $self->{ path } );
Condition50%1/2
$self->filesystem->stat_path($self->{'path'}) || (return $self->decline_msg('not_found', 'file', $self->{'path'}))
l!l
380
MC/DC0%0/1
$self->filesystem->stat_path($self->{'path'}) || (return $self->decline_msg('not_found', 'file', $self->{'path'}))
$self->filesystem->stat_path($self->{'path'})
Truth table50%1/2
A
A= $self->filesystem->stat_path($self->{'path'})
Aresult
11
00
301  
302 # the definitive path can be tagged on the end
303 # $self->{ definitive } = $stats->[STAT_PATH]
304 # if defined $stats->[STAT_PATH];
305  
306 return wantarray
307 38 ▶ ? @$stats
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 38 38
308 : $stats;
309 }
310  
311 sub stats {
312 59 ▶ my $stats = $_[0]->{ stats } ||= $_[0]->stat;
Subroutine stats: called CC=3 SCAR=11.7
Pod documented
Condition66%2/3
$_[0]{"stats"} ||= $_[0]->stat
l!l&&r!l&&!r
21380
MC/DC0%0/2
$_[0]{"stats"} ||= $_[0]->stat
$_[0]{"stats"} $_[0]->stat
Truth table66%2/3
A ||= B
A= $_[0]{"stats"}B= $_[0]->stat
ABresult
1X1
011
000
313 return wantarray
314 59 ▶ ? @$stats
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 59 59
315 : $stats;
316 }
317  
318 sub restat {
319 0 ▶ my $self = shift;
Subroutine restat: not called CC=1 SCAR=6.9
Pod documented
320 0 delete $self->{ stats };
321 0 delete @$self{ keys %$TS_FIELD }; # timestamps for created, modified, etc.
322 0 return $self->stats;
323 }
324  
325 sub permissions {
326 1 ▶ shift->mode & 0777;
Subroutine permissions: called CC=1 SCAR=0.0
Pod documented
327 }
328  
329 sub chmod {
330 1 ▶ my $self = shift;
Subroutine chmod: called CC=1 SCAR=0.0
Pod documented
331 1 $self->filesystem->chmod_path($self->{ path }, @_);
332 1 return $self;
333 }
334  
335 sub basename {
336 3 ▶ my $self = shift;
Subroutine basename: called CC=2 SCAR=6.9
Pod documented
337 3 my $name = $self->name;
338 3 ▶ $name = $self->{ path } unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 2 1 3
339 3 $name =~ s/$MATCH_EXT//g;
340 3 return $name;
341 }
342  
343 sub extension {
344 2 ▶ my $self = shift;
Subroutine extension: called CC=2 SCAR=7.2
Pod documented
345 2 ▶ return $self->{ path } =~ $MATCH_EXT
Branch: 1 branch
BranchTrueFalseTotal
$self->{'path'} =~ /$Badger::Filesystem::Path::MATCH_EXT/ ? : 2 0 2
346 ? $1
347 : '';
348 }
349  
350 sub filesystem {
351 1150 ▶ my $self = shift;
Subroutine filesystem: called CC=3 SCAR=11.0
Pod documented
352 1150 ▶ return $self->class->any_var('FILESYSTEM')->prototype
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 1 1149 1150
353 unless ref $self;
354 $self->{ filesystem }
355 1149 ▶ ||= $self->class->any_var('FILESYSTEM')->prototype;
Condition66%2/3
$self->{"filesystem"} ||= $self->class->any_var("FILESYSTEM")->prototype
l!l&&r!l&&!r
1124250
MC/DC0%0/2
$self->{"filesystem"} ||= $self->class->any_var("FILESYSTEM")->prototype
$self->{"filesystem"} $self->class->any_var("FILESYSTEM")->prototype
Truth table66%2/3
A ||= B
A= $self->{"filesystem"}B= $self->class->any_var("FILESYSTEM")->prototype
ABresult
1X1
011
000
356 }
357  
358 sub visit {
359 14 ▶ my $self = shift;
Subroutine visit: called CC=1 SCAR=0.0
Pod documented
360 14 my $visitor = $self->filesystem->visitor(@_);
361 14 $visitor->visit($self);
362 14 return $visitor;
363 }
364  
365 sub collect {
366 2 ▶ shift->visit(@_)->collect;
Subroutine collect: called CC=1 SCAR=0.0
Pod documented
367 }
368  
369 sub enter {
370 # enter() is a custom accept() method for the entry point of a visitor
371 0 ▶ shift->accept;
Subroutine enter: not called CC=1 SCAR=6.9
Pod documented
372 }
373  
374 sub accept {
375 0 ▶ $_[1]->visit_path($_[0]);
Subroutine accept: not called CC=1 SCAR=6.9
Pod documented
376 }
377  
378 sub metadata {
379 3 ▶ my $self = shift;
Subroutine metadata: called CC=5 SCAR=16.1
Pod documented
380 3 ▶ my $meta = $self->{ metadata } ||= { };
Condition100%2/2
$self->{"metadata"} ||= {}
l!l
21
MC/DC100%1/1
$self->{"metadata"} ||= {}
$self->{"metadata"}
Truth table100%2/2
A
A= $self->{"metadata"}
Aresult
11
00
381 3 ▶ if (@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 1 2 3
elsif (@_ > 1) { } 1 1 2
382 1 return $meta->{ $_[0] };
383 }
384 elsif (@_ > 1) {
385 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
386 1 my $key = shift;
387 1 $meta->{ $key } = shift;
388 }
389 }
390 2 return $meta;
391 }
392  
393 70 1;
394  
395 =encoding utf8
396  
397 =head1 NAME
398  
399 Badger::Filesystem::Path - generic fileystem path object
400  
401 =head1 SYNOPSIS
402  
403 # using Badger::Filesytem constructor subroutine
404 use Badger::Filesystem 'Path';
405  
406 # use native OS-specific paths:
407 $path = Path('/path/to/something');
408  
409 # or generic OS-independant paths
410 $path = Path('path', 'to', 'something');
411  
412 # manual object construction
413 use Badger::Filesystem::Path;
414  
415 # positional arguments
416 $path = Badger::Filesystem::Path->new('/path/to/something');
417 $path = Badger::Filesystem::Path->new(['path', 'to', 'something']);
418  
419 # named parameters
420 $path = Badger::Filesystem::Path->new(
421 path => '/path/to/something'
422 );
423 $path = Badger::Filesystem::Path->new(
424 path => ['path', 'to', 'something']
425 );
426  
427 # path inspection methods
428 $path->path; # current path
429 $path->base; # parent directory or path itself
430 $path->parent; # directory object for base
431 $path->extension # filename .XXX extension
432 $path->basename # filename without .XXX extension
433 $path->is_absolute; # path is absolute
434 $path->is_relative; # path is relative
435 $path->exists; # returns true/false
436 $path->must_exist; # throws error if not
437 @stats = $path->stat; # returns list
438 $stats = $path->stat; # returns list ref
439  
440 # path translation methods
441 $path->relative; # relative to cwd
442 $path->relative($base); # relative to $base
443 $path->absolute; # relative to filesystem root
444 $path->definitive; # physical file location
445 $path->collapse; # resolve '.' and '..' in $path
446  
447 # path comparison methods
448 $path->above($another_path); # $path is ancestor of $another_path
449 $path->below($another_path); # $path is descendant of $another_path
450  
451 =head1 INTRODUCTION
452  
453 This is the documentation for the C<Badger::Filesystem::Path> module.
454 It defines a base class object for the L<Badger::Filesystem::File> and
455 L<Badger::Filesystem::Directory> objects which inherit (and in some cases
456 redefine) the methods described below.
457  
458 In other words, you should read this documentation first if you're working
459 with L<Badger::Filesystem::File> or L<Badger::Filesystem::Directory> objects.
460  
461 =head1 DESCRIPTION
462  
463 The C<Badger::Filesystem::Path> module defines a base class object for
464 representing paths in a real or virtual file system.
465  
466 You can create a generic path object (e.g. to represent a path that doesn't
467 relate to a specific file or directory in a file system), using the C<Path>
468 constructor method in L<Badger::Filesystem>.
469  
470 use Badger::Filesystem 'Path';
471  
472 my $path = Path('/path/to/something');
473  
474 However in most cases you'll want to create a file or directory subclass
475 object. The easiest way to do that is like this:
476  
477 use Badger::Filesystem 'File Path';
478  
479 my $file = File('/path/to/file');
480 my $dir = Dir('/path/to/dir');
481  
482 If you're concerned about portability to other operating systems and/or
483 file systems, then you can specify paths as a list or reference to a list
484 of component names.
485  
486 my $file = File('path', 'to', 'file');
487 my $dir = Dir(['path', 'to', 'dir']);
488  
489 =head1 METHODS
490  
491 =head2 new($path)
492  
493 Constructor method to create a new C<Badger::Filesystem::Path> object.
494 The path can be specified as a single positional argument, either as a
495 text string or reference to list of path components.
496  
497 # single text string
498 $path = Badger::Filesystem::Path->new('/path/to/something');
499  
500 # reference to list
501 $path = Badger::Filesystem::Path->new(['path', 'to', 'something']);
502  
503 It can also be specified as a C<path> named parameter.
504  
505 # named parameter list
506 $path = Badger::Filesystem::Path->new(
507 path => '/path/to/something'
508 );
509  
510 # reference to hash of named parameter(s)
511 $path = Badger::Filesystem::Path->new({
512 path => '/path/to/something'
513 });
514  
515 The constructor method also recognises the C<filesystem> named parameter which
516 can contain a reference to the L<Badger::Filesystem> object or class that
517 created it. In most cases you can rely on the L<Badger::Filesystem> to create
518 path objects for you, using either the L<path()|Badger::Filesystem/path()>
519 method, or the L<Path()|Badger::Filesystem/Path()> subroutine.
520  
521 use Badger::Filesystem 'FS Path';
522  
523 # FS is alias for 'Badger::Filesystem'
524 # Path() is constructor subrooutine
525 my $path;
526  
527 # using the path() method
528 $path = FS->path('/path/to/something');
529 $path = FS->path('path', 'to', 'something');
530 $path = FS->path(['path', 'to', 'something']);
531  
532 # using the Path() subroutine
533 $path = Path('/path/to/something');
534 $path = Path('path', 'to', 'something');
535 $path = Path(['path', 'to', 'something']);
536  
537 The examples that follow will use the C<Path()> constructor subroutine.
538  
539 =head2 init(\%config)
540  
541 Default initialisation method which subclasses (e.g.
542 L<Badger::Filesystem::Directory> and L<Badger::Filesystem::File>) can
543 redefine.
544  
545 =head2 path()
546  
547 This method returns the path as a text string. It is called automatically
548 whenever the path object is stringified.
549  
550 =head2 is_absolute()
551  
552 Returns true if the path is absolute, false if not.
553  
554 =head2 is_relative()
555  
556 Returns true if the path is relative, false if not.
557  
558 =head2 absolute($base)
559  
560 Returns an absolute representation of the path, relative to the C<$base>
561 path passed as an argument, or the current working directory if C<$base>
562 is not specified.
563  
564 # assume cwd is /foo/bar,
565 my $path = Path('/baz/bam');
566  
567 print $path->absolute; # /foo/bar/baz/bam
568 print $path->absolute('/wiz'); # /wiz/baz/bam
569  
570 =head2 relative($base)
571  
572 Returns a relative representation of the path, relative to the C<$base>
573 path passed as an argument, or the current working directory if C<$base>
574 is not specified.
575  
576 # assume cwd is /foo/bar,
577 my $path = Path('/foo/bar/baz/bam');
578  
579 print $path->relative; # /baz/bam
580 print $path->relative('/foo'); # /bar/baz/bam
581  
582 =head2 definitive()
583  
584 Returns the definitive representation of the path which in most cases will
585 be the same as the L<absolute()> path.
586  
587 However, if you're using a L<virtual filesystem|Badger::Filesystem::Virtual>,
588 then the I<definitive> path I<will> include the virtual root directory,
589 whereas a the I<absolute> path will I<not>.
590  
591 my $vfs = Badger::Filesystem::Virtual->new( root => '/my/vfs' );
592 my $path = $vfs->file('/foo/bar');
593 print $path->absolute; # /foo/bar
594 print $path->definitive; # /my/vfs/foo/bar
595  
596 =head2 canonical()
597  
598 This method returns the canonical representation of the path. In most cases
599 this is the same as the absolute path (in fact the base class aliases the
600 C<canonical()> method directly to the L<absolute()> method).
601  
602 print Path('foo')->canonical; # /your/current/path/foo
603 print Path('/foo/bar')->canonical; # /foo/bar
604 print Path('/foo/bar/')->canonical; # /foo/bar
605 print Path('/foo/bar.txt')->canonical; # /foo/bar.txt
606  
607 Note that the C<Badger::Filesystem::Path> base class will I<remove> any
608 trailing slashes (or whatever the appropriate directory separator is for your
609 filesystem) from the end of an absolute path.
610  
611 In the case of directories, implemented by the
612 L<Badger::Filesystem::Directory> subclass, a trailing slash (or relevant
613 separator for your filesystem) will be added.
614  
615 print Dir('/foo/bar')->canonical; # /foo/bar/
616  
617 This is done by delegation to the
618 L<slash_directory()|Badger::Filesystem/slash_directory()> method in
619 L<Badger::Filesystem>.
620  
621 =head2 collapse()
622  
623 Reduces the path to its simplest form by resolving and removing any C<.>
624 (current directory) and C<..> (parent directory) components (or whatever the
625 corresponding tokens are for the current and parent directories of your
626 filesystem).
627  
628 my $path = Path('/foo/bar/../baz')->collapse;
629 print $path; # /foo/baz
630  
631 See the L<collapse_dir()|Badger::Filesystem/collapse_dir()> method in
632 L<Badger::Filesystem> for further information.
633  
634 =head2 above($child)
635  
636 Returns true if the path is "above" the C<$child> path passed as an argument.
637 Formally, we say that the path is an I<ancestor> of C<$child> meaning that it
638 is the parent directory, or grand-parent, or great-grand-parent, and so on.
639  
640 my $parent = Path('/foo/bar');
641 my $child = Path('/foo/bar/baz');
642 $parent->above($child); # true
643  
644 This is implemented as a simple prefix match. That is, the parent path must
645 appear at the start of the child path. Consequently, this method will not
646 account for symbolic links or other similar filesystem features, and it may
647 not work properly on systems that don't follow this convention (although there
648 are none that I'm aware of).
649  
650 =head2 below($parent)
651  
652 Returns true if the path is "below" the C<$parent> path passed as an argument.
653 Formally, we say that the path is a I<descendant> of C<$parent> meaning that it
654 is an immediate sub-directory, or sub-sub-directory, and so on.
655  
656 my $parent = Path('/foo/bar');
657 my $child = Path('/foo/bar/baz');
658 $child->below($parent); # true
659  
660 Like L<above()>, this is implemented using a simple prefix match.
661  
662 =head2 base()
663  
664 Returns the base directory of a path. For L<Badger::Filesystem::Path> and
665 L<Badger::Filesystem::Directory> objects, this method will return the complete
666 path.
667  
668 print Path('/foo/bar')->base; # /foo/bar
669 print Directory('/foo/bar')->base; # /foo/bar
670  
671 However the L<Badger::Filesystem::File> module returns the parent directory in
672 which the file is located.
673  
674 print File('/foo/bar')->base; # /foo
675  
676 =head2 parent($skip_generations) / up($skip_generations)
677  
678 Returns a L<Badger::Filesystem::Directory> object representing the parent
679 directory for a path.
680  
681 Path->('/foo/bar')->parent; # path object for /foo
682  
683 A numerical argument can be provided to indicate the number of generation
684 you want to skip. A value of C<0> is the same as providing no argument - it
685 returns the parent. A value of C<1> skips the parent and returns the
686 grand-parent, and so on.
687  
688 Path->('/foo/bar/baz/bam')->parent(2); # path object for /foo
689  
690 The root directory will be returned if you try to skip too many generations.
691  
692 Path->('/foo/bar/baz/bam')->parent(20); # path object for /
693  
694 =head2 path_up()
695  
696 This returns a text string representing the parent of a path. If the path
697 contains multiple items (e.g. '/foo/bar' or 'foo/bar') then the last item
698 will be removed (e.g. resulting in '/foo' or 'foo' respectively). If an
699 absolute path contains one item or none (e.g. '/foo' or '/') then the
700 root directory ('/') will be returned. A relative path with only one item
701 (e.g. 'foo') is assumed to be relative to the current working directory
702 which will be returned (e.g. '/path/to/current/dir').
703  
704 =head2 exists()
705  
706 Returns true if the path exists in the filesystem (e.g. as a file, directory,
707 or some other entry), or false if not.
708  
709 if ($path->exists) {
710 print "$path already exists\n";
711 }
712 else {
713 print "Creating $path\n";
714 # ...etc...
715 }
716  
717 =head2 must_exist($create)
718  
719 Checks that the path exists (by calling L<exists()>) and throws an error
720 if it doesn't.
721  
722 $path->must_exist; # no need to check return value
723  
724 The C<$create> flag can be set to have it attempt to L<create()> itself if it
725 doesn't already exist. However, this only makes sense for file and directory
726 subclasses and not base class paths.
727  
728 $dir->must_exist(1); # create if it doesn't
729  
730 =head2 create()
731  
732 In the base class this will method will throw an error. You can't physically
733 create an abstract path unless you know what kind of concrete entity (e.g.
734 file or directory) it maps onto. In other words, the L<create()> method will
735 only work for the L<Badger::Filesystem::File> and
736 L<Badger::Filesystem::Directory> subclasses.
737  
738 $path->create; # FAIL
739 $dir->create; # OK
740 $file->create; # OK
741  
742 =head2 chmod($perms)
743  
744 This method changes the file permissions on a file or directory.
745  
746 $file->chmod(0775);
747  
748 =head2 stat()
749  
750 Performs a filesystem C<stat> on the path and returns a list (in list
751 context), or a reference to a list (in scalar context) containing the 13
752 information elements.
753  
754 @list = $path->stat; # list context
755 $list = $path->stat; # scalar context
756  
757 A summary of the fields is shown below. See C<perldoc -f stat> for complete
758 details. Each of the individual fields can also be accessed via their own
759 methods, also listed in the table.
760  
761 Field Method Description
762 ------------------------------------------------------------------------
763 0 device() device number of filesystem
764 1 inoode() inode number
765 2 mode() file mode (type and permissions)
766 3 links() number of (hard) links to the file
767 4 user() numeric user ID of file’s owner
768 5 group() numeric group ID of file’s owner
769 6 device_type() the device identifier (special files only)
770 7 size() total size of file, in bytes
771 8 atime() last access time in seconds since the epoch
772 9 mtime() last modify time in seconds since the epoch
773 10 ctime() inode change time in seconds since the epoch (*)
774 11 block_size() preferred block size for file system I/O
775 12 blocks() actual number of blocks allocated
776  
777 In addition to those that are returned by Perl's inbuilt C<stat> function,
778 this method returns four additional flags.
779  
780 13 readable() file is readable by current process
781 14 writeable() file is writeable by current process
782 15 executable() file is executable by current process
783 16 owner() file is owned by current process
784  
785 =head2 stats()
786  
787 A wrapper around the L<stat()> method which caches the results to avoid
788 making repeated filesystem calls.
789  
790 @list = $path->stats; # list context
791 $list = $path->stats; # scalar context
792  
793 Note that the L<accessed()>, L<created()> and L<modified()> methods also
794 cache the L<Badger::Timestamp> objects they create to represent the
795 access, creation and modification times respectively.
796  
797 =head2 restat()
798  
799 Clears any cached values stored by the L<stats()>, L<accessed()>,
800 L<created()> and L<modified()> methods and calls L<stats()> to reload
801 (and re-cache) the data from a L<stat()> call.
802  
803 =head2 device()
804  
805 Returns the device number for the file. See L<stat()>.
806  
807 =head2 inode()
808  
809 Returns the inode number for the file. See L<stat()>.
810  
811 =head2 mode()
812  
813 Returns the file mode for the file. Note that this contains both the
814 file type and permissions. See L<stat()>.
815  
816 =head2 permissions() / perms()
817  
818 Returns the file permissions. This is equivalent to
819 C<< $file->mode & 0777 >>.
820  
821 =head2 links()
822  
823 Returns the number of hard links to the file. See L<stat()>.
824  
825 =head2 user()
826  
827 Returns the numeric user ID of the file's owner. See L<stat()>.
828  
829 =head2 group()
830  
831 Returns the numeric group ID of the file's group. See L<stat()>.
832  
833 =head2 device_type()
834  
835 Returns the device identifier (for special files only). See L<stat()>.
836  
837 =head2 size()
838  
839 Returns the total size of the file in bytes. See L<stat()>.
840  
841 =head2 atime()
842  
843 Returns the time (in seconds since the epoch) that the file was last accessed.
844 See L<stat()>.
845  
846 =head2 accessed()
847  
848 Returns a L<Badger::Timestamp> object for the L<atime()> value. This object
849 will auto-stringify to produce an ISO-8601 formatted date. You can also
850 call various methods to access different parts of the time and/or date.
851  
852 print $file->accessed; # 2009/04/20 16:25:00
853 print $file->accessed->date; # 2009/04/20
854 print $file->accessed->year; # 2009
855  
856 =head2 mtime()
857  
858 Returns the time (in seconds since the epoch) that the file was last modified.
859 See L<stat()>.
860  
861 =head2 modified()
862  
863 Returns a L<Badger::Timestamp> object for the L<mtime()> value.
864  
865 print $file->modified; # 2009/04/20 16:25:00
866 print $file->modified->time; # 16:25:0
867 print $file->modified->hour; # 16
868  
869 =head2 ctime()
870  
871 Returns the time (in seconds since the epoch) that the file was created. See
872 L<stat()>.
873  
874 =head2 created()
875  
876 Returns a L<Badger::Timestamp> object for the L<ctime()> value.
877  
878 print $file->created; # 2009/04/20 16:25:00
879 print $file->created->date; # 2009/04/20
880 print $file->created->time; # 16:25:00
881  
882 =head2 block_size()
883  
884 Returns the preferred block size for file system I/O on the file. See
885 L<stat()>.
886  
887 =head2 blocks()
888  
889 Returns the actual number of blocks allocated to the file. See L<stat()>.
890  
891 =head2 readable()
892  
893 Returns a true value if the file is readable by the current user (i.e. the
894 owner of the current process), false if not. See L<stat()>.
895  
896 =head2 writeable()
897  
898 Returns a true value if the file is writeable by the current user (i.e. the
899 owner of the current process), false if not. See L<stat()>.
900  
901 =head2 executable()
902  
903 Returns a true value if the file is executable by the current user (i.e. the
904 owner of the current process), false if not. See L<stat()>.
905  
906 =head2 owner()
907  
908 Returns a true value if the file is owned by the current user (i.e. the
909 owner of the current process), false if not. See L<stat()>.
910  
911 =head2 filesystem()
912  
913 Returns a reference to a L<Badger::Filesystem> object, or the name of the
914 filesystem class (e.g. L<Badger::Filesystem> or a subclass) that created
915 the path object. If this is undefined then the default value defined in
916 the L<$FILESYSTEM> class variable is returned. Unless you've changed it,
917 or re-defined it in a subclass, this value will be C<Badger::Filesystem>.
918  
919 The end result is that you can use the C<filesystem> method to access a
920 L<Badger::Filesystem> object or class through which you can perform other
921 filesystem related operations. This is used internally by a number of
922 method.
923  
924 # access filesystem via existing path
925 $path->filesystem->dir('/a/new/directory/object');
926  
927 # same as
928 Badger::Filesystem->dir('/a/new/directory/object');
929  
930 =head2 visit($visitor)
931  
932 Entry point for a filesystem visitor to visit a filesystem path. A
933 reference to a L<Badger::Filesystem::Visitor> object (or subclass) should
934 be passed as the first argument.
935  
936 use Badger::Filesystem::Visitor;
937  
938 my $visitor = Badger::Filesystem::Visitor->new( recurse => 1 );
939 $path->visit($visitor);
940  
941 Alternately, a list or reference to a hash array of named parameters may be
942 provided. These will be used to instantiate a new
943 L<Badger::Filesystem::Visitor> object (via the L<Badger::Filesystem>
944 L<visitor()|Badger::Filesystem/visitor()> method) which will then be
945 applied to the path. If no arguments are passed then a visitor is created
946 with a default configuration.
947  
948 # either list of named params
949 $path->visit( recurse => 1 );
950  
951 # or reference to hash array
952 $path->visit({ recurse => 1});
953  
954 The method then calls the visitor
955 L<visit()|Badger::Filesystem::Visitor/visit()> passing C<$self> as an argument
956 to begin the visit.
957  
958 =head2 accept($visitor)
959  
960 This method is called to dispatch a visitor to the correct method for a
961 filesystem object. In the L<Badger::Filesystem::Path> base class, it calls the
962 visitor L<visit_path()|Badger::Filesystem::Visitor/visit_path()> method,
963 passing the C<$self> object reference as an argument. Subclasses redefine this
964 method to call other visitor methods.
965  
966 =head2 enter($visitor)
967  
968 This is a special case of the L<accept()> method which subclasses (e.g.
969 L<directory|Badger::Filesystem::Directory>) use to differentiate between the
970 initial entry point of a visitor and subsequent visits to directories
971 contained therein. In the base class it simply delegates to the L<accept()>
972 method.
973  
974 =head2 collect(\%params)
975  
976 This is a short-cut to call the L<visit()> method and then the
977 L<collect()|Badger::Filesystem::Visitor/collect()> method on the
978 L<Badger::Filesystem::Visitor> object returned.
979  
980 # short form
981 my @items = $path->collect( files => 1, dirs => 0 );
982  
983 # long form
984 my @items = $path->visit( files => 1, dirs => 0 )->collect;
985  
986 =head2 metadata() / meta()
987  
988 This method allows you to associate metadata with a path. The method
989 accepts multiple arguments to set metadata:
990  
991 $path->metadata( title => 'An Example', author => 'Arthur Dent' );
992  
993 It also accepts a single argument to fetch a metadata item:
994  
995 print $path->metadata('author'); # Arthur Dent
996  
997 You can also call it without arguments. The method returns a reference
998 to a hash array of metadata items.
999  
1000 my $meta = $path->metadata;
1001 print $meta->{ author }; # Arthur Dent
1002  
1003 =head1 STUB METHODS
1004  
1005 The following methods serve little or no purpose in the
1006 C<Badger::Filesystem::Path> base class. They are redefined by the
1007 C<Badger::Filesystem::Directory> and C<Badger::Filesystem::File> modules
1008 to do the right thing.
1009  
1010 =head2 is_file()
1011  
1012 This method always returns false in the C<Badger::Filesystem::Path> base
1013 class. The C<Badger::Filesystem::File> subclass redefines this to return
1014 true. NOTE: this may be changed to examine the filesystem and return true
1015 if the path references a file.
1016  
1017 =head2 is_directory() / is_dir()
1018  
1019 This method always returns false in the C<Badger::Filesystem::Path> base
1020 class. The C<Badger::Filesystem::Directory> subclass redefines this to return
1021 true. NOTE: this may be changed to examine the filesystem and return true
1022 if the path references a file.
1023  
1024 =head2 volume() / vol()
1025  
1026 Returns any volume defined as part of the path. This method does nothing in
1027 the C<Badger::Filesystem::Path> base class.
1028  
1029 =head2 directory() / dir()
1030  
1031 Returns the directory portion of a path. This method does nothing in the
1032 C<Badger::Filesystem::Path> base class.
1033  
1034 =head2 name()
1035  
1036 Returns the file name portion of a path. This method does nothing in the
1037 C<Badger::Filesystem::Path> base class.
1038  
1039 =head2 extension() / ext()
1040  
1041 Returns any file extension portion following the final C<.> in the path.
1042 This works in the C<Badger::Filesystem::Path> base class by looking at the
1043 full path.
1044  
1045 print Path('/foo/bar.txt')->extension; # txt
1046  
1047 =head2 basename() / base_name()
1048  
1049 Returns the filename I<without> the file extension following the final
1050 C<.> in the path. This works (for some definition of "works") in the
1051 C<Badger::Filesystem::Path> base class by looking at the path L<name()>,
1052 if defined, or the full C<path> if not. Note that this will produce
1053 unexpected results in some cases due to the fact that the base class
1054 does not define a value for L<name()>. e.g.
1055  
1056 print Path('/foo/bar.txt')->basename; # /foo/bar
1057  
1058 However, in most cases you would be using this through a
1059 L<Badger::Filesystem::File> subclass which will product the correct
1060 results.
1061  
1062 print File('/foo/bar.txt')->basename; # bar
1063  
1064 =head1 AUTHOR
1065  
1066 Andy Wardley L<http://wardley.org/>
1067  
1068 =head1 COPYRIGHT
1069  
1070 Copyright (C) 2005-2009 Andy Wardley. All rights reserved.
1071  
1072 =head1 ACKNOWLEDGEMENTS
1073  
1074 The C<Badger::Filesystem> modules are built around a number of existing
1075 Perl modules, including L<File::Spec>, L<File::Path>, L<Cwd>, L<IO::File>,
1076 L<IO::Dir> and draw heavily on ideas in L<Path::Class>.
1077  
1078 Please see the L<ACKNOWLEDGEMENTS|Badger::Filesystem/ACKNOWLEDGEMENTS>
1079 in L<Badger::Filesystem> for further information.
1080  
1081 =head1 SEE ALSO
1082  
1083 L<Badger::Filesystem>,
1084 L<Badger::Filesystem::File>,
1085 L<Badger::Filesystem::Directory>,
1086 L<Badger::Filesystem::Visitor>.
1087  
1088 =cut
1089  
1090 # Local Variables:
1091 # mode: Perl
1092 # perl-indent-level: 4
1093 # indent-tabs-mode: nil
1094 # End:
1095 #
1096 # vim: expandtab shiftwidth=4:
1097 # TextMate: should support split pane editing
« lib/Badger/Modules.pm ↑ Summary lib/Badger/Debug.pm »