Asm::Preproc::Stream - Object to encapsulate an iterator that is able to unget
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->();
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.
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.
Retrieves the element at the head of the stream and removes it from the stream.
Returns undef if the stream is empty
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.
Return an iterator function that returns the next stream element on each call.
Inspired in HOP::Stream.