Sidef

 view release on metacpan or  search on metacpan

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

            chomp($line);
            $code->run(Sidef::Types::String::String->new($line));
        }

        $self;
    }

    *each_line = \&each;

    sub each_char {
        my ($self, $code) = @_;

        while (defined(my $char = CORE::getc($self->{fh}))) {
            $code->run(Sidef::Types::String::String->new($char));
        }

        $self;
    }

    sub eof {
        my ($self) = @_;
        CORE::eof($self->{fh})
          ? (Sidef::Types::Bool::Bool::TRUE)
          : (Sidef::Types::Bool::Bool::FALSE);
    }

    sub tell {
        my ($self) = @_;
        Sidef::Types::Number::Number::_set_int(CORE::tell($self->{fh}));
    }

    sub rewind {
        my ($self) = @_;
        CORE::seek($self->{fh}, 0, 0)
          ? (Sidef::Types::Bool::Bool::TRUE)
          : (Sidef::Types::Bool::Bool::FALSE);
    }

    sub seek {
        my ($self, $pos, $whence) = @_;
        CORE::seek($self->{fh}, $pos, $whence)
          ? (Sidef::Types::Bool::Bool::TRUE)
          : (Sidef::Types::Bool::Bool::FALSE);
    }

    sub sysseek {
        my ($self, $pos, $whence) = @_;
        CORE::sysseek($self->{fh}, $pos, $whence)
          ? (Sidef::Types::Bool::Bool::TRUE)
          : (Sidef::Types::Bool::Bool::FALSE);
    }

    sub fileno {
        my ($self) = @_;
        Sidef::Types::Number::Number::_set_int(CORE::fileno($self->{fh}));
    }

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

        state $x = require Fcntl;
        $self->flock(&Fcntl::LOCK_EX);
    }

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

        state $x = require Fcntl;
        $self->flock(&Fcntl::LOCK_UN);
    }

    sub flock {
        my ($self, $mode) = @_;
        CORE::flock($self->{fh}, $mode)
          ? (Sidef::Types::Bool::Bool::TRUE)
          : (Sidef::Types::Bool::Bool::FALSE);
    }

    sub close {
        my ($self) = @_;
        CORE::close($self->{fh})
          ? (Sidef::Types::Bool::Bool::TRUE)
          : (Sidef::Types::Bool::Bool::FALSE);
    }

    sub stat {
        my ($self) = @_;
        Sidef::Types::Glob::Stat->stat($self->{fh}, $self);
    }

    sub lstat {
        my ($self) = @_;
        Sidef::Types::Glob::Stat->lstat($self->{fh}, $self);
    }

    sub truncate {
        my ($self, $length) = @_;
        CORE::truncate($self->{fh}, $length // 0)
          ? (Sidef::Types::Bool::Bool::TRUE)
          : (Sidef::Types::Bool::Bool::FALSE);
    }

    sub read_to {
        my ($self, $var_ref) = @_;

        my $line = CORE::readline($self->{fh});

        if (defined($line)) {
            chomp($line);
            $$var_ref = Sidef::Types::String::String->new($line);
        }
        else {
            undef $$var_ref;
        }

        $self;
    }

    sub write_from {
        my ($self, @args) = @_;
        CORE::print {$self->{fh}} @args;
        $self;
    }

    sub copy {
        my ($self, $fh) = @_;

        if (ref($fh) ne __PACKAGE__) {
            return;
        }

        state $x = require File::Copy;
        File::Copy::copy($self->{fh}, $fh->{fh})
          ? (Sidef::Types::Bool::Bool::TRUE)
          : (Sidef::Types::Bool::Bool::FALSE);
    }

    *cp = \©

    {
        no strict 'refs';
        *{__PACKAGE__ . '::' . '>>'} = \&read_to;
        *{__PACKAGE__ . '::' . '»'}  = \&read_to;
        *{__PACKAGE__ . '::' . '<<'} = \&write_from;
        *{__PACKAGE__ . '::' . '«'}  = \&write_from;
    }

};

1



( run in 3.004 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )