File Coverage

blib/lib/LotusNotes/LoadExport.pm
Criterion Covered Total %
statement 12 98 12.2
branch 0 32 0.0
condition 0 5 0.0
subroutine 4 8 50.0
pod 3 3 100.0
total 19 146 13.0


line stmt bran cond sub pod time code
1             #
2             # LoadExport.pm
3             #
4             # Load records from a Lotus Notes database export.
5             # Expects an array reference of fields to extract.
6             # Returns an array reference of hashes where each hash represents a record
7             #
8             package LotusNotes::LoadExport;
9            
10             our $VERSION = sprintf("%d.%02d", q'$Revision: 1.1 $' =~ /(\d+)\.(\d+)/);
11            
12 1     1   84353 use strict;
  1         3  
  1         38  
13 1     1   5 use warnings;
  1         2  
  1         29  
14 1     1   6 use Carp;
  1         7  
  1         67  
15 1     1   869 use IO::File;
  1         11533  
  1         1264  
16            
17             my @data;
18            
19             sub new
20             {
21 0     0 1   my $proto = shift;
22 0   0       my $class = ref($proto) || $proto;
23            
24 0           my $self = {};
25 0           bless($self, $class);
26            
27             # Run initialisation code
28 0           $self->_init(@_);
29            
30 0           return $self;
31             }
32            
33            
34             sub _init
35             {
36 0     0     my $self = shift;
37            
38             # Handle the passed arguments
39 0           my %args = (
40             filename => 'required',
41             fieldnames => 'required array ref',
42             @_
43             );
44            
45             # Make sure we have the required arguments
46 0 0         unless ($args{filename})
47             {
48 0           carp('E921 - No filename provided to LotusNotes::LoadExport->new(filename => the_export_filename, fieldnames => \@fields', "\n");
49             }
50 0 0         unless (-r $args{filename})
51             {
52 0           carp('E922 - filename ', $args{filename}, " does not exist or is not readable\n");
53             }
54            
55 0 0         unless ($args{fieldnames})
56             {
57 0           carp('E923 - fieldnames array reference not provided to LotusNotes::LoadExport->new(filename => the_export_filename, fieldnames => \@fields', "\n");
58             }
59 0 0         unless (@{$args{fieldnames}})
  0            
60             {
61 0           carp("E925 - fieldnames array reference does not have any fieldnames!\n");
62             }
63            
64            
65             # Store the arguments we want
66 0           $self->{filename} = $args{filename};
67 0           $self->{fieldnames} = $args{fieldnames};
68             }
69            
70            
71             sub load
72             {
73 0     0 1   my $self = shift;
74            
75 0           my $fh = IO::File->new($self->{filename}, 'r');
76 0 0         unless ($fh)
77             {
78 0           carp("E926 - Open of ", $self->{filename}, " failed:$!\n");
79             }
80            
81            
82             # Generate the regex to match the labels and extract the data
83 0           my $regex_str;
84 0           foreach my $label (@{ $self->{fieldnames} })
  0            
85             {
86 0           $regex_str .= q{^(} . $label . q{):\s+(.*)\s*$|};
87             }
88             # Remove the trailing pipe character
89 0           $regex_str =~ s/\|$//;
90 0           my $regex = qr{$regex_str};
91            
92 0           my %record;
93 0           my $record_cnt = 0;
94            
95 0           while (<$fh>)
96             {
97             # This the the end of record flag
98 0 0         if (/\f/)
99             {
100 0           push @data, { %record };
101 0           $record_cnt++;
102 0           %record = ();
103 0           next;
104             }
105            
106 0           chomp;
107 0 0         if ((my @matched) = $_ =~ $regex)
108             {
109 0 0         my ($label, $value) = grep { $_ && $_ ne '' } @matched;
  0            
110 0           $record{$label} = $value;
111             }
112             }
113            
114             # Handle the last record if not already added to the array
115 0 0         if (keys %record)
116             {
117 0           push @data, { %record };
118             }
119            
120            
121 0           $fh->close();
122            
123 0           $self->{record_cnt} = $record_cnt;
124            
125 0           return \@data;
126             }
127            
128            
129             sub get_next
130             {
131 0     0 1   my $self = shift;
132            
133 0           my $fh;
134             my $file_position;
135 0 0         if ($self->{get_next_fh})
136             {
137 0           $fh = $self->{get_next_fh};
138 0   0       $file_position = $self->{get_next_file_position} || 0;
139 0           $fh->setpos($file_position);
140            
141 0           $self->{get_next_record_cnt} = 0;
142             }
143             else
144             {
145 0           $fh = IO::File->new($self->{filename}, 'r');
146 0 0         unless ($fh)
147             {
148 0           carp("E926 - Open of ", $self->{filename}, " failed:$!\n");
149             }
150 0           $file_position = 0;
151            
152             # Sore for later use
153 0           $self->{get_next_fh} = $fh;
154 0           $self->{get_next_file_position} = $file_position;
155             }
156            
157            
158             # Generate the regex to match the labels and extract the data
159 0           my $regex;
160 0 0         if ($self->{get_next_regex})
161             {
162 0           $regex = $self->{get_next_regex};
163             }
164             else
165             {
166 0           my $regex_str;
167 0           foreach my $label (@{ $self->{fieldnames} })
  0            
168             {
169 0           $regex_str .= q{^(} . $label . q{):\s+(.*)\s*$|};
170             }
171             # Remove the trailing pipe character
172 0           $regex_str =~ s/\|$//;
173 0           $regex = qr{$regex_str};
174            
175             # Sore for later use
176 0           $self->{get_next_regex} = $regex;
177             }
178            
179 0           my %record;
180 0           while (<$fh>)
181             {
182             # This the the end of record flag
183 0 0         if (/\f/)
184             {
185 0           $self->{get_next_record_cnt}++;
186 0           $self->{get_next_file_position} = $fh->getpos();
187 0           return \%record;
188             }
189            
190 0           chomp;
191 0 0         if ((my @matched) = $_ =~ $regex)
192             {
193 0 0         my ($label, $value) = grep { $_ && $_ ne '' } @matched;
  0            
194 0           $record{$label} = $value;
195             }
196             }
197            
198             # Handle the last record if not already added to the array
199 0 0         if (keys %record)
200             {
201 0           $self->{get_next_record_cnt}++;
202 0           return \%record;
203             }
204            
205 0           $fh->close();
206 0           return;
207             }
208            
209            
210             #####################################################################
211             # DO NOT REMOVE THE FOLLOWING LINE, IT IS NEEDED TO LOAD THIS LIBRARY
212             1;
213            
214            
215             __END__