| 1 | package Iterator::File; | ||
| 2 | |||
| 3 | ## $Id: File.pm,v 1.14 2008/06/18 06:46:27 wdr1 Exp $ | ||
| 4 | |||
| 5 | 7 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 7 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 7 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 7 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 7 | ▶ | use IO::File; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 7 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 7 | ▶ | use Iterator::File::Utility; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 0 | ▶ | use Iterator::File::Status; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 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 | use overload '""' => \&overload_as_string, | ||
| 27 | '+' => 'overload_add'; | ||
| 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 | my ($filename, %config) = @_; | ||
| 45 | |||
| 46 | croak("No file name given to iterator_file!") unless (defined($filename)); | ||
| 47 | my $iterator = new Iterator::File( %config, | ||
| 48 | 'filename' => $filename | ||
| 49 | ); | ||
| 50 | return $iterator; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | sub new { | ||
| 56 | my ($class, %config) = @_; | ||
| 57 | |||
| 58 | %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 | unless (defined $config{'state_class'}) { | ||
| 62 | if ($config{'resume'}) { | ||
| 63 | $config{'state_class'} = $config{'state_class_with_resume'}; | ||
| 64 | } else { | ||
| 65 | $config{'state_class'} = $config{'state_class_without_resume'}; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | my $self = $class->SUPER::new( %config ); | ||
| 70 | bless(\%config, $class); | ||
| 71 | |||
| 72 | ## Instatiate the needed objects... | ||
| 73 | my $source_class = $config{'source_class'}; | ||
| 74 | my $state_class = $config{'state_class'}; | ||
| 75 | |||
| 76 | $self->_lazy_load_module( $source_class ); | ||
| 77 | $self->_lazy_load_module( $state_class ); | ||
| 78 | |||
| 79 | $self->{'source_object'} = $source_class->new( %config ); | ||
| 80 | $self->{'state_object'} = $state_class->new( %config ); | ||
| 81 | |||
| 82 | ## Do we care about status? | ||
| 83 | if ($self->{'status'}) { | ||
| 84 | $self->{'status_object'} = Iterator::File::Status->new( %config ); | ||
| 85 | } | ||
| 86 | |||
| 87 | $self->initialize(); | ||
| 88 | |||
| 89 | return $self; | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | sub initialize { | ||
| 95 | my ($self) = @_; | ||
| 96 | |||
| 97 | $self->{'source_object'}->initialize(); | ||
| 98 | $self->{'state_object'}->initialize(); | ||
| 99 | |||
| 100 | ## Pickup where we left off... | ||
| 101 | if ($self->{resume}) { | ||
| 102 | $self->{source_object}->advance_to( $self->{'state_object'}->marker() ); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | sub next { | ||
| 109 | my ($self) = @_; | ||
| 110 | |||
| 111 | my $state = $self->{'state_object'}; | ||
| 112 | my $source = $self->{'source_object'}; | ||
| 113 | |||
| 114 | if ( $self->{repeat_on_resume} ) { | ||
| 115 | $state->advance_marker(); | ||
| 116 | } | ||
| 117 | |||
| 118 | my $next_data = $source->next(); | ||
| 119 | |||
| 120 | if ( !$self->{repeat_on_resume} ) { | ||
| 121 | $state->advance_marker(); | ||
| 122 | } | ||
| 123 | |||
| 124 | unless ( defined($next_data) ) { | ||
| 125 | ## All done | ||
| 126 | $self->_verbose( "Finished. Cleaning up..." ); | ||
| 127 | $state->finish(); | ||
| 128 | $source->finish(); | ||
| 129 | } else { | ||
| 130 | if ($self->{'status'}) { | ||
| 131 | my $marker = $self->{'state_object'}->marker(); | ||
| 132 | $self->{'status_object'}->emit_status( $marker ); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | return $next_data; | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | sub skip_next { | ||
| 142 | my ($self, $num) = @_; | ||
| 143 | |||
| 144 | $num ||= 1; | ||
| 145 | while ($num--) { | ||
| 146 | $self->next(); | ||
| 147 | } | ||
| 148 | |||
| 149 | return $self->value(); | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | sub value { | ||
| 155 | my ($self) = @_; | ||
| 156 | |||
| 157 | return $self->{'source_object'}->value(); | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | sub finish { | ||
| 163 | my ($self) = @_; | ||
| 164 | |||
| 165 | $self->{'source_object'}->finish(); | ||
| 166 | $self->{'state_object'}->finish(); | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 | sub state_object { | ||
| 172 | my ($self) = @_; | ||
| 173 | |||
| 174 | return $self->{'state_object'}; | ||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | |||
| 179 | sub source_object { | ||
| 180 | my ($self) = @_; | ||
| 181 | |||
| 182 | return $self->{'source_object'}; | ||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | |||
| 187 | sub emit_status { | ||
| 188 | my ($self) = @_; | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | sub _lazy_load_module { | ||
| 194 | my ($self, $module, @module_args) = @_; | ||
| 195 | |||
| 196 | $self->_debug( "Loading '$module'... "); | ||
| 197 | eval "require $module"; | ||
| 198 | if ($@) { | ||
| 199 | confess ("Unable to load '$module': $@!"); | ||
| 200 | } | ||
| 201 | |||
| 202 | $module->import( @module_args ); | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | |||
| 207 | sub overload_as_string { | ||
| 208 | my ($self) = @_; | ||
| 209 | |||
| 210 | return $self->value(); | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | |||
| 215 | sub overload_add { | ||
| 216 | my ($self, $num) = @_; | ||
| 217 | |||
| 218 | $self->skip_next($num); | ||
| 219 | |||
| 220 | return $self; | ||
| 221 | } | ||
| 222 | |||
| 223 | 1; | ||
| 224 | __END__ | ||