CWB-CQP-More

 view release on metacpan or  search on metacpan

lib/CWB/CQP/More/Iterator.pm  view on Meta::CPAN

package CWB::CQP::More::Iterator;
$CWB::CQP::More::Iterator::VERSION = '0.08';
use warnings;
use strict;
use Data::Dumper;

sub new {
    my ($class, $cwb, $resultset, %ops) = @_;
    return undef unless $resultset && eval { $cwb->isa('CWB::CQP::More') };

    my $self = { name => $resultset, cwb => $cwb };

    $self->{pos}   = 0;
    $self->{crp}   = uc($ops{corpus}) || undef;
    $self->{size}  = $ops{size}       || 1;

    $self->{fname} = $self->{crp}?"$self->{crp}:$self->{name}" : $self->{name};
    $self->{limit} = $cwb->size($self->{fname}) || 0;

    return bless $self => $class;
}

sub reset {
    my $self = shift;
    $self->{pos} = 0;
}

sub increment {
    my $self = shift;
    my $current = $self->{size};
    $self->{size} = shift @_ if $_[0];
    return $current;
}

sub next {
    my ($self) = @_;
    my $cwb = $self->{cwb};
    if ($self->{pos} < $self->{limit}) {
        my @lines = $cwb->cat($self->{fname},
                              $self->{pos} => $self->{pos} + $self->{size} -1);
        $self->{pos} += $self->{size};

        if (scalar(@lines) > 1) {
            return @lines
        } else {
            return wantarray ? @lines : $lines[0];
        }

    } else {
        return undef
    }
}

sub peek {
    my ($self, $offset) = @_;
    my $cwb = $self->{cwb};

    $offset = $self->{pos} + $offset;
    if ($offset >= 0 && $offset < $self->{limit}) {
        my ($line) = $cwb->cat($self->{fname}, $offset => $offset);
        return $line;
    } else {
        return undef;
    }
}

sub backward {
    my ($self, $offset) = @_;
    return $self->forward(-$offset);
}

sub forward {
    my ($self, $offset) = @_;

    $offset = $self->{pos} + $offset;

    $offset = 0                  if $offset < 0;
    $offset = $self->{limit} - 1 if $offset > $self->{limit};

    $self->{pos} = $offset;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.591 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )