1 package TAP::Parser::IteratorFactory;
4 use vars qw($VERSION @ISA);
7 use TAP::Parser::Iterator::Array ();
8 use TAP::Parser::Iterator::Stream ();
9 use TAP::Parser::Iterator::Process ();
11 @ISA = qw(TAP::Object);
15 TAP::Parser::IteratorFactory - Internal TAP::Parser Iterator
27 use TAP::Parser::IteratorFactory;
28 my $factory = TAP::Parser::IteratorFactory->new;
29 my $iter = $factory->make_iterator(\*TEST);
30 my $iter = $factory->make_iterator(\@array);
31 my $iter = $factory->make_iterator(\%hash);
33 my $line = $iter->next;
37 This is a factory class for simple iterator wrappers for arrays, filehandles,
38 and hashes. Unless you're subclassing, you probably won't need to use this
47 Creates a new factory class.
48 I<Note:> You currently don't need to instantiate a factory in order to use it.
50 =head3 C<make_iterator>
52 Create an iterator. The type of iterator created depends on the arguments to
55 my $iter = TAP::Parser::Iterator->make_iterator( $filehandle );
57 Creates a I<stream> iterator (see L</make_stream_iterator>).
59 my $iter = TAP::Parser::Iterator->make_iterator( $array_reference );
61 Creates an I<array> iterator (see L</make_array_iterator>).
63 my $iter = TAP::Parser::Iterator->make_iterator( $hash_reference );
65 Creates a I<process> iterator (see L</make_process_iterator>).
70 my ( $proto, $thing ) = @_;
73 if ( $ref eq 'GLOB' || $ref eq 'IO::Handle' ) {
74 return $proto->make_stream_iterator($thing);
76 elsif ( $ref eq 'ARRAY' ) {
77 return $proto->make_array_iterator($thing);
79 elsif ( $ref eq 'HASH' ) {
80 return $proto->make_process_iterator($thing);
83 die "Can't iterate with a $ref";
88 =head3 C<make_stream_iterator>
90 Make a new stream iterator and return it. Passes through any arguments given.
91 Defaults to a L<TAP::Parser::Iterator::Stream>.
93 =head3 C<make_array_iterator>
95 Make a new array iterator and return it. Passes through any arguments given.
96 Defaults to a L<TAP::Parser::Iterator::Array>.
98 =head3 C<make_process_iterator>
100 Make a new process iterator and return it. Passes through any arguments given.
101 Defaults to a L<TAP::Parser::Iterator::Process>.
105 sub make_stream_iterator {
107 TAP::Parser::Iterator::Stream->new(@_);
110 sub make_array_iterator {
112 TAP::Parser::Iterator::Array->new(@_);
115 sub make_process_iterator {
117 TAP::Parser::Iterator::Process->new(@_);
125 Please see L<TAP::Parser/SUBCLASSING> for a subclassing overview.
127 There are a few things to bear in mind when creating your own
134 The factory itself is never instantiated (this I<may> change in the future).
135 This means that C<_initialize> is never called.
141 package MyIteratorFactory;
146 use MyStreamIterator;
147 use TAP::Parser::IteratorFactory;
149 @ISA = qw( TAP::Parser::IteratorFactory );
151 # override stream iterator
152 sub make_stream_iterator {
154 MyStreamIterator->new(@_);
161 Originally ripped off from L<Test::Harness>.
167 L<TAP::Parser::Iterator>,
168 L<TAP::Parser::Iterator::Array>,
169 L<TAP::Parser::Iterator::Stream>,
170 L<TAP::Parser::Iterator::Process>,