File Coverage

blib/lib/Iterator/File.pm
Criterion Covered Total %
statement 88 96 91.6
branch 13 20 65.0
condition 1 2 50.0
subroutine 20 22 90.9
pod 6 12 50.0
total 128 152 84.2


line stmt bran cond sub pod time code
1             package Iterator::File;
2              
3             ## $Id: File.pm,v 1.14 2008/06/18 06:46:27 wdr1 Exp $
4              
5 7     7   250189 use 5.006;
  7         47  
  7         268  
6 7     7   40 use strict;
  7         12  
  7         267  
7 7     7   36 use warnings;
  7         27  
  7         431  
8              
9 7     7   35 use Carp;
  7         12  
  7         674  
10 7     7   6559 use IO::File;
  7         88070  
  7         974  
11 7     7   6154 use Data::Dumper;
  7         56959  
  7         527  
12              
13 7     7   4372 use Iterator::File::Utility;
  7         18  
  7         205  
14 7     7   4387 use Iterator::File::Status;
  7         23  
  7         194  
15              
16             require Exporter;
17              
18             our @ISA = qw(Exporter Iterator::File::Utility);
19              
20             our @EXPORT = qw(
21             iterator_file
22             );
23              
24             our $VERSION = substr(q$Revision: 1.14 $, 10);
25              
26 7         90 use overload '""' => \&overload_as_string,
27 7     7   1122 '+' => 'overload_add';
  7         13  
28              
29             my %default_config =
30             (
31             'chomp' => 1,
32             'source_class' => 'Iterator::File::Source::FlatFile',
33             'state_class_without_resume' => 'Iterator::File::State::Interface',
34             'state_class_with_resume' => 'Iterator::File::State::TempFile',
35             'resume' => 0,
36             'repeat_on_resume' => 1,
37             'verbose' => 0,
38             'status' => 0,
39             );
40            
41              
42              
43             sub iterator_file {
44 10     10 1 9022 my ($filename, %config) = @_;
45              
46 10 50       34 croak("No file name given to iterator_file!") unless (defined($filename));
47 10         73 my $iterator = new Iterator::File( %config,
48             'filename' => $filename
49             );
50 10         35 return $iterator;
51             }
52              
53              
54              
55             sub new {
56 10     10 1 35 my ($class, %config) = @_;
57              
58 10         113 %config = (%default_config, %config);
59             ## What type of default state class do we use?
60             ## Make sure we respect any explicit input from the user.
61 10 50       53 unless (defined $config{'state_class'}) {
62 10 100       30 if ($config{'resume'}) {
63 4         10 $config{'state_class'} = $config{'state_class_with_resume'};
64             } else {
65 6         15 $config{'state_class'} = $config{'state_class_without_resume'};
66             }
67             }
68              
69 10         278 my $self = $class->SUPER::new( %config );
70 10         27 bless(\%config, $class);
71              
72             ## Instatiate the needed objects...
73 10         21 my $source_class = $config{'source_class'};
74 10         16 my $state_class = $config{'state_class'};
75            
76 10         33 $self->_lazy_load_module( $source_class );
77 10         59 $self->_lazy_load_module( $state_class );
78            
79 10         286 $self->{'source_object'} = $source_class->new( %config );
80 10         130 $self->{'state_object'} = $state_class->new( %config );
81              
82             ## Do we care about status?
83 10 50       33 if ($self->{'status'}) {
84 0         0 $self->{'status_object'} = Iterator::File::Status->new( %config );
85             }
86              
87 10         28 $self->initialize();
88              
89 10         77 return $self;
90             }
91              
92              
93              
94             sub initialize {
95 10     10 1 16 my ($self) = @_;
96              
97 10         35 $self->{'source_object'}->initialize();
98 10         72 $self->{'state_object'}->initialize();
99              
100             ## Pickup where we left off...
101 10 100       35 if ($self->{resume}) {
102 4         17 $self->{source_object}->advance_to( $self->{'state_object'}->marker() );
103             }
104             }
105              
106              
107              
108             sub next {
109 39     39 1 75 my ($self) = @_;
110              
111 39         55 my $state = $self->{'state_object'};
112 39         85 my $source = $self->{'source_object'};
113              
114 39 50       171 if ( $self->{repeat_on_resume} ) {
115 39         103 $state->advance_marker();
116             }
117              
118 39         95 my $next_data = $source->next();
119              
120 39 50       87 if ( !$self->{repeat_on_resume} ) {
121 0         0 $state->advance_marker();
122             }
123              
124 39 100       68 unless ( defined($next_data) ) {
125             ## All done
126 5         30 $self->_verbose( "Finished. Cleaning up..." );
127 5         13 $state->finish();
128 5         12 $source->finish();
129             } else {
130 34 50       74 if ($self->{'status'}) {
131 0         0 my $marker = $self->{'state_object'}->marker();
132 0         0 $self->{'status_object'}->emit_status( $marker );
133             }
134             }
135              
136 39         173 return $next_data;
137             }
138              
139              
140              
141             sub skip_next {
142 5     5 0 11 my ($self, $num) = @_;
143              
144 5   50     14 $num ||= 1;
145 5         15 while ($num--) {
146 12         21 $self->next();
147             }
148              
149 5         13 return $self->value();
150             }
151              
152              
153              
154             sub value {
155 27     27 1 48 my ($self) = @_;
156              
157 27         79 return $self->{'source_object'}->value();
158             }
159              
160              
161              
162             sub finish {
163 2     2 1 12 my ($self) = @_;
164              
165 2         9 $self->{'source_object'}->finish();
166 2         45 $self->{'state_object'}->finish();
167             }
168              
169              
170              
171             sub state_object {
172 2     2 0 33 my ($self) = @_;
173              
174 2         8 return $self->{'state_object'};
175             }
176              
177              
178              
179             sub source_object {
180 0     0 0 0 my ($self) = @_;
181            
182 0         0 return $self->{'source_object'};
183             }
184              
185              
186              
187             sub emit_status {
188 0     0 0 0 my ($self) = @_;
189             }
190              
191              
192              
193             sub _lazy_load_module {
194 20     20   37 my ($self, $module, @module_args) = @_;
195              
196 20         103 $self->_debug( "Loading '$module'... ");
197 20         1914 eval "require $module";
198 20 50       85 if ($@) {
199 0         0 confess ("Unable to load '$module': $@!");
200             }
201              
202 20         161 $module->import( @module_args );
203             }
204              
205              
206              
207             sub overload_as_string {
208 7     7 0 144 my ($self) = @_;
209              
210 7         16 return $self->value();
211             }
212              
213              
214              
215             sub overload_add {
216 4     4 0 15 my ($self, $num) = @_;
217              
218 4         10 $self->skip_next($num);
219            
220 4         9 return $self;
221             }
222              
223             1;
224             __END__