ETLp/File/Read.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 3 Podpod n/a totaltotal 0.0% 0 / 54 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package ETLp::File::Read;
2  
3 use MooseX::Declare;
4  
5 =head1 NAME
6  
7 ETLp::File::Read - Open a file and read a line when requested
8  
9 =head1 SYNOPSIS
10  
11 use ETLp::File::Read;
12  
13 my $fh = ETLp::File::Read->new(
14 filename => "/data/comit/final.csv",
15 );
16
17 while (my $line = $fh->get_line) {
18 print $fh->line_counter() .": " . $line, "\n";
19 };
20
21 =cut
22  
23 class ETLp::File::Read with ETLp::Role::Config {
24 use ETLp::Exception;
25 use File::LocalizeNewlines;
26 has 'filename' => (is => 'ro', isa => 'Str', required => 1);
27 has 'directory' => (is => 'ro', isa => 'Str', required => 0);
28 has 'localize' => (is => 'ro', isa => 'Bool', required => 0, default => 0);
29 has 'skip' => (is => 'ro', isa => 'Int', required => 0, default => 0);
30  
31 =head1 METHODS
32  
33 =head2 new
34  
35 Create a File object.
36  
37 Parameters
38  
39 Hash or hashref consisting of
40  
41 * directory: optional. The directory where the file resides
42 * filename: required. Name of the file to be opened. Requires full path
43 to the file is the directory is not supplied
44 * localize: optional. Whether to localize line endings.
45  
46 Returns
47  
48 * a ETLp::File::Read object
49
50 =head2 line_counter
51  
52 Return the line number last read from the file
53  
54 Parameters
55  
56 None
57  
58 Returns
59  
60 A postive integer
61
62 =cut
63  
64 method line_counter {
65 return $self->{_line_counter};
66 }
67  
68 =head2 get_line
69  
70 gets the next line from the file.
71  
72 Parameters
73  
74 None
75
76 Returns
77  
78 A line's content. The Record separator (EOL character) is removed
79
80 =cut
81  
82 method get_line {
83 my $fh = $self->{_fh};
84 my $line = <$fh>;
85 if ($line) {
86 chomp $line;
87 $self->{_line_counter}++;
88 } else {
89 return;
90 }
91  
92 # Skip header rows if required.
93 if ($self->line_counter <= $self->skip) {
94 $self->logger->debug(
95 'Skipping line. Counter now ' . $self->line_counter);
96 return $self->get_line;
97 }
98  
99 return $line;
100 }
101
102 method BUILD {
103
104 my $filename = $self->filename;
105
106 if ($self->directory) {
107 $filename = $self->directory . '/' . $filename;
108 }
109
110 $self->logger->debug("Filename: $filename");
111
112 if ($self->localize) {
113 my $localize = File::LocalizeNewlines->new;
114 my $num_localized = $localize->localize($filename);
115 }
116
117 $self->logger->debug("Skip: ". $self->skip);
118
119 open(my $fh, "<", $filename)
120 || ETLpException->throw(error => "Cannot open $filename: $!");
121 $self->{_fh} = $fh;
122 $self->{_line_counter} = 0;
123
124 }
125  
126 }
127  
128 =head1 LICENSE AND COPYRIGHT
129  
130 Copyright 2010 Redbone Systems Ltd
131  
132 This program is free software; you can redistribute it and/or modify it
133 under the terms of either: the GNU General Public License as published
134 by the Free Software Foundation; or the Artistic License.
135  
136 The terms are in the LICENSE file that accompanies this application
137
138 =cut