File Coverage

blib/lib/Iterator/File/Source/FlatFile.pm
Criterion Covered Total %
statement 49 49 100.0
branch 6 8 75.0
condition 4 6 66.6
subroutine 13 13 100.0
pod 6 6 100.0
total 78 82 95.1


line stmt bran cond sub pod time code
1             package Iterator::File::Source::FlatFile;
2              
3             ## $Id: FlatFile.pm,v 1.6 2008/06/16 07:42:08 wdr1 Exp $
4              
5 5     5   1353 use 5.006;
  5         18  
  5         201  
6 5     5   26 use strict;
  5         8  
  5         151  
7 5     5   24 use warnings;
  5         9  
  5         125  
8              
9 5     5   29 use Carp;
  5         7  
  5         418  
10 5     5   26 use IO::File;
  5         8  
  5         1431  
11 5     5   31 use Data::Dumper;
  5         21  
  5         251  
12              
13 5     5   2611 use Iterator::File::Source::Interface;
  5         12  
  5         45  
14              
15             our @ISA = qw(Iterator::File::Source::Interface);
16              
17             our $VERSION = substr(q$Revision: 1.6 $, 10);
18              
19             our %default_config = ();
20              
21             sub new {
22 10     10 1 62 my ($class, %config) = @_;
23              
24 10 50       31 croak("No file name given!") unless ($config{filename});
25 10         80 %config = (%default_config, %config);
26 10         49 $config{'_current_value'} = undef;
27              
28             ## Instantiation
29 10         66 my $self = $class->SUPER::new( %config );
30 10         25 bless($self, $class);
31              
32 10         43 return $self;
33             }
34              
35              
36             sub initialize {
37 10     10 1 14 my ($self) = @_;
38            
39 10         51 $self->_debug( __PACKAGE__ . " initializing... ");
40            
41 10   33     64 my $fh = new IO::File ($self->{filename})
42             || croak("Couldn't open '", $self->{filename}, "': $!");
43 10         801 $self->{'_file_handle'} = $fh;
44              
45             }
46              
47              
48             sub next {
49 43     43 1 45 my ($self) = @_;
50              
51 43         51 my $fh = $self->{_file_handle};
52 43         195 $self->{'_current_value'} = <$fh>;
53            
54 43 100 100     128 if ( $self->{'chomp'} && $self->{'_current_value'} ) {
55 13         21 chomp( $self->{'_current_value'} );
56             }
57            
58 43         114 return $self->{'_current_value'};
59             }
60              
61              
62             sub advance_to {
63 4     4 1 7 my ($self, $marker) = @_;
64              
65 4 100       17 return unless ($marker);
66            
67 1         8 $self->_verbose("Advancing to '$marker'...");
68 1         2 my $i = 0;
69 1         5 while ($i++ <= $marker) {
70 4         6 $self->next();
71             }
72             }
73              
74              
75             sub value {
76 27     27 1 30 my ($self) = @_;
77              
78 27         109 return $self->{'_current_value'};
79             }
80              
81              
82             sub finish {
83 7     7 1 11 my ($self) = @_;
84              
85 7         12 my $fh = $self->{'_file_handle'};
86 7 50       42 $fh->close() || warn("Couldn't close '", $self->{'filename'}, "': $!");
87             }
88             1;
89             __END__