DataSource/Lines.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 196 CC 10 SCAR 22.0
CC
10
Cov
0%
load
20.0
read_next
20.0
read_session
6.0
CRAP 9.0
1 package IDS::DataSource::Lines;
2 use base qw(IDS::DataSource);
3  
4 =head1 IDS::DataSource::Lines
5  
6 =head2 Introduction
7  
8 This class is a subclass of IDS::DataSource, and meets its interface
9 specification.
10  
11 This class exists primarily as a template for new IDS::DataSource subclasses,
12 but it is fully-functional, if a little boring.
13  
14 =cut
15  
16 use strict;
17 use warnings;
18 use Carp qw(cluck carp confess);
19 use IO::Handle;
20  
21 $IDS::DataSource::Lines::VERSION = "1.0";
22  
23 =over
24  
25 =item load(source)
26  
27 load exactly one request from a file, IO::Handle, or supplied string.
28 If a string is used, it is up to the caller to ensure that more than
29 one request is not in the string.
30  
31 =back
32  
33 =cut
34  
35 sub load {
36 my $self = shift;
37 my $source = shift;
38 defined($source) or
39 confess *load{PACKAGE} . "::load called without a source\n";
40  
41 $self->empty();
42 if ($source->isa("IO::Handle")) {
43 ${$self->{"params"}}{"source"} = "IO::handle";
44 $self->read_session($source);
45 } elsif ($source =~ /\n/) {
46 ${$self->{"params"}}{"source"} = "Supplied string";
47 $self->{"data"} = $source;
48 } elsif (-f $source) {
49 ${$self->{"params"}}{"source"} = $source;
50 my $fh = new IO::File("< $source");
51 defined($fh) or carp "Unable to open $source: $!";
52 $self->read_session($fh);
53 } else {
54 confess *load{PACKAGE} . "::load: I do not know what to do with '$source'\n";
55 }
56 $self->parse;
57 }
58  
59 =over
60  
61 =item read_session(filehandle)
62  
63 Read an HTTP session (up to EOF or a blank line) from the filehandle
64 passed as an argument.
65  
66 =back
67  
68 =cut
69  
70 sub read_session {
71 my $self = shift;
72 my $fh = shift or
73 cluck *read_session{PACKAGE} .
74 "::read_session called without a filehandle";
75  
76 my $data = <$fh>;
77 chomp($data);
78  
79 if (defined($data) && $data) {
80 $self->load($data);
81 return 1;
82 } else {
83 $self->warn(${$self->{"params"}}{"source"} . ": data was empty", [],"");
84 return 0;
85 }
86 }
87  
88 =over
89  
90 =item read_next(filehandle)
91  
92 Read the next request from the list of files (filehandle containing
93 list of file names is argument).
94  
95 =back
96  
97 =cut
98  
99 # This function has to keep track of state, making it more complex than
100 # a loop in a caller.
101 sub read_next {
102 my $self = shift;
103 my $fname_fh = shift;
104 defined($fname_fh) or
105 cluck *read_next{PACKAGE} . "::read_next called without a filehandle";
106
107 my $fh = $self->{"current_fh"};
108  
109 # reset everything for the new request
110 $self->empty();
111  
112 my $ret;
113 do {
114 # See if we are done.
115 return undef if $fname_fh->eof && $fh->eof;
116  
117 # while we need another file and we have files in the list;
118 # may run 0 times if there is more to read from the current
119 # file.
120 while (! (defined($fh) && ! $fh->eof) && ! $fname_fh->eof) {
121 $self->mesg(1, "Processed " . $self->{"fname"} . "\n")
122 if exists($self->{"fname"}) && defined($self->{"fname"});
123 $fh = $self->open_next($fname_fh);
124 }
125 # see if we ran out of filenames
126 return undef unless defined($fh);
127  
128 $self->{"session"}++;
129 ${$self->{"params"}}{"source"} = $self->{"fname"} . " request " .
130 $self->{"session"};
131 $ret = $self->read_session($fh);
132 } until ($ret);
133 $self->{"current_fh"} = $fh;
134 return 1;
135 }
136  
137 # open_next is a utility function used by read_files; should not be
138 # called outside of this object
139 sub open_next {
140 my $self = shift;
141 my $fname_fh = shift;
142 my $fname;
143  
144 # skip blank or commented lines
145 do {
146 $fname = $fname_fh->getline;
147 chomp $fname;
148 # support comments in input
149 $fname =~ s/\s*#.*$//;
150 } while ($fname =~ /^$/ && ! $fname_fh->eof);
151 $self->{"fname"} = $fname;
152 $self->{"session"} = 0;
153 $self->{"current_fh"} = new IO::File "$fname" or
154 $self->warn("Unable to open $fname: $!\n", [], "");
155 $self->mesg(1, "Opened $fname");
156 return $self->{"current_fh"};
157 }
158  
159 =over
160  
161 =item data()
162  
163 Return the data used for the tokens we have. If called in array mode,
164 we return the individual lines, otherwise the join of those line.
165  
166 =back
167  
168 =cut
169  
170 sub data {
171 my $self = shift;
172 return $self->{"data"};
173 }
174  
175 =over
176  
177 =item source()
178  
179 =item source(value)
180  
181 Set and/or get the data source.
182  
183 =back
184  
185 =cut
186  
187 sub source {
188 my $self = shift;
189 if (defined($_[0])) {
190 my $old = ${$self->{"params"}}{"source"};
191 ${$self->{"params"}}{"source"} = $_[0];
192 return $old;
193 } else {
194 return ${$self->{"params"}}{"source"};
195 }
196 }
197  
198 =over
199  
200 =item tokens()
201  
202 Return the tokens that result from parsing the structure. The tokens
203 can be returned as an array or a reference to the internal array holding
204 them (for efficiency). Modify this referenced array at your own risk.
205  
206 =back
207  
208 =cut
209  
210 sub tokens {
211 my $self = shift;
212  
213 my @tokens = ( $self->{"data"} );
214  
215 return wantarray ? @tokens : \@tokens;
216 }
217  
218 sub empty {
219 my $self = shift;
220 undef $self->{"data"};
221 }
222  
223 =head2 Functions required by IDS::DataSource
224  
225 =over
226  
227 =item default_parameters()
228  
229 Sets all of the default values for the parameters. Normally called by
230 new() or one of its descendents.
231  
232 =back
233  
234 =cut
235  
236 sub default_parameters {
237 my $self = shift;
238 my %params = (
239 # general parameters
240 # source # filled in by IDS::DataSource::HTTP::read_session,
241 # it is the source of data,
242 # used when producing error/warn msgs
243 "msg_fh" => $self->fh_or_stdout,# Where warning messages go; nowhere if undef
244 "verbose" => 0, # Print extra information; larger means more
245
246 );
247 $self->{"params"} = \%params;
248 }
249  
250 =over
251  
252 =item param_options()
253  
254 Command-line option specifiers for our parameters for GetOpt::Long.
255  
256 =back
257  
258 =cut
259  
260 sub param_options {
261 my $self = shift;
262 return (
263 "lines_verbose=i" => \${$self->{"params"}}{"verbose"},
264 );
265 }
266  
267 =head1 AUTHOR INFORMATION
268  
269 Copyright 2005-2007, Kenneth Ingham. All rights reserved.
270  
271 This library is free software; you can redistribute it and/or modify
272 it under the same terms as Perl itself.
273  
274 Address bug reports and comments to: ids_test at i-pi.com. When sending
275 bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm,
276 IDS::DataSource.pm, the version of Perl, and the name and version of the
277 operating system you are using. Since Kenneth is a PhD student, the
278 speed of the reponse depends on how the research is proceeding.
279  
280 =head1 BUGS
281  
282 Please report them.
283  
284 =head1 SEE ALSO
285  
286 L<IDS::Algorithm>, L<IDS::DataSource>
287  
288 =cut
289  
290  
291 1;