lib/Data/Record.pm

filter: Statementstmt 99.1% 117 / 118 Branchbran 89.7% 61 / 68 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 16 / 16 Podpod 100% 8 / 8 totaltotal 95.3% 205 / 215 CC 39 SCAR 17.4
CC
39
Cov
95%
records
11.0
new
8.0
_create_token
6.0
CRAP 5.7
↑ Summary
1 package Data::Record;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Data::Record - "split" on steroids
9  
10 =head1 VERSION
11  
12 Version 0.02
13  
14 =cut
15  
16 3 our $VERSION = '0.02';
17 3 ▶use constant NOT_FOUND => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use constant ALL_RECORDS => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use constant TRIM_RECORDS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 SYNOPSIS
22  
23 use Regexp::Common;
24 use Data::Record;
25 my $record = Data::Record->new({
26 split => "\n",
27 unless => $RE{quoted},
28 });
29 my @data = $record->records($data);
30  
31 =head1 DESCRIPTION
32  
33 Sometimes we need data split into records and a simple split on the input
34 record separator (C<$/>) or some other value fails because the values we're
35 splitting on may allowed in other parts of the data. Perhaps they're quoted.
36 Perhaps they're embedded in other data which should not be split up.
37  
38 This module allows you to specify what you wish to split the data on, but also
39 speficy an "unless" regular expression. If the text in question matches the
40 "unless" regex, it will not be split there. This allows us to do things like
41 split on newlines unless newlines are embedded in quotes.
42  
43 =head1 METHODS
44  
45 =head2 new
46  
47 Common usage:
48  
49 my $record = Data::Record->new({
50 split => qr/$split/,
51 unless => qr/$unless/,
52 });
53  
54 Advanced usage:
55  
56 my $record = Data::Record->new({
57 split => qr/$split/,
58 unless => qr/$unless/, # optional
59 token => $token, # optional
60 chomp => 0, # optional
61 limit => $limit, # optional (do not use with trim)
62 trim => 1, # optional (do not use with limit)
63 fields => {
64 split => ',',
65 unless => $RE{quoted}, # from Regexp::Common
66 }
67 });
68  
69 The constructor takes a hashref of key/value pairs to set the behavior of data
70 records to be created.
71  
72 =over 4
73
74 =item * split
75  
76 This is the value to split the data on. It may be either a regular expression
77 or a string.
78  
79 Defaults to the current input record separator (C<$/>).
80  
81 =item * unless
82  
83 Data will be split into records matching the split value I<unless> they also
84 match this value. No default.
85  
86 If you do not have an C<unless> value, use of this module is overkill.
87  
88 =item * token
89  
90 You will probably never need to set this value.
91  
92 Internally, this module attempts to find a token which does not match any text
93 found in the data to be split and also does not match the split value. This is
94 necessary because we mask the data we don't want to split using this token.
95 This allows us to split the resulting text.
96  
97 In the unlikely event that the module cannot find a token which is not in the
98 text, you may set the token value yourself to some string value. Do not set it
99 to a regular expression.
100  
101 =item * chomp
102  
103 By default, the split value is discarded (chomped) from each record. Set this
104 to a true value to keep the split value on each record. This differs slightly
105 from how it's done with split and capturing parentheses:
106  
107 split /(\,)/, '3,4,5';
108  
109 Ordinarily, this results in the following list:
110  
111 ( 3, ',', 4, ',', 5 )
112  
113 This module assumes you want those values I<with> the preceding record. By
114 setting chomp to false, you get the following list:
115  
116 ( '3,', '4,' 5 )
117  
118 =item * limit
119  
120 The default split behavior is similar to this:
121  
122 split $split_regex, $data;
123  
124 Setting C<limit> will cause the behavior to act like this:
125  
126 split $split_regex, $data, $limit
127  
128 See C<perldoc -f split> for more information about the behavior of C<limit>.
129  
130 You may not set both C<limit> and C<trim> in the constructor.
131  
132 =item * trim
133  
134 By default, we return all records. This means that due to the nature of split
135 and how we're doing things, we sometimes get a trailing null record. However,
136 setting this value causes the module to behave as if we had done this:
137  
138 split $split_regex, $data, 0;
139  
140 When C<split> is called with a zero as the third argument, trailing null values
141 are discarded. See C<perldoc -f split> for more information.
142  
143 You may not set both C<limit> and C<trim> in the constructor.
144  
145 B<Note>: This does I<not> trim white space around returned records.
146  
147 =item * fields
148  
149 By default, individual records are returned as strings. If you set C<fields>,
150 you pass in a hashref of arguments that are identical to what C<new> would take
151 and resulting records are returned as array references processed by a new
152 C<Data::Record> instance.
153  
154 Example: a quick CSV parser which assumes that commas and newlines may both be
155 in quotes:
156  
157 # four lines, but there are only three records! (newline in quotes)
158 $data = <<'END_DATA';
159 1,2,"programmer, perl",4,5
160 1,2,"programmer,
161 perl",4,5
162 1,2,3,4,5
163 END_DATA
164
165 $record = $RECORD->new({
166 split => "\n",
167 unless => $quoted,
168 trim => 1,
169 fields => {
170 split => ",",
171 unless => $quoted,
172 }
173 });
174 my @records = $record->records($data);
175 foreach my $fields (@records) {
176 foreach my $field = (@$fields);
177 # do something
178 }
179 }
180  
181 Note that above example will not remove the quotes from individual fields.
182  
183 =back
184  
185 =cut
186  
187 5 ▶sub new {
Subroutine new: called CC=8 SCAR=20.8
Pod documented
188 5 my ( $class, $value_of ) = @_;
189 5 my %value_of = %$value_of;
190  
191 # XXX fix this later after we have the core working
192 5 my $self = bless {}, $class;
193  
194 5 ▶ unless ( exists $value_of{split} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $value_of{"split"}) 1 4 5
195 1 $value_of{split} = $/;
196 }
197 $self->split( $value_of{split} )->unless( $value_of{unless} )
198 ->chomp( exists $value_of{chomp} ? $value_of{chomp} : 1 )
199 5 ▶ ->limit( exists $value_of{limit} ? $value_of{limit} : ALL_RECORDS );
Branch: 2 branches
BranchTrueFalseTotal
exists $value_of{'chomp'} ? : 0 5 5
exists $value_of{'limit'} ? : 1 4 5
200 5 ▶ $self->token( $value_of{token} ) if exists $value_of{token};
Branch: 1 branch
BranchTrueFalseTotal
if exists $value_of{"token"} 0 5 5
201 5 ▶ if ( exists $value_of{trim} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value_of{"trim"}) 2 3 5
202 $self->_croak("You may not specify 'trim' if 'limit' is specified")
203 2 ▶ if exists $value_of{limit};
Branch: 1 branch
BranchTrueFalseTotal
if exists $value_of{"limit"} 1 1 2
204 1 $self->trim(1);
205 }
206 4 ▶ $self->_fields( $value_of{fields} ) if exists $value_of{fields};
Branch: 1 branch
BranchTrueFalseTotal
if exists $value_of{"fields"} 1 3 4
207 4 return $self;
208 }
209  
210 ##############################################################################
211  
212 =head2 split
213  
214 my $split = $record->split;
215 $record->split($on_value);
216  
217 Getter/setter for split value. May be a regular expression or a scalar value.
218  
219 =cut
220  
221 30 ▶sub split {
Subroutine split: called CC=3 SCAR=11.0
Pod documented
222 30 my $self = shift;
223 30 ▶ return $self->{split} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 24 6 30
224  
225 6 my $split = shift;
226 6 ▶ $split = qr/\Q$split\E/ unless 'Regexp' eq ref $split;
Branch: 1 branch
BranchTrueFalseTotal
unless "Regexp" eq ref $split 5 1 6
227 6 $self->{split} = $split;
228 6 return $self;
229 }
230  
231 ##############################################################################
232  
233 =head2 unless
234  
235 my $unless = $self->unless;
236 $self->unless($is_value);
237  
238 Getter/setter for unless value. May be a regular expression or a scalar value.
239  
240 =cut
241  
242 16 ▶sub unless {
Subroutine unless: called CC=5 SCAR=16.1
Pod documented
243 16 my $self = shift;
244 16 ▶ return $self->{unless} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 10 6 16
245  
246 6 my $unless = shift;
247 6 ▶ $unless = '' unless defined $unless;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $unless 1 5 6
248 6 ▶ $unless = qr/\Q$unless\E/
Branch: 1 branch
BranchTrueFalseTotal
unless "Regexp" eq ref $unless or "Regexp::Common" eq ref $unless 2 4 6
Condition66%2/3
"Regexp" eq ref $unless or "Regexp::Common" eq ref $unless
l!l&&r!l&&!r
402
MC/DC50%1/2
"Regexp" eq ref $unless or "Regexp::Common" eq ref $unless
"Regexp" eq ref $unless "Regexp::Common" eq ref $unless
Truth table66%2/3
A or B
A= "Regexp" eq ref $unlessB= "Regexp::Common" eq ref $unless
ABresult
1X1
011
000
249 unless 'Regexp' eq ref $unless
250 || 'Regexp::Common' eq ref $unless;
251 6 $self->{unless} = $unless;
252 6 return $self;
253 }
254  
255 ##############################################################################
256  
257 =head2 chomp
258  
259 my $chomp = $record->chomp;
260 $record->chomp(0);
261  
262 Getter/setter for boolean chomp value.
263  
264 =cut
265  
266 28 ▶sub chomp {
Subroutine chomp: called CC=2 SCAR=6.9
Pod documented
267 28 my $self = shift;
268 28 ▶ return $self->{chomp} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 21 7 28
269  
270 7 $self->{chomp} = shift;
271 7 return $self;
272 }
273  
274 ##############################################################################
275  
276 =head2 limit
277  
278 my $limit = $record->limit;
279 $record->limit(3);
280  
281 Getter/setter for integer limit value.
282  
283 =cut
284  
285 19 ▶sub limit {
Subroutine limit: called CC=3 SCAR=11.0
Pod documented
286 19 my $self = shift;
287 19 ▶ return $self->{limit} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 11 8 19
288  
289 8 my $limit = shift;
290 8 ▶ unless ( $limit =~ /^-?\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($limit =~ /^-?\d+$/) 2 6 8
291 2 $self->_croak("limit must be an integer value, not ($limit)");
292 }
293 6 $self->{limit} = $limit;
294 6 return $self;
295 }
296  
297 ##############################################################################
298  
299 =head2 trim
300  
301 my $trim = $record->trim;
302 $record->trim(1);
303  
304 Getter/setter for boolean limit value. Setting this value will cause any
305 previous C<limit> value to be overwritten.
306  
307 =cut
308  
309 2 ▶sub trim {
Subroutine trim: called CC=3 SCAR=11.4
Pod documented
310 2 my $self = shift;
311 2 ▶ return $self->{trim} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
312  
313 2 my $limit = shift;
314 2 ▶ $self->{limit} = $limit ? TRIM_RECORDS : ALL_RECORDS;
Branch: 1 branch
BranchTrueFalseTotal
$limit ? : 2 0 2
315 }
316  
317 ##############################################################################
318  
319 =head2 token
320  
321 my $token = $record->token;
322 $record->token($string_not_found_in_text);
323  
324 Getter/setter for token value. Token must be a string that does not match the
325 split value and is not found in the text.
326  
327 You can return the current token value if you have set it in your code. If you
328 rely on this module to create a token (this is the normal behavior), it is not
329 available via this method until C<records> is called.
330  
331 Setting the token to an undefined value causes L<Data::Record> to try and find
332 a token itself.
333  
334 If the token matches the split value, this method will croak when you attempt
335 to set the token.
336  
337 If the token is found in the data, the C<records> method will croak when it is
338 called.
339  
340 =cut
341  
342 28 ▶sub token {
Subroutine token: called CC=4 SCAR=13.9
Pod documented
343 28 my $self = shift;
344 28 ▶ return $self->{token} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 14 14 28
345  
346 14 my $token = shift;
347 14 ▶ if ( defined $token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token) 13 1 14
348 13 ▶ if ( $token =~ $self->split ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ $self->split) 1 12 13
349 1 $self->_croak(
350 1 "Token ($token) must not match the split value (@{[$self->split]})"
351 );
352 }
353 }
354 13 $self->{token} = $token;
355 13 return $self;
356 }
357  
358 ##############################################################################
359  
360 =head2 records
361  
362 my @records = $record->records($data);
363  
364 Returns C<@records> for C<$data> based upon current split criteria.
365  
366 =cut
367  
368 12 ▶sub records {
Subroutine records: called CC=11 SCAR=24.0
Pod documented
369 12 my ( $self, $data ) = @_;
370 12 my $token = $self->_create_token($data);
371 10 my @values;
372 10 ▶ if ( defined( my $unless = $self->unless ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $unless = $self->unless)) 10 0 10
373 10 my $index = 0;
374 10 $data =~ s{($unless)}
375 {
376 15 $values[$index] = $1;
377 15 $token . $index++ . $token;
378 }gex;
379  
380 #main::diag($data);
381 }
382 10 my $split = $self->split;
383 10 ▶ $split = $self->chomp ? $split : qr/($split)/;
Branch: 1 branch
BranchTrueFalseTotal
$self->chomp ? : 7 3 10
384  
385 # if they have a numeric split value, we don't want to split tokens
386 10 my $token_re = qr/\Q$token\E/;
387 10 ▶ $split = qr/(?<!$token_re)$split(?!$token_re)/
Branch: 1 branch
BranchTrueFalseTotal
if 0 =~ /$split/ 1 9 10
388 if 0 =~ $split;
389 10 my @records = split $split, $data, $self->limit;
390 10 ▶ unless ( $self->chomp ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->chomp) 3 7 10
391 3 my @new_records;
392 3 ▶ while ( defined( my $record = shift @records ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $record = shift @records)) 9 3 12
393 9 ▶ if (@records) {
Branch: 1 branch
BranchTrueFalseTotal
if (@records) 9 0 9
394 9 $record = join '', $record, shift @records;
395 }
396 9 push @new_records, $record;
397 }
398 3 @records = @new_records;
399 }
400  
401 10 foreach my $record (@records) {
402 38 ▶ unless ( NOT_FOUND eq index $record, $token ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-1 eq index($record, $token)) 15 23 38
403 15 $record =~ s{$token_re(\d+)$token_re}{$values[$1]}gex;
404 }
405 }
406 10 ▶ if ( my $field = $self->_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field = $self->_fields) 1 9 10
407 1 $_ = [ $field->records($_) ] foreach @records;
408 }
409 10 return @records;
410 }
411  
412 11 ▶sub _fields {
Subroutine _fields: called CC=3 SCAR=11.2
413 11 my $self = shift;
414 11 ▶ return $self->{fields} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 10 1 11
415  
416 1 my $fields = ref($self)->new(shift);
417 1 ▶ if ( defined( my $token = $self->token ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $token = $self->token)) 0 1 1
418 0 $fields->token($token);
419 }
420 1 $self->{fields} = $fields;
421 1 return $self;
422 }
423  
424 3 my @tokens = map { $_ x 6 } qw( ~ ` ? " { } ! @ $ % ^ & * - _ + = );
425  
426 12 ▶sub _create_token {
Subroutine _create_token: called CC=6 SCAR=17.9
427 12 my ( $self, $data ) = @_;
428 12 my $token;
429 12 ▶ if ( defined( $token = $self->token ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($token = $self->token)) 7 5 12
430 7 ▶ $self->_croak("Current token ($token) found in data")
Branch: 1 branch
BranchTrueFalseTotal
unless -1 eq index($data, $token) 1 6 7
431 unless NOT_FOUND eq index $data, $token;
432 }
433  
434 11 foreach my $curr_token (@tokens) {
435 27 ▶ if ( NOT_FOUND eq index $data, $curr_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 eq index($data, $curr_token)) 10 17 27
436 10 $token = $curr_token;
437 10 $self->token($token);
438 10 last;
439 }
440 }
441 11 ▶ if ( defined $token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token) 10 1 11
442 10 return $token;
443 }
444  
445 1 my $tried = join ", ", @tokens;
446 1 $self->_croak(
447 "Could not determine a unique token for data. Tried ($tried)");
448 }
449  
450 6 ▶sub _croak {
Subroutine _croak: called CC=1 SCAR=0.0
451 6 my ( $self, $message ) = @_;
452 6 require Carp;
453 6 Carp::croak($message);
454 }
455  
456 =head1 BUGS
457  
458 It's possible to get erroneous results if the split value is C</\d+/>. I've
459 tried to work around this. Please let me know if there is a problem.
460  
461 =head1 CAVEATS
462  
463 This module must read I<all> of the data at once. This can make it slow for
464 larger data sets.
465  
466 =head1 AUTHOR
467  
468 Curtis "Ovid" Poe, C<< <ovid [at] cpan [dot] org> >>
469  
470 =head1 BUGS
471  
472 Please report any bugs or feature requests to
473 C<bug-data-record@rt.cpan.org>, or through the web interface at
474 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Record>.
475 I will be notified, and then you'll automatically be notified of progress on
476 your bug as I make changes.
477  
478 =head1 ACKNOWLEDGEMENTS
479  
480 Thanks to the Monks for inspiration from
481 L<http://perlmonks.org/index.pl?node_id=492002>.
482  
483 0.02 Thanks to Smylers and Stefano Rodighiero for catching POD errors.
484  
485 =head1 COPYRIGHT & LICENSE
486  
487 Copyright 2005 Curtis "Ovid" Poe, all rights reserved.
488  
489 This program is free software; you can redistribute it and/or modify it
490 under the same terms as Perl itself.
491  
492 =cut
493  
494 3 1; # End of Data::Record
↑ Summary