Paulo Custodio > Iterator-Simple-Lookahead-0.05 > Iterator::Simple::Lookahead

Download:
Iterator-Simple-Lookahead-0.05.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Module Version: 0.05   Source   Latest Release: Iterator-Simple-Lookahead-0.06

NAME ^

Iterator::Simple::Lookahead - Simple iterator with lookahead and unget

SYNOPSIS ^

  use Iterator::Simple::Lookahead;
  my $iter = Iterator::Simple::Lookahead->new( sub {}, @values );
  my $first = $iter->peek();
  my $second = $iter->peek(1);
  my $next = $iter->next; # or $iter->()
  $iter->unget( sub {}, @values );

DESCRIPTION ^

This module encapsulates an iterator function. An iterator function returns the next element in the stream on each call, and returns undef on end of input.

The iterator can return a code reference - this new iterator is inserted at the head of the queue.

The object allows the user to peek() the Nth element of the stream without consuming it, or to get it and remove from the stream.

A list of items can also be pushed back to the stream by unget(), to be retrieved in the subsequent next() calls. The next item can also be retrieved by calling $iter->().

The input list to the constructor and to unget() contains items to be retrieved on each next() call, or code references to be called to extract the next item from the stream.

Other types of input can be converted to a code reference by iter() of Iterator::Simple.

This module is built on top of Iterator::Simple and returns iterators that are compatible with the former, i.e. an object of type Iterator::Simple::Lookahead can be used as an input to iter().

FUNCTIONS ^

new

Creates a new object ready to retrieve elements from the given input list. The list contains either values to be returned in a subsequent next() call, or code references to be called to extract the next item from the stream.

Other types of input can be converted to a code reference by iter() of Iterator::Simple.

peek

Retrieves the Nth-element at the head of the stream, but keeps it in the stream to be retrieved by next().

Calls the iterator function to compute the items up to the Nth element, returns undef if the stream is exhausted before reaching N.

As a special case, peek() retrieves the element at the head of the stream, or undef if the stream is empty.

next

Retrieves the element at the head of the stream and removes it from the stream.

Returns undef if the stream is empty

unget

Pushes back a list of values and/or iterators to the stream, that will be retrieved on the subsequent calls to next().

Can be called from within an iterator, to insert values that will be returned before the current call, e.g. calling from the iterator:

  $stream->unget(1..3); return 4;

will result in the values 4,1,2,3 being returned from the stream.

EXPORT ^

None.

AUTHOR ^

Paulo Custodio, <pscust at cpan.org>

BUGS and FEEDBACK ^

Please report any bugs or feature requests through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Iterator-Simple-Lookahead.

ACKNOWLEDGEMENTS ^

Inspired in HOP::Stream and Iterator::Simple.

COPYRIGHT AND LICENSE ^

Copyright (C) 2013 by Paulo Custodio

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.16.1 or, at your option, any later version of Perl 5 you may have available.

syntax highlighting: