Sidef

 view release on metacpan or  search on metacpan

lib/Sidef/Types/Glob/Dir.pm  view on Meta::CPAN

package Sidef::Types::Glob::Dir {

    use utf8;
    use 5.016;

    use parent qw(
      Sidef::Types::Glob::File
    );

    require Encode;
    require File::Spec;

    sub new {
        my (undef, $dir) = @_;
        if (@_ > 2) {
            shift(@_);
            $dir = File::Spec->catdir(map { "$_" } @_);
        }
        elsif (ref($dir)) {
            return $dir->to_dir;
        }
        bless \$dir, __PACKAGE__;
    }

    *call = \&new;

    sub get_value { ${$_[0]} }
    sub to_dir    { $_[0] }

    sub root {
        __PACKAGE__->new(File::Spec->rootdir);
    }

    sub home {
        my ($self) = @_;

        my $home = $ENV{HOME} || $ENV{LOGDIR};

        if (not $home and $^O ne 'MSWin32') {
            $home = (getpwuid($<))[7];
        }

        $home ? __PACKAGE__->new($home) : do {
            state $x = require File::HomeDir;
            __PACKAGE__->new(File::HomeDir->my_home);
        };
    }

    sub tmp {
        __PACKAGE__->new(File::Spec->tmpdir);
    }

    *temp = \&tmp;

    sub mktemp {
        my ($self, %opts) = @_;
        state $x = require File::Temp;
        __PACKAGE__->new(File::Temp::tempdir(CLEANUP => 1, %opts));
    }

    *make_tmp  = \&mktemp;
    *make_temp = \&mktemp;

    sub find {
        my ($self, $arg) = @_;
        state $x = require File::Find;

        my @files;
        my $ref      = ref($arg // '');
        my $is_block = $ref eq 'Sidef::Types::Block::Block';

        File::Find::find(
            sub {
                my $file = Encode::decode_utf8($File::Find::name);

                if (-d $file) {
                    $file = __PACKAGE__->new($file);
                }
                else {
                    $file = Sidef::Types::Glob::File->new($file);
                }

                $is_block ? $arg->run($file) : push(@files, $file);
            },
            $$self
        );

        $is_block ? $self : Sidef::Types::Array::Array->new(\@files);
    }

    *browse = \&find;

    sub cwd {
        state $x = require Cwd;
        __PACKAGE__->new(Encode::decode_utf8(Cwd::getcwd()));
    }

    sub pwd {
        __PACKAGE__->new(File::Spec->curdir);
    }



( run in 0.489 second using v1.01-cache-2.11-cpan-39bf76dae61 )