NAME

Asm::Preproc::Stream - Object to encapsulate an iterator that is able to unget

SYNOPSIS

  use Asm::Preproc::Stream;
  my $stream = Asm::Preproc::Stream->new(@input)
  my $head = $stream->head;
  my $next = $stream->get;
  $stream->unget(@items);
  my $it = $stream->iterator; my $next = $it->();

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 head 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, to be retrieved in the subsequent get() calls.

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

FUNCTIONS

new

Creates a new object ready to retrieve elements from the given input list.

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

Returns undef if the stream is empty.

get

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 get().

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

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

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

iterator

Return an iterator function that returns the next stream element on each call.

ACKNOWLEDGEMENTS

Inspired in HOP::Stream.

BUGS, FEEDBACK, AUTHOR, LICENCE and COPYRIGHT

See Asm::Preproc.