Debian lenny version packages
[pkg-perl] / deb-src / libtest-harness-perl / libtest-harness-perl-3.12 / lib / TAP / Parser / IteratorFactory.pm
1 package TAP::Parser::IteratorFactory;
2
3 use strict;
4 use vars qw($VERSION @ISA);
5
6 use TAP::Object                    ();
7 use TAP::Parser::Iterator::Array   ();
8 use TAP::Parser::Iterator::Stream  ();
9 use TAP::Parser::Iterator::Process ();
10
11 @ISA = qw(TAP::Object);
12
13 =head1 NAME
14
15 TAP::Parser::IteratorFactory - Internal TAP::Parser Iterator
16
17 =head1 VERSION
18
19 Version 3.12
20
21 =cut
22
23 $VERSION = '3.12';
24
25 =head1 SYNOPSIS
26
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);
32
33   my $line = $iter->next;
34
35 =head1 DESCRIPTION
36
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
39 module directly.
40
41 =head1 METHODS
42
43 =head2 Class Methods
44
45 =head3 C<new>
46
47 Creates a new factory class.
48 I<Note:> You currently don't need to instantiate a factory in order to use it.
49
50 =head3 C<make_iterator>
51
52 Create an iterator.  The type of iterator created depends on the arguments to
53 the constructor:
54
55   my $iter = TAP::Parser::Iterator->make_iterator( $filehandle );
56
57 Creates a I<stream> iterator (see L</make_stream_iterator>).
58
59   my $iter = TAP::Parser::Iterator->make_iterator( $array_reference );
60
61 Creates an I<array> iterator (see L</make_array_iterator>).
62
63   my $iter = TAP::Parser::Iterator->make_iterator( $hash_reference );
64
65 Creates a I<process> iterator (see L</make_process_iterator>).
66
67 =cut
68
69 sub make_iterator {
70     my ( $proto, $thing ) = @_;
71
72     my $ref = ref $thing;
73     if ( $ref eq 'GLOB' || $ref eq 'IO::Handle' ) {
74         return $proto->make_stream_iterator($thing);
75     }
76     elsif ( $ref eq 'ARRAY' ) {
77         return $proto->make_array_iterator($thing);
78     }
79     elsif ( $ref eq 'HASH' ) {
80         return $proto->make_process_iterator($thing);
81     }
82     else {
83         die "Can't iterate with a $ref";
84     }
85 }
86
87
88 =head3 C<make_stream_iterator>
89
90 Make a new stream iterator and return it.  Passes through any arguments given.
91 Defaults to a L<TAP::Parser::Iterator::Stream>.
92
93 =head3 C<make_array_iterator>
94
95 Make a new array iterator and return it.  Passes through any arguments given.
96 Defaults to a L<TAP::Parser::Iterator::Array>.
97
98 =head3 C<make_process_iterator>
99
100 Make a new process iterator and return it.  Passes through any arguments given.
101 Defaults to a L<TAP::Parser::Iterator::Process>.
102
103 =cut
104
105 sub make_stream_iterator {
106     my $proto = shift;
107     TAP::Parser::Iterator::Stream->new(@_);
108 }
109
110 sub make_array_iterator {
111     my $proto = shift;
112     TAP::Parser::Iterator::Array->new(@_);
113 }
114
115 sub make_process_iterator {
116     my $proto = shift;
117     TAP::Parser::Iterator::Process->new(@_);
118 }
119
120 1;
121
122
123 =head1 SUBCLASSING
124
125 Please see L<TAP::Parser/SUBCLASSING> for a subclassing overview.
126
127 There are a few things to bear in mind when creating your own
128 C<ResultFactory>:
129
130 =over 4
131
132 =item 1
133
134 The factory itself is never instantiated (this I<may> change in the future).
135 This means that C<_initialize> is never called.
136
137 =back
138
139 =head2 Example
140
141   package MyIteratorFactory;
142
143   use strict;
144   use vars '@ISA';
145
146   use MyStreamIterator;
147   use TAP::Parser::IteratorFactory;
148
149   @ISA = qw( TAP::Parser::IteratorFactory );
150
151   # override stream iterator
152   sub make_stream_iterator {
153     my $proto = shift;
154     MyStreamIterator->new(@_);
155   }
156
157   1;
158
159 =head1 ATTRIBUTION
160
161 Originally ripped off from L<Test::Harness>.
162
163 =head1 SEE ALSO
164
165 L<TAP::Object>,
166 L<TAP::Parser>,
167 L<TAP::Parser::Iterator>,
168 L<TAP::Parser::Iterator::Array>,
169 L<TAP::Parser::Iterator::Stream>,
170 L<TAP::Parser::Iterator::Process>,
171
172 =cut
173