App-Pod
view release on metacpan or search on metacpan
t/cpan/Mojo2/File.pm view on Meta::CPAN
package Mojo2::File;
use Mojo::Base -strict;
use overload
'@{}' => sub { shift->to_array },
bool => sub { 1 },
'""' => sub { ${ $_[0] } },
fallback => 1;
use Carp qw(croak);
use Cwd qw(getcwd);
use Exporter qw(import);
use File::Basename ();
use File::Copy qw(copy move);
use File::Find qw(find);
use File::Path ();
use File::Spec::Functions
qw(abs2rel canonpath catfile file_name_is_absolute rel2abs splitdir);
use File::stat ();
use File::Temp ();
use IO::File ();
use Mojo::Collection;
our @EXPORT_OK = ( 'curfile', 'path', 'tempdir', 'tempfile' );
sub basename { File::Basename::basename ${ shift() }, @_ }
sub child { $_[0]->new( ${ shift() }, @_ ) }
sub chmod {
my ( $self, $mode ) = @_;
chmod $mode, $$self or croak qq{Can't chmod file "$$self": $!};
return $self;
}
sub copy_to {
my ( $self, $to ) = @_;
copy( $$self, $to ) or croak qq{Can't copy file "$$self" to "$to": $!};
return $self->new( -d $to ? ( $to, File::Basename::basename $self) : $to );
}
sub curfile { __PACKAGE__->new( Cwd::realpath( ( caller )[1] ) ) }
sub dirname { $_[0]->new( scalar File::Basename::dirname ${ $_[0] } ) }
sub extname { shift->basename =~ /.+\.([^.]+)$/ ? $1 : '' }
sub is_abs { file_name_is_absolute ${ shift() } }
sub list {
my ( $self, $options ) = ( shift, shift // {} );
return Mojo::Collection->new unless -d $$self;
opendir( my $dir, $$self ) or croak qq{Can't open directory "$$self": $!};
my @files = grep { $_ ne '.' && $_ ne '..' } readdir $dir;
@files = grep { !/^\./ } @files unless $options->{hidden};
@files = map { catfile $$self, $_ } @files;
@files = grep { !-d } @files unless $options->{dir};
return Mojo::Collection->new( map { $self->new( $_ ) } sort @files );
}
sub list_tree {
my ( $self, $options ) = ( shift, shift // {} );
# This may break in the future, but is worth it for performance
local $File::Find::skip_pattern = qr/^\./ unless $options->{hidden};
# The File::Find documentation lies, this is needed for CIFS
local $File::Find::dont_use_nlink = 1 if $options->{dont_use_nlink};
my %all;
my $wanted = sub {
if ( $options->{max_depth} ) {
( my $rel = $File::Find::name ) =~ s!^\Q$$self\E/?!!;
$File::Find::prune = 1 if splitdir( $rel ) >= $options->{max_depth};
}
$all{$File::Find::name}++ if $options->{dir} || !-d $File::Find::name;
};
find { wanted => $wanted, no_chdir => 1 }, $$self if -d $$self;
delete $all{$$self};
return Mojo::Collection->new( map { $self->new( canonpath $_) }
sort keys %all );
}
sub lstat { File::stat::lstat( ${ shift() } ) }
sub make_path {
my $self = shift;
File::Path::make_path $$self, @_;
return $self;
t/cpan/Mojo2/File.pm view on Meta::CPAN
Construct a new scalar-based L<Mojo::File> object for the absolute path to the current source file.
=head2 path
my $path = path;
my $path = path('/home/sri/.vimrc');
my $path = path('/home', 'sri', '.vimrc');
my $path = path(File::Temp->newdir);
Construct a new scalar-based L<Mojo::File> object, defaults to using the current working directory.
# "foo/bar/baz.txt" (on UNIX)
path('foo', 'bar', 'baz.txt');
=head2 tempdir
my $path = tempdir;
my $path = tempdir('tempXXXXX');
Construct a new scalar-based L<Mojo::File> object for a temporary directory with L<File::Temp>.
# Longer version
my $path = path(File::Temp->newdir('tempXXXXX'));
=head2 tempfile
my $path = tempfile;
my $path = tempfile(DIR => '/tmp');
Construct a new scalar-based L<Mojo::File> object for a temporary file with L<File::Temp>.
# Longer version
my $path = path(File::Temp->new(DIR => '/tmp'));
=head1 METHODS
L<Mojo::File> implements the following methods.
=head2 basename
my $name = $path->basename;
my $name = $path->basename('.txt');
Return the last level of the path with L<File::Basename>.
# ".vimrc" (on UNIX)
path('/home/sri/.vimrc')->basename;
# "test" (on UNIX)
path('/home/sri/test.txt')->basename('.txt');
=head2 child
my $child = $path->child('.vimrc');
Return a new L<Mojo::File> object relative to the path.
# "/home/sri/.vimrc" (on UNIX)
path('/home')->child('sri', '.vimrc');
=head2 chmod
$path = $path->chmod(0644);
Change file permissions.
=head2 copy_to
my $destination = $path->copy_to('/home/sri');
my $destination = $path->copy_to('/home/sri/.vimrc.backup');
Copy file with L<File::Copy> and return the destination as a L<Mojo::File> object.
=head2 dirname
my $name = $path->dirname;
Return all but the last level of the path with L<File::Basename> as a L<Mojo::File> object.
# "/home/sri" (on UNIX)
path('/home/sri/.vimrc')->dirname;
=head2 extname
my $ext = $path->extname;
Return file extension of the path.
# "js"
path('/home/sri/test.js')->extname;
=head2 is_abs
my $bool = $path->is_abs;
Check if the path is absolute.
# True (on UNIX)
path('/home/sri/.vimrc')->is_abs;
# False (on UNIX)
path('.vimrc')->is_abs;
=head2 list
my $collection = $path->list;
my $collection = $path->list({hidden => 1});
List all files in the directory and return a L<Mojo::Collection> object containing the results as L<Mojo::File>
objects. The list does not include C<.> and C<..>.
# List files
say for path('/home/sri/myapp')->list->each;
These options are currently available:
=over 2
=item dir
dir => 1
Include directories.
( run in 0.935 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )