| M3-ServerView documentation | Contained in the M3-ServerView distribution. |
M3::ServerView::ResultSet - Contents returned by a view
Instances of this class functions as an iterator for iterating over results returned when searching a view.
Creates a new result set with the contents of the array reference ARRAY.
Returns a list of all items in the result set.
Returns the number of items in the result set.
Returns the first item in the result set or undef if the result set is empty.
Returns the next item in the result set or undef if there's no more items.
Resets the result set to start from the beginning again.
| M3-ServerView documentation | Contained in the M3-ServerView distribution. |
package M3::ServerView::ResultSet; use strict; use warnings; use Carp qw(croak); use Scalar::Util qw(refaddr); sub new { my ($pkg, $records) = @_; $records = [] unless defined $records; croak "Not an array reference" unless ref $records eq "ARRAY"; my $self = bless $records, $pkg; $self->reset(); return $self; } { my %Position; sub reset { my ($self) = @_; $Position{refaddr $self} = 0; } sub next { my ($self) = @_; return if $Position{refaddr $self} >= @$self; return $self->[$Position{refaddr $self}++]; } } sub all { my ($self) = @_; return @$self; } sub first { my ($self) = @_; return $self->[0]; } sub count { my ($self) = @_; return scalar @$self; } 1; __END__