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";
87 =head3 C<make_stream_iterator>
89 Make a new stream iterator and return it. Passes through any arguments given.
90 Defaults to a L<TAP::Parser::Iterator::Stream>.
92 =head3 C<make_array_iterator>
94 Make a new array iterator and return it. Passes through any arguments given.
95 Defaults to a L<TAP::Parser::Iterator::Array>.
97 =head3 C<make_process_iterator>
99 Make a new process iterator and return it. Passes through any arguments given.
100 Defaults to a L<TAP::Parser::Iterator::Process>.
104 sub make_stream_iterator {
106 TAP::Parser::Iterator::Stream->new(@_);
109 sub make_array_iterator {
111 TAP::Parser::Iterator::Array->new(@_);
114 sub make_process_iterator {
116 TAP::Parser::Iterator::Process->new(@_);
123 Please see L<TAP::Parser/SUBCLASSING> for a subclassing overview.
125 There are a few things to bear in mind when creating your own
132 The factory itself is never instantiated (this I<may> change in the future).
133 This means that C<_initialize> is never called.
139 package MyIteratorFactory;
144 use MyStreamIterator;
145 use TAP::Parser::IteratorFactory;
147 @ISA = qw( TAP::Parser::IteratorFactory );
149 # override stream iterator
150 sub make_stream_iterator {
152 MyStreamIterator->new(@_);
159 Originally ripped off from L<Test::Harness>.
165 L<TAP::Parser::Iterator>,
166 L<TAP::Parser::Iterator::Array>,
167 L<TAP::Parser::Iterator::Stream>,
168 L<TAP::Parser::Iterator::Process>,