Mojo/File.pm

filter: Statementstmt 93.3% 154 / 165 Branchbran 78.7% 52 / 66 Conditioncond 69.0% 29 / 42 MC/DCmcdc 52.1% 12 / 23 Subroutinesub 96.3% 52 / 54 Podpod 100% 34 / 34 totaltotal 86.7% 333 / 384 CC 50 SCAR 18.3
CC
50
Cov
86%
download
30.0
_download_error
30.0
__ANON__
11.0
CRAP 6.2
« Mojolicious/Plugin/JSONConfig.pm ↑ Summary Mojolicious/Routes.pm »
1 package Mojo::File;
2 49 ▶use Mojo::Base -strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 49 ▶use overload '@{}' => sub { shift->to_array }, bool => sub {1}, '""' => sub { ${$_[0]} }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
4  
5 49 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 49 ▶use Cwd qw(getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 49 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 49 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 49 ▶use File::Copy qw(copy move);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 49 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 49 ▶use File::Spec::Functions qw(abs2rel canonpath catfile file_name_is_absolute rel2abs splitdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 49 ▶use File::stat ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 49 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 49 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 49 ▶use Mojo::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 49 ▶use Mojo::Util qw(decode encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 49 our @EXPORT_OK = ('curfile', 'path', 'tempdir', 'tempfile');
19  
20 65 ▶sub basename { File::Basename::basename ${shift()}, @_ }
Subroutine basename: called CC=1 SCAR=0.0
Pod documented
21  
22 416 ▶sub child { $_[0]->new(${shift()}, @_) }
Subroutine child: called CC=1 SCAR=0.0
Pod documented
23  
24 4 ▶sub chmod {
Subroutine chmod: called CC=2 SCAR=6.9
Pod documented
25 4 my ($self, $mode) = @_;
26 4 ▶ chmod $mode, $$self or croak qq{Can't chmod file "$$self": $!};
Branch: 1 branch
BranchTrueFalseTotal
CORE::chmod $mode, $$self or croak(qq[Can't chmod file "$$self": $!]) 3 1 4
27 3 return $self;
28 }
29  
30 2 ▶sub copy_to {
Subroutine copy_to: called CC=3 SCAR=11.1
Pod documented
31 2 my ($self, $to) = @_;
32 2 ▶ copy($$self, $to) or croak qq{Can't copy file "$$self" to "$to": $!};
Branch: 1 branch
BranchTrueFalseTotal
copy($$self, $to) or croak(qq[Can't copy file "$$self" to "$to": $!]) 2 0 2
33 2 ▶ return $self->new(-d $to ? ($to, File::Basename::basename $self) : $to);
Branch: 1 branch
BranchTrueFalseTotal
-d $to ? : 1 1 2
34 }
35  
36 138 ▶sub curfile { __PACKAGE__->new(Cwd::realpath((caller)[1])) }
Subroutine curfile: called CC=1 SCAR=0.0
Pod documented
37  
38 44 ▶sub dirname { $_[0]->new(scalar File::Basename::dirname ${$_[0]}) }
Subroutine dirname: called CC=1 SCAR=0.0
Pod documented
39  
40 0 ▶sub download {
Subroutine download: not called CC=5 SCAR=34.0
Pod documented
41 0 ▶ my ($self, $url, $options) = (shift, shift, shift // {});
Condition0%0/2
shift() // {}
l!l
00
MC/DC0%0/1
shift() // {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
42 my $ua = $options->{ua}
43 0 ▶ || do { require Mojo::UserAgent; Mojo::UserAgent->new(max_redirects => 10, max_response_size => 0) };
Condition0%0/3
$options->{'ua'} || do { require Mojo::UserAgent; 'Mojo::UserAgent'->new('max_redirects', 10, 'max_response_size', 0) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{'ua'} || do { require Mojo::UserAgent; 'Mojo::UserAgent'->new('max_redirects', 10, 'max_response_size', 0) }
$options->{'ua'} do { require Mojo::UserAgent; 'Mojo::UserAgent'->new('max_redirects', 10, 'max_response_size', 0) }
Truth table0%0/3
A || B
A= $options->{'ua'}B= do { require Mojo::UserAgent; 'Mojo::UserAgent'->new('max_redirects', 10, 'max_response_size', 0) }
ABresult
1X1
011
000
44 0 ▶ my $tx = _download_error($ua->transactor->download($ua->head($url => $options->{headers} // {}), $$self));
Condition0%0/2
$options->{"headers"} // {}
l!l
00
MC/DC0%0/1
$options->{"headers"} // {}
$options->{"headers"}
Truth table0%0/2
A
A= $options->{"headers"}
Aresult
11
00
45 0 ▶ return $tx ? !!_download_error($ua->start($tx)) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$tx ? : 0 0 0
46 }
47  
48 38 ▶sub extname { shift->basename =~ /.+\.([^.]+)$/ ? $1 : '' }
Subroutine extname: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->basename =~ /.+\.([^.]+)$/u ? : 27 11 38
49  
50 17 ▶sub is_abs { file_name_is_absolute ${shift()} }
Subroutine is_abs: called CC=1 SCAR=0.0
Pod documented
51  
52 31 ▶sub list {
Subroutine list: called CC=7 SCAR=19.5
Pod documented
53 31 ▶ my ($self, $options) = (shift, shift // {});
Condition100%2/2
shift() // {}
l!l
328
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
54  
55 31 ▶ return Mojo::Collection->new unless -d $$self;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $$self 2 29 31
56 29 ▶ opendir(my $dir, $$self) or croak qq{Can't open directory "$$self": $!};
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dir, $$self or croak(qq[Can't open directory "$$self": $!]) 29 0 29
57 29 ▶ my @files = grep { $_ ne '.' && $_ ne '..' } readdir $dir;
Condition100%3/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
2929192
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
58 29 ▶ @files = grep { !/^\./ } @files unless $options->{hidden};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"hidden"} 27 2 29
59 29 @files = map { catfile $$self, $_ } @files;
60 29 ▶ @files = grep { !-d } @files unless $options->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"dir"} 27 2 29
61  
62 29 return Mojo::Collection->new(map { $self->new($_) } sort @files);
63 }
64  
65 127 ▶sub list_tree {
Subroutine list_tree: called CC=3 SCAR=11.0
Pod documented
66 127 ▶ my ($self, $options) = (shift, shift // {});
Condition100%2/2
shift() // {}
l!l
27100
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
67 127 ▶ return Mojo::Collection->new unless -d $$self;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $$self 7 120 127
68  
69 120 my (@results, $walk);
70 381 ▶ $walk = sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
71 381 my ($path, $depth) = @_;
72 381 ▶ opendir my $dh, $path or return;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $path or return 381 0 381
73 381 ▶ my @names = sort grep { $_ ne '.' && $_ ne '..' } readdir $dh;
Condition100%3/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
3813811285
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
74 381 ▶ @names = grep { !/^\./ } @names unless $options->{hidden};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"hidden"} 355 26 381
75 381 for my $name (@names) {
76 1277 my $child = $self->new($path, $name);
77 1277 my $is_dir = -d $$child;
78 1277 ▶ push @results, $child if $options->{dir} || !$is_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"dir"} or not $is_dir 1026 251 1277
Condition100%3/3
$options->{"dir"} or not $is_dir
l!l&&r!l&&!r
66960251
MC/DC100%2/2
$options->{"dir"} or not $is_dir
$options->{"dir"} not $is_dir
Truth table100%3/3
A or not(B)
A= $options->{"dir"}B= not $is_dir
ABresult
1X1
011
000
79 __SUB__->($$child, $depth + 1)
80 1277 ▶ if $is_dir && !-l $$child && (!$options->{max_depth} || $depth + 1 < $options->{max_depth});
Branch: 1 branch
BranchTrueFalseTotal
if $is_dir and not -l $$child and !$options->{"max_depth"} || $depth + 1 < $options->{"max_depth"} 261 1016 1277
Condition100%9/9
$is_dir and not -l $$child and !$options->{"max_depth"} || $depth + 1 < $options->{"max_depth"}
!ll&&!rl&&r
100511261
$is_dir and not -l $$child
!ll&&!rl&&r
10014272
not $options->{"max_depth"} or $depth + 1 < $options->{"max_depth"}
l!l&&r!l&&!r
2501111
MC/DC100%4/4
$is_dir and not -l $$child and !$options->{"max_depth"} || $depth + 1 < $options->{"max_depth"}
$is_dir not -l $$child not $options->{"max_depth"} $depth + 1 < $options->{"max_depth"}
Truth table100%5/5
A and not(B) and not(C) or D
A= $is_dirB= not -l $$childC= not $options->{"max_depth"}D= $depth + 1 < $options->{"max_depth"}
ABCDresult
0XXX0
10XX0
11000
111X1
11011
81 }
82 120 };
83 120 $walk->($$self, 0);
84  
85 120 return Mojo::Collection->new(@results);
86 }
87  
88 1 ▶sub lstat { File::stat::lstat(${shift()}) }
Subroutine lstat: called CC=1 SCAR=0.0
Pod documented
89  
90 24 ▶sub make_path {
Subroutine make_path: called CC=1 SCAR=0.0
Pod documented
91 24 my $self = shift;
92 24 File::Path::make_path $$self, @_;
93 24 return $self;
94 }
95  
96 10 ▶sub move_to {
Subroutine move_to: called CC=3 SCAR=11.1
Pod documented
97 10 my ($self, $to) = @_;
98 10 ▶ move($$self, $to) or croak qq{Can't move file "$$self" to "$to": $!};
Branch: 1 branch
BranchTrueFalseTotal
move($$self, $to) or croak(qq[Can't move file "$$self" to "$to": $!]) 10 0 10
99 10 ▶ return $self->new(-d $to ? ($to, File::Basename::basename $self) : $to);
Branch: 1 branch
BranchTrueFalseTotal
-d $to ? : 1 9 10
100 }
101  
102 5685 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
103 5685 my $class = shift;
104 5685 ▶ croak 'Invalid path' if grep { !defined } @_;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @_ 2 5683 5685
105 5683 ▶ my $value = @_ == 1 ? $_[0] : @_ > 1 ? catfile @_ : canonpath getcwd;
Branch: 2 branches
BranchTrueFalseTotal
@_ == 1 ? : 2497 3186 5683
@_ > 1 ? : 3030 156 3186
106 5683 ▶ return bless \$value, ref $class || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
351521680
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
107 }
108  
109 89 ▶sub open {
Subroutine open: called CC=2 SCAR=6.9
Pod documented
110 89 my $self = shift;
111 89 my $handle = IO::File->new;
112 89 ▶ $handle->open($$self, @_) or croak qq{Can't open file "$$self": $!};
Branch: 1 branch
BranchTrueFalseTotal
$handle->open($$self, @_) or croak(qq[Can't open file "$$self": $!]) 88 1 89
113 88 return $handle;
114 }
115  
116 1588 ▶sub path { __PACKAGE__->new(@_) }
Subroutine path: called CC=1 SCAR=0.0
Subroutine f: called CC=1 SCAR=0.0
Pod documented
117  
118 341 ▶sub realpath { $_[0]->new(Cwd::realpath ${$_[0]}) }
Subroutine realpath: called CC=1 SCAR=0.0
Pod documented
119  
120 50 ▶sub remove {
Subroutine remove: called CC=3 SCAR=11.0
Pod documented
121 50 my ($self, $mode) = @_;
122 50 ▶ unlink $$self or croak qq{Can't remove file "$$self": $!} if -e $$self;
Branch: 1 branch
BranchTrueFalseTotal
if -e $$self 44 6 50
Condition80%4/5
-e $$self && (unlink($$self) || croak(qq[Can't remove file "$$self": $!]))
!ll
6440
unlink $$self or croak(qq[Can't remove file "$$self": $!])
l!l
431
MC/DC0%0/2
-e $$self && (unlink($$self) || croak(qq[Can't remove file "$$self": $!]))
-e $$self unlink $$self
Truth table66%2/3
A && B
A= -e $$selfB= unlink $$self
ABresult
0X0
100
111
123 49 return $self;
124 }
125  
126 2 ▶sub remove_tree {
Subroutine remove_tree: called CC=1 SCAR=0.0
Pod documented
127 2 my $self = shift;
128 2 File::Path::remove_tree $$self, @_;
129 2 return $self;
130 }
131  
132 133 ▶sub sibling {
Subroutine sibling: called CC=1 SCAR=0.0
Pod documented
133 133 my $self = shift;
134 133 return $self->new(scalar File::Basename::dirname($self), @_);
135 }
136  
137 92 ▶sub slurp {
Subroutine slurp: called CC=5 SCAR=16.1
Pod documented
138 92 my ($self, $encoding) = @_;
139  
140 92 ▶ CORE::open my $file, '<', $$self or croak qq{Can't open file "$$self": $!};
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $file, "<", $$self or croak(qq[Can't open file "$$self": $!]) 91 1 92
141 91 my $ret = my $content = '';
142 91 ▶ while ($ret = $file->sysread(my $buffer, 131072, 0)) { $content .= $buffer }
Branch: 1 branch
BranchTrueFalseTotal
if ($ret = $file->sysread(my $buffer, 131072, 0)) 91 91 182
143 91 ▶ croak qq{Can't read from file "$$self": $!} unless defined $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 0 91 91
144  
145 91 ▶ return $encoding ? decode($encoding, $content) : $content;
Branch: 1 branch
BranchTrueFalseTotal
$encoding ? : 2 89 91
146 }
147  
148 55 ▶sub spew {
Subroutine spew: called CC=5 SCAR=16.1
Pod documented
149 55 my ($self, $content, $encoding) = @_;
150 55 ▶ $content = encode($encoding, $content) if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 1 54 55
151 55 ▶ CORE::open my $file, '>', $$self or croak qq{Can't open file "$$self": $!};
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $file, ">", $$self or croak(qq[Can't open file "$$self": $!]) 54 1 55
152 54 ▶ ($file->syswrite($content) // -1) == length $content or croak qq{Can't write to file "$$self": $!};
Branch: 1 branch
BranchTrueFalseTotal
($file->syswrite($content) // -1) == length $content or croak(qq[Can't write to file "$$self": $!]) 52 1 53
Condition50%1/2
$file->syswrite($content) // -1
l!l
530
MC/DC0%0/1
$file->syswrite($content) // -1
$file->syswrite($content)
Truth table50%1/2
A
A= $file->syswrite($content)
Aresult
11
00
153 52 return $self;
154 }
155  
156 1 ▶sub spurt { shift->spew(join '', @_) }
Subroutine spurt: called CC=1 SCAR=0.0
Pod documented
157  
158 4 ▶sub stat { File::stat::stat(${shift()}) }
Subroutine stat: called CC=1 SCAR=0.0
Pod documented
159  
160 1 ▶sub tap { shift->Mojo::Base::tap(@_) }
Subroutine tap: called CC=1 SCAR=0.0
Pod documented
161  
162 38 ▶sub tempdir { __PACKAGE__->new(File::Temp->newdir(@_)) }
Subroutine tempdir: called CC=1 SCAR=0.0
Pod documented
163  
164 51 ▶sub tempfile { __PACKAGE__->new(File::Temp->new(@_)) }
Subroutine tempfile: called CC=1 SCAR=0.0
Pod documented
165  
166 100 ▶sub to_abs { $_[0]->new(rel2abs ${$_[0]}) }
Subroutine to_abs: called CC=1 SCAR=0.0
Pod documented
167  
168 1132 ▶sub to_array { [splitdir ${shift()}] }
Subroutine to_array: called CC=1 SCAR=0.0
Pod documented
169  
170 1035 ▶sub to_rel { $_[0]->new(abs2rel(${$_[0]}, $_[1])) }
Subroutine to_rel: called CC=1 SCAR=0.0
Pod documented
171  
172 1342 ▶sub to_string {"${$_[0]}"}
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
173  
174 4 ▶sub touch {
Subroutine touch: called CC=3 SCAR=11.0
Pod documented
175 4 my $self = shift;
176 4 ▶ $self->open('>') unless -e $$self;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $$self 2 2 4
177 4 ▶ utime undef, undef, $$self or croak qq{Can't touch file "$$self": $!};
Branch: 1 branch
BranchTrueFalseTotal
utime undef, undef, $$self or croak(qq[Can't touch file "$$self": $!]) 4 0 4
178 4 return $self;
179 }
180  
181 1 ▶sub with_roles { shift->Mojo::Base::with_roles(@_) }
Subroutine with_roles: called CC=1 SCAR=0.0
Pod documented
182  
183 0 ▶sub _download_error {
Subroutine _download_error: not called CC=5 SCAR=34.0
184 0 my $tx = shift;
185  
186 0 ▶ return $tx unless my $err = $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
unless my $err = $tx->error 0 0 0
187 0 ▶ return undef if $err->{message} eq 'Download complete' || $err->{message} eq 'Download incomplete';
Branch: 1 branch
BranchTrueFalseTotal
if $err->{"message"} eq "Download complete" or $err->{"message"} eq "Download incomplete" 0 0 0
Condition0%0/3
$err->{"message"} eq "Download complete" or $err->{"message"} eq "Download incomplete"
l!l&&r!l&&!r
000
MC/DC0%0/2
$err->{"message"} eq "Download complete" or $err->{"message"} eq "Download incomplete"
$err->{"message"} eq "Download complete" $err->{"message"} eq "Download incomplete"
Truth table0%0/3
A or B
A= $err->{"message"} eq "Download complete"B= $err->{"message"} eq "Download incomplete"
ABresult
1X1
011
000
188 0 ▶ croak "$err->{code} response: $err->{message}" if $err->{code};
Branch: 1 branch
BranchTrueFalseTotal
if $err->{"code"} 0 0 0
189 0 croak "Download error: $err->{message}";
190 }
191  
192 49 1;
193  
194 =encoding utf8
195  
196 =head1 NAME
197  
198 Mojo::File - File system paths
199  
200 =head1 SYNOPSIS
201  
202 use Mojo::File;
203  
204 # Portably deal with file system paths
205 my $path = Mojo::File->new('/home/sri/.vimrc');
206 say $path->slurp;
207 say $path->dirname;
208 say $path->basename;
209 say $path->extname;
210 say $path->sibling('.bashrc');
211  
212 # Use the alternative constructor
213 use Mojo::File qw(path);
214 my $path = path('/tmp/foo/bar')->make_path;
215 $path->child('test.txt')->spew('Hello Mojo!');
216  
217 =head1 DESCRIPTION
218  
219 L<Mojo::File> is a scalar-based container for file system paths that provides a friendly API for dealing with different
220 operating systems.
221  
222 # Access scalar directly to manipulate path
223 my $path = Mojo::File->new('/home/sri/test');
224 $$path .= '.txt';
225  
226 =head1 FUNCTIONS
227  
228 L<Mojo::File> implements the following functions, which can be imported individually.
229  
230 =head2 curfile
231  
232 my $path = curfile;
233  
234 Construct a new scalar-based L<Mojo::File> object for the absolute path to the current source file.
235  
236 =head2 path
237  
238 my $path = path;
239 my $path = path('/home/sri/.vimrc');
240 my $path = path('/home', 'sri', '.vimrc');
241 my $path = path(File::Temp->newdir);
242  
243 Construct a new scalar-based L<Mojo::File> object, defaults to using the current working directory.
244  
245 # "foo/bar/baz.txt" (on UNIX)
246 path('foo', 'bar', 'baz.txt');
247  
248 =head2 tempdir
249  
250 my $path = tempdir;
251 my $path = tempdir('tempXXXXX');
252  
253 Construct a new scalar-based L<Mojo::File> object for a temporary directory with L<File::Temp>.
254  
255 # Longer version
256 my $path = path(File::Temp->newdir('tempXXXXX'));
257  
258 =head2 tempfile
259  
260 my $path = tempfile;
261 my $path = tempfile(DIR => '/tmp');
262  
263 Construct a new scalar-based L<Mojo::File> object for a temporary file with L<File::Temp>.
264  
265 # Longer version
266 my $path = path(File::Temp->new(DIR => '/tmp'));
267  
268 =head1 METHODS
269  
270 L<Mojo::File> implements the following methods.
271  
272 =head2 basename
273  
274 my $name = $path->basename;
275 my $name = $path->basename('.txt');
276  
277 Return the last level of the path with L<File::Basename>.
278  
279 # ".vimrc" (on UNIX)
280 path('/home/sri/.vimrc')->basename;
281  
282 # "test" (on UNIX)
283 path('/home/sri/test.txt')->basename('.txt');
284  
285 =head2 child
286  
287 my $child = $path->child('.vimrc');
288  
289 Return a new L<Mojo::File> object relative to the path.
290  
291 # "/home/sri/.vimrc" (on UNIX)
292 path('/home')->child('sri', '.vimrc');
293  
294 =head2 chmod
295  
296 $path = $path->chmod(0644);
297  
298 Change file permissions.
299  
300 =head2 copy_to
301  
302 my $destination = $path->copy_to('/home/sri');
303 my $destination = $path->copy_to('/home/sri/.vimrc.backup');
304  
305 Copy file with L<File::Copy> and return the destination as a L<Mojo::File> object.
306  
307 =head2 dirname
308  
309 my $name = $path->dirname;
310  
311 Return all but the last level of the path with L<File::Basename> as a L<Mojo::File> object.
312  
313 # "/home/sri" (on UNIX)
314 path('/home/sri/.vimrc')->dirname;
315  
316 =head2 download
317  
318 my $bool = $path->download('https://example.com/test.tar.gz');
319 my $bool = $path->download('https://example.com/test.tar.gz', {headers => {Accept => '*/*'}});
320 my $bool = $path->download('https://example.com/test.tar.gz', {ua => Mojo::UserAgent->new});
321  
322 Download file from URL, returns true once the file has been downloaded completely. Incomplete downloads are resumed.
323 Follows C<10> redirects by default and does not limit the size of the response, which will be streamed memory
324 efficiently. Note that this method is B<EXPERIMENTAL> and might change without warning!
325  
326 =head2 extname
327  
328 my $ext = $path->extname;
329  
330 Return file extension of the path.
331  
332 # "js"
333 path('/home/sri/test.js')->extname;
334  
335 =head2 is_abs
336  
337 my $bool = $path->is_abs;
338  
339 Check if the path is absolute.
340  
341 # True (on UNIX)
342 path('/home/sri/.vimrc')->is_abs;
343  
344 # False (on UNIX)
345 path('.vimrc')->is_abs;
346  
347 =head2 list
348  
349 my $collection = $path->list;
350 my $collection = $path->list({hidden => 1});
351  
352 List all files in the directory and return a L<Mojo::Collection> object containing the results as L<Mojo::File>
353 objects. The list does not include C<.> and C<..>.
354  
355 # List files
356 say for path('/home/sri/myapp')->list->each;
357  
358 These options are currently available:
359  
360 =over 2
361  
362 =item dir
363  
364 dir => 1
365  
366 Include directories.
367  
368 =item hidden
369  
370 hidden => 1
371  
372 Include hidden files.
373  
374 =back
375  
376 =head2 list_tree
377  
378 my $collection = $path->list_tree;
379 my $collection = $path->list_tree({hidden => 1});
380  
381 List all files recursively in the directory and return a L<Mojo::Collection> object containing the results as
382 L<Mojo::File> objects. The list does not include C<.> and C<..>, and symbolic links to directories are not followed.
383  
384 # List all templates
385 say for path('/home/sri/myapp/templates')->list_tree->each;
386  
387 These options are currently available:
388  
389 =over 2
390  
391 =item dir
392  
393 dir => 1
394  
395 Include directories.
396  
397 =item hidden
398  
399 hidden => 1
400  
401 Include hidden files and directories.
402  
403 =item max_depth
404  
405 max_depth => 3
406  
407 Maximum number of levels to descend when searching for files.
408  
409 =back
410  
411 =head2 lstat
412  
413 my $stat = $path->lstat;
414  
415 Return a L<File::stat> object for the symlink.
416  
417 # Get symlink size
418 say path('/usr/sbin/sendmail')->lstat->size;
419  
420 # Get symlink modification time
421 say path('/usr/sbin/sendmail')->lstat->mtime;
422  
423 =head2 make_path
424  
425 $path = $path->make_path;
426 $path = $path->make_path({mode => 0711});
427  
428 Create the directories if they don't already exist, any additional arguments are passed through to L<File::Path>.
429  
430 =head2 move_to
431  
432 my $destination = $path->move_to('/home/sri');
433 my $destination = $path->move_to('/home/sri/.vimrc.backup');
434  
435 Move file with L<File::Copy> and return the destination as a L<Mojo::File> object.
436  
437 =head2 new
438  
439 my $path = Mojo::File->new;
440 my $path = Mojo::File->new('/home/sri/.vimrc');
441 my $path = Mojo::File->new('/home', 'sri', '.vimrc');
442 my $path = Mojo::File->new(File::Temp->new);
443 my $path = Mojo::File->new(File::Temp->newdir);
444  
445 Construct a new L<Mojo::File> object, defaults to using the current working directory.
446  
447 # "foo/bar/baz.txt" (on UNIX)
448 Mojo::File->new('foo', 'bar', 'baz.txt');
449  
450 =head2 open
451  
452 my $handle = $path->open('+<');
453 my $handle = $path->open('r+');
454 my $handle = $path->open(O_RDWR);
455 my $handle = $path->open('<:encoding(UTF-8)');
456  
457 Open file with L<IO::File>.
458  
459 # Combine "fcntl.h" constants
460 use Fcntl qw(O_CREAT O_EXCL O_RDWR);
461 my $handle = path('/home/sri/test.pl')->open(O_RDWR | O_CREAT | O_EXCL);
462  
463 =head2 realpath
464  
465 my $realpath = $path->realpath;
466  
467 Resolve the path with L<Cwd> and return the result as a L<Mojo::File> object.
468  
469 =head2 remove
470  
471 $path = $path->remove;
472  
473 Delete file.
474  
475 =head2 remove_tree
476  
477 $path = $path->remove_tree;
478 $path = $path->remove_tree({keep_root => 1});
479  
480 Delete this directory and any files and subdirectories it may contain, any additional arguments are passed through to
481 L<File::Path>.
482  
483 =head2 sibling
484  
485 my $sibling = $path->sibling('.vimrc');
486  
487 Return a new L<Mojo::File> object relative to the directory part of the path.
488  
489 # "/home/sri/.vimrc" (on UNIX)
490 path('/home/sri/.bashrc')->sibling('.vimrc');
491  
492 # "/home/sri/.ssh/known_hosts" (on UNIX)
493 path('/home/sri/.bashrc')->sibling('.ssh', 'known_hosts');
494  
495 =head2 slurp
496  
497 my $bytes = $path->slurp;
498 my $chars = $path->slurp('UTF-8');
499  
500 Read all data at once from the file. If an encoding is provided, an attempt will be made to decode the content.
501  
502 =head2 spew
503  
504 $path = $path->spew($bytes);
505 $path = $path->spew($chars, 'UTF-8');
506  
507 Write all data at once to the file. If an encoding is provided, an attempt to encode the content will be made prior to
508 writing.
509  
510 =head2 spurt
511  
512 $path = $path->spurt(@bytes);
513  
514 Alias for L</"spew"> that writes multiple chunks of bytes.
515  
516 =head2 stat
517  
518 my $stat = $path->stat;
519  
520 Return a L<File::stat> object for the path.
521  
522 # Get file size
523 say path('/home/sri/.bashrc')->stat->size;
524  
525 # Get file modification time
526 say path('/home/sri/.bashrc')->stat->mtime;
527  
528 =head2 tap
529  
530 $path = $path->tap(sub {...});
531  
532 Alias for L<Mojo::Base/"tap">.
533  
534 =head2 to_abs
535  
536 my $absolute = $path->to_abs;
537  
538 Return absolute path as a L<Mojo::File> object, the path does not need to exist on the file system.
539  
540 =head2 to_array
541  
542 my $parts = $path->to_array;
543  
544 Split the path on directory separators.
545  
546 # "home:sri:.vimrc" (on UNIX)
547 join ':', @{path('/home/sri/.vimrc')->to_array};
548  
549 =head2 to_rel
550  
551 my $relative = $path->to_rel('/some/base/path');
552  
553 Return a relative path from the original path to the destination path as a L<Mojo::File> object.
554  
555 # "sri/.vimrc" (on UNIX)
556 path('/home/sri/.vimrc')->to_rel('/home');
557  
558 =head2 to_string
559  
560 my $str = $path->to_string;
561  
562 Stringify the path.
563  
564 =head2 touch
565  
566 $path = $path->touch;
567  
568 Create file if it does not exist or change the modification and access time to the current time.
569  
570 # Safely read file
571 say path('.bashrc')->touch->slurp;
572  
573 =head2 with_roles
574  
575 my $new_class = Mojo::File->with_roles('Mojo::File::Role::One');
576 my $new_class = Mojo::File->with_roles('+One', '+Two');
577 $path = $path->with_roles('+One', '+Two');
578  
579 Alias for L<Mojo::Base/"with_roles">.
580  
581 =head1 OPERATORS
582  
583 L<Mojo::File> overloads the following operators.
584  
585 =head2 array
586  
587 my @parts = @$path;
588  
589 Alias for L</"to_array">.
590  
591 =head2 bool
592  
593 my $bool = !!$path;
594  
595 Always true.
596  
597 =head2 stringify
598  
599 my $str = "$path";
600  
601 Alias for L</"to_string">.
602  
603 =head1 SEE ALSO
604  
605 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
606  
607 =cut
« Mojolicious/Plugin/JSONConfig.pm ↑ Summary Mojolicious/Routes.pm »