blib/lib/Text/FixedWidth.pm

filter: Statementstmt 90.9% 150 / 165 Branchbran 73.5% 50 / 68 Conditioncond 66.6% 22 / 33 MC/DCmcdc 30.0% 6 / 20 Subroutinesub 95.2% 20 / 21 Podpod 100% 7 / 7 totaltotal 81.2% 255 / 314 CC 49 SCAR 20.7
CC
49
Cov
83%
_getf
17.6
set_attribute
12.5
_set
7.1
CRAP 7.9
↑ Summary
1 package Text::FixedWidth;
2  
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use vars ('$AUTOLOAD');
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Text::FixedWidth - Easy OO manipulation of fixed width text files
12  
13 =cut
14  
15 9 our $VERSION = '0.13';
16  
17 =head1 SYNOPSIS
18  
19 use Text::FixedWidth;
20  
21 my $fw = new Text::FixedWidth;
22 $fw->set_attributes(qw(
23 fname undef %10s
24 lname undef %-10s
25 points 0 %04d
26 ));
27  
28 $fw->parse(string => ' JayHannah 0003');
29 $fw->get_fname; # Jay
30 $fw->get_lname; # Hannah
31 $fw->get_points; # 0003
32  
33 $fw->set_fname('Chuck');
34 $fw->set_lname('Norris');
35 $fw->set_points(17);
36 $fw->string; # ' ChuckNorris 0017'
37  
38 If you're familiar with printf formats, then this class should make processing
39 fixed width files trivial.
40 Just define your attributes and then you can get_* and set_* all day long. When
41 you're happy w/ your values envoke string() to spit out your object in your
42 defined fixed width format.
43  
44 When reading a fixed width file, simply pass each line of the file into parse(), and
45 then you can use the get_ methods to retrieve the value of whatever attributes you
46 care about.
47  
48 =head1 METHODS
49  
50 =head2 new
51  
52 Constructor. Does nothing fancy.
53  
54 =cut
55  
56 7 ▶sub new {
Subroutine new: called CC=3 SCAR=12.3
Pod documented
57 7 my ($caller,%args) = (@_);
58  
59 7 my $caller_is_obj = ref($caller);
60 7 ▶ my $class = $caller_is_obj || $caller;
Condition33%1/3
$caller_is_obj || $caller
l!l&&r!l&&!r
070
MC/DC0%0/2
$caller_is_obj || $caller
$caller_is_obj $caller
Truth table33%1/3
A || B
A= $caller_is_objB= $caller
ABresult
1X1
011
000
61 7 ▶ my $self = bless {}, ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
62 7 return $self;
63 }
64  
65  
66 =head2 set_attributes
67  
68 Pass in arguments in sets of 3 and we'll set up attributes for you.
69  
70 The first argument is the attribute name. The second argument is the default
71 value we should use until told otherwise. The third is the printf format we should
72 use to read and write this attribute from/to a string.
73  
74 $fw->set_attributes(qw(
75 fname undef %10s
76 lname undef %-10s
77 points 0 %04d
78 );
79  
80 =cut
81  
82 5 ▶sub set_attributes {
Subroutine set_attributes: called CC=6 SCAR=18.1
Pod documented
83 5 my ($self, @att) = @_;
84  
85 0 ▶ unless (@att % 3 == 0) { die "set_attributes() requires sets of 3 parameters"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (@att % 3 == 0) 0 5 5
86 5 ▶ while (@att) {
Branch: 1 branch
BranchTrueFalseTotal
if (@att) 14 5 19
87 14 my ($att, $value, $sprintf) = splice @att, 0, 3;
88 14 ▶ if (exists $self->{_attributes}{$att}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_attributes"}{$att}) 0 14 14
89 0 die "You already set attribute name '$att'! You can't set it again! All your attribute names must be unique";
90 }
91 14 ▶ if ($value && $value eq "undef") { $value = undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($value and $value eq "undef") 8 6 14
Condition100%3/3
$value and $value eq "undef"
!ll&&!rl&&r
248
MC/DC100%2/2
$value and $value eq "undef"
$value $value eq "undef"
Truth table100%3/3
A and B
A= $valueB= $value eq "undef"
ABresult
0X0
100
111
92 14 $self->{_attributes}{$att}{sprintf} = $sprintf;
93 14 $self->{_attributes}{$att}{value} = $value;
94 14 my ($length) = ($sprintf =~ /(\d+)/g);
95 14 $self->{_attributes}{$att}{length} = $length;
96 14 push @{$self->{_attribute_order}}, $att;
97 }
98  
99 5 return 1;
100 }
101  
102  
103 =head2 set_attribute
104  
105 Like set_attributes, but only sets one attribute at a time, via named parameters:
106  
107 $fw->set_attribute(
108 name => 'lname',
109 default => undef,
110 format => '%10s',
111 );
112  
113 If an sprintf 'format' is insufficiently flexible, you can set 'reader' to a code reference
114 and also define 'length'. For example, if you need a money format without a period:
115  
116 $fw->set_attribute(
117 name => 'points2',
118 reader => sub { sprintf("%07.0f", $_[0]->get_points2 * 100) },
119 length => 7,
120 );
121 $fw->set_points2(13.2);
122 $fw->get_points2; # 13.2
123 $fw->getf_points2; # 0001320
124  
125 Similarly, you can set 'writer' to a code reference for arbitrary manipulations when
126 setting attributes:
127  
128 $fw->set_attribute(
129 name => 'points3',
130 writer => sub { $_[1] / 2 },
131 format => '%-6s',
132 );
133 $fw->set_points3(3);
134 $fw->get_points3; # 1.5
135 $fw->getf_points3; # '1.5 '
136  
137 =cut
138  
139 7 ▶sub set_attribute {
Subroutine set_attribute: called CC=10 SCAR=25.2
Pod documented
140 7 my ($self, %args) = @_;
141 7 my $att = $args{name};
142 7 my $value = $args{default};
143 7 my $sprintf = $args{format};
144 7 my $reader = $args{reader};
145 7 my $writer = $args{writer};
146 7 my $length = $args{length};
147  
148 7 ▶ unless ($att) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($att) 0 7 7
149 0 die "set_attribute() requires a 'name' argument";
150 }
151 7 ▶ unless ($sprintf || $reader) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sprintf or $reader) 0 7 7
Condition66%2/3
$sprintf or $reader
l!l&&r!l&&!r
430
MC/DC0%0/2
$sprintf or $reader
$sprintf $reader
Truth table66%2/3
A or B
A= $sprintfB= $reader
ABresult
1X1
011
000
152 0 die "set_attribute() requires a 'format' or a 'reader' argument";
153 }
154 7 ▶ if ($reader && not defined $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader and not defined $length) 0 7 7
Condition66%2/3
$reader and not defined $length
!ll&&!rl&&r
430
MC/DC0%0/2
$reader and not defined $length
$reader not defined $length
Truth table66%2/3
A and not(B)
A= $readerB= not defined $length
ABresult
0X0
100
111
155 0 die "set_attribute() requires a 'length' when a 'reader' argument is provided";
156 }
157 7 ▶ if (exists $self->{_attributes}{$att}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_attributes"}{$att}) 0 7 7
158 0 die "You already set attribute name '$att'! You can't set it again! All your attribute names must be unique";
159 }
160  
161 0 ▶ if ($value && $value eq "undef") { $value = undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($value and $value eq "undef") 0 7 7
Condition33%1/3
$value and $value eq "undef"
!ll&&!rl&&r
700
MC/DC0%0/2
$value and $value eq "undef"
$value $value eq "undef"
Truth table33%1/3
A and B
A= $valueB= $value eq "undef"
ABresult
0X0
100
111
162 7 $self->{_attributes}{$att}{value} = $value;
163 7 ▶ if ($sprintf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sprintf) { } 4 3 7
164 4 $self->{_attributes}{$att}{sprintf} = $sprintf;
165 4 ($length) = ($sprintf =~ /(\d+)/g);
166 } else {
167 3 $self->{_attributes}{$att}{reader} = $reader;
168 }
169 7 $self->{_attributes}{$att}{length} = $length;
170 7 $self->{_attributes}{$att}{writer} = $writer;
171 7 push @{$self->{_attribute_order}}, $att;
172  
173 7 return 1;
174 }
175  
176  
177 =head2 parse
178  
179 Parses the string you hand in. Sets each attribute to the value it finds in the string.
180  
181 $fw->parse(string => ' JayHannah 0003');
182  
183 =cut
184  
185 11 ▶sub parse {
Subroutine parse: called CC=5 SCAR=16.1
Pod documented
186 11 my ($self, %args) = @_;
187  
188 11 ▶ die ref($self).":Please provide a string argument" if (!$args{string});
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"string"} 0 11 11
189 11 my $string = $args{string};
190  
191 11 ▶ $self = $self->clone if $args{clone};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"clone"} 10 1 11
192  
193 11 my $offset = 0;
194 11 foreach (@{$self->{_attribute_order}}) {
195 44 my $length = $self->{_attributes}{$_}{length};
196 44 $self->{_attributes}{$_}{value} = substr $string, $offset, $length;
197 44 $offset += $length;
198 }
199  
200 11 ▶ return $args{clone}? $self : 1;
Branch: 1 branch
BranchTrueFalseTotal
$args{'clone'} ? : 10 1 11
201 }
202  
203  
204 =head2 string
205  
206 Dump the object to a string. Walks each attribute in order and outputs each in the
207 format that was specified during set_attributes().
208  
209 print $fw->string; # ' ChuckNorris 0017'
210  
211 =cut
212  
213 7 ▶sub string {
Subroutine string: called CC=2 SCAR=6.9
Pod documented
214 7 my ($self) = @_;
215 7 my $rval;
216 7 foreach my $att (@{$self->{_attribute_order}}) {
217 28 $rval .= $self->_getf($att);
218 }
219 7 return $rval;
220 }
221  
222  
223 =head2 getf_*
224  
225 For the 'foo' attribute, we provide the getter get_foo() per the SYNOPSIS above.
226 But we also provide getf_foo(). get_* returns the current value in no particular format,
227 while getf_* returns the fixed-width formatted value.
228  
229 $fw->get_fname; # Jay (no particular format)
230 $fw->getf_fname; # ' Jay' (the format you specified)
231  
232 =cut
233  
234 41 ▶sub _getf {
Subroutine _getf: called CC=14 SCAR=28.7
235 41 my ($self, $att) = @_;
236  
237 41 my $value = $self->{_attributes}{$att}{value};
238 41 my $length = $self->{_attributes}{$att}{length};
239 41 my $sprintf = $self->{_attributes}{$att}{sprintf};
240 41 my $reader = $self->{_attributes}{$att}{reader};
241 41 ▶ if ($reader) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader) 3 38 41
242 3 my $rval = $reader->($self);
243 3 ▶ if (length($rval) != $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rval != $length) 0 3 3
244 0 die "string() error: " . ref($self) . " is loaded with a 'reader' which returned a string of length " . length($rval) . ", but 'length' was set to $length. Please correct the class. The error occured on attribute '$att' converting value '$value' to '$rval'";
245 }
246 3 return $rval;
247 }
248  
249 38 ▶ if (defined ($value) and length($value) > $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and length $value > $length) 0 38 38
Condition66%2/3
defined $value and length $value > $length
!ll&&!rl&&r
10280
MC/DC0%0/2
defined $value and length $value > $length
defined $value length $value > $length
Truth table66%2/3
A and B
A= defined $valueB= length $value > $length
ABresult
0X0
100
111
250 0 warn "string() error! " . ref($self) . " length of attribute '$att' cannot exceed '$length', but it does. Please shorten the value '$value'";
251 0 return 0;
252 }
253 38 ▶ if (not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 10 28 38
254 10 $value = '';
255 }
256 38 ▶ unless ($sprintf) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sprintf) 0 38 38
257 0 warn "string() error! " . ref($self) . " sprintf not set on attribute $att. Using '%s'";
258 0 $sprintf = '%s';
259 }
260  
261 38 my $rval;
262 38 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($sprintf =~ /\%\d*[duoxefgXEGbB]/ and !defined($value) || $value eq "" || !($value =~ /^(\d+\.?\d*|\.\d+)$/)) 3 35 38
Condition77%7/9
$sprintf =~ /\%\d*[duoxefgXEGbB]/ and !defined($value) || $value eq "" || !($value =~ /^(\d+\.?\d*|\.\d+)$/)
!ll&&!rl&&r
2963
not defined $value or $value eq "" or not $value =~ /^(\d+\.?\d*|\.\d+)$/
l!l&&r!l&&!r
306
not defined $value or $value eq ""
l!l&&r!l&&!r
036
MC/DC50%2/4
$sprintf =~ /\%\d*[duoxefgXEGbB]/ and !defined($value) || $value eq "" || !($value =~ /^(\d+\.?\d*|\.\d+)$/)
$sprintf =~ /\%\d*[duoxefgXEGbB]/ not defined $value $value eq "" not $value =~ /^(\d+\.?\d*|\.\d+)$/
Truth table60%3/5
A and not(B) or C or not(D)
A= $sprintf =~ /\%\d*[duoxefgXEGbB]/B= not defined $valueC= $value eq ""D= not $value =~ /^(\d+\.?\d*|\.\d+)$/
ABCDresult
0XXX0
10000
11XX1
101X1
10011
263 $sprintf =~ /\%\d*[duoxefgXEGbB]/ && ( # perldoc -f sprintf
264 (not defined $value) ||
265 $value eq "" ||
266 $value !~ /^(\d+\.?\d*|\.\d+)$/ # match valid number
267 )
268 ) {
269 3 ▶ $value = '' if (not defined $value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 3 3
270 3 warn "string() warning: " . ref($self) . " attribute '$att' contains '$value' which is not numeric, yet the sprintf '$sprintf' appears to be numeric. Using 0";
271 3 $value = 0;
272 }
273 38 ▶ $rval = sprintf($sprintf, (defined $value ? $value : ""));
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 38 0 38
274  
275 38 ▶ if (length($rval) != $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rval != $length) 0 38 38
276 0 die "string() error: " . ref($self) . " is loaded with an sprintf format which returns a string that is NOT the correct length. Please correct the class. The error occured on attribute '$att' converting value '$value' via sprintf '$sprintf', which is '$rval', which is not '$length' characters long";
277 }
278  
279 38 return $rval;
280 }
281  
282  
283 =head2 auto_truncate
284  
285 Text::FixedWidth can automatically truncate long values for you. Use this method to tell your $fw
286 object which attributes should behave this way.
287  
288 $fw->auto_truncate("fname", "lname");
289  
290 (The default behavior if you pass in a value that is too long is to carp out a warning,
291 ignore your set(), and return undef.)
292  
293 =cut
294  
295 2 ▶sub auto_truncate {
Subroutine auto_truncate: called CC=3 SCAR=11.0
Pod documented
296 2 my ($self, @attrs) = @_;
297 2 $self->{_auto_truncate} = {};
298 2 foreach my $attr (@attrs) {
299 3 ▶ unless ($self->{_attributes}{$attr}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_attributes"}{$attr}) 1 2 3
300 1 carp "Can't auto_truncate attribute '$attr' because that attribute does not exist";
301 1 next;
302 }
303 2 $self->{_auto_truncate}->{$attr} = 1;
304 }
305 2 return 1;
306 }
307  
308 =head2 clone
309  
310 Provides a clone of a Text::FixedWidth object. If available it will attempt
311 to use L<Clone::Fast> or L<Clone::More> falling back on L<Storable/dclone>.
312  
313 my $fw_copy = $fw->clone;
314  
315 This method is most useful when being called from with in the L</parse> method.
316  
317 while( my $row = $fw->parse( clone => 1, string => $str ) ) {
318 print $row->foobar;
319 }
320  
321 See L</parse> for further information.
322  
323 =cut
324  
325 11 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
326 11 my $self = shift;
327 11 return Storable::dclone($self);
328 }
329  
330  
331  
332  
333 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
334  
335 # Using Damian methodology so I don't need to require Moose.
336 # Object Oriented Perl (1st edition)
337 # Damian Conway
338 # Release date 15 Aug 1999
339 # Publisher Manning Publications
340 40 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=13.9
341 9 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
342 40 ▶ if ($AUTOLOAD =~ /.*::get_(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::FixedWidth::AUTOLOAD =~ /.*::get_(\w+)/) 17 23 40
343 17 my $att = $1;
344 17 ▶ *{$AUTOLOAD} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
345 55 $_[0]->_get($att);
346 17 };
347 17 return &{$AUTOLOAD};
348 }
349  
350 23 ▶ if ($AUTOLOAD =~ /.*::getf_(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::FixedWidth::AUTOLOAD =~ /.*::getf_(\w+)/) 13 10 23
351 13 my $att = $1;
352 13 ▶ *{$AUTOLOAD} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
353 13 $_[0]->_getf($att);
354 13 };
355 13 return &{$AUTOLOAD};
356 }
357  
358 10 ▶ if ($AUTOLOAD =~ /.*::set_(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::FixedWidth::AUTOLOAD =~ /.*::set_(\w+)/) 10 0 10
359 10 my $att = $1;
360 10 ▶ *{$AUTOLOAD} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
361 17 $_[0]->_set($att, $_[1]);
362 10 };
363 10 return &{$AUTOLOAD};
364 }
365  
366 0 confess ref($_[0]).":No such method: $AUTOLOAD";
367 }
368  
369  
370 55 ▶sub _get {
Subroutine _get: called CC=4 SCAR=13.9
371 55 my ($self, $att) = @_;
372 55 ▶ croak "Can't get_$att(). No such attribute: $att" unless (defined $self->{_attributes}{$att});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_attributes"}{$att} 0 55 55
373 55 my $ret = $self->{_attributes}{$att}{value};
374 55 ▶ $ret =~ s/\s+$// if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 33 22 55
375 55 ▶ $ret =~ s/^\s+// if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 33 22 55
376 55 return $ret;
377 }
378  
379  
380 17 ▶sub _set {
Subroutine _set: called CC=7 SCAR=19.5
381 17 my ($self, $att, $val) = @_;
382  
383 17 my $length = $self->{_attributes}{$att}{length};
384 17 my $writer = $self->{_attributes}{$att}{writer};
385  
386 17 ▶ croak "Can't set_$att(). No such attribute: $att" unless (defined $self->{_attributes}{$att});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_attributes"}{$att} 0 17 17
387 17 ▶ if (defined $self->{_attributes}{$att}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_attributes'}{$att}) { } 17 0 17
388 17 ▶ if ($writer) {
Branch: 2 branches
BranchTrueFalseTotal
if ($writer) { } 3 14 17
elsif (defined $val and length $val > $length) { } 3 14 17
389 3 $val = $writer->($self, $val);
390 ▶ } elsif (defined $val && length($val) > $length) {
Condition100%3/3
defined $val and length $val > $length
!ll&&!rl&&r
383
MC/DC100%2/2
defined $val and length $val > $length
defined $val length $val > $length
Truth table100%3/3
A and B
A= defined $valB= length $val > $length
ABresult
0X0
100
111
391 3 ▶ if ($self->{_auto_truncate}{$att}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_auto_truncate'}{$att}) { } 2 1 3
392 2 $val = substr($val, 0, $length);
393 2 $self->{_attributes}{$att}{value} = $val;
394 } else {
395 1 carp "Can't set_$att('$val'). Value must be $length characters or shorter";
396 1 return undef;
397 }
398 }
399 16 $self->{_attributes}{$att}{value} = $val;
400 16 return 1;
401 } else {
402 0 return 0;
403 }
404 }
405  
406  
407 =head1 ALTERNATIVES
408  
409 Other modules that may do similar things:
410 L<Parse::FixedLength>,
411 L<Text::FixedLength>,
412 L<Data::FixedFormat>,
413 L<AnyData::Format::Fixed>
414  
415 =head1 AUTHOR
416  
417 Jay Hannah, C<< <jay at jays.net> >>, http://jays.net
418  
419 =head1 BUGS
420  
421 Please report any bugs or feature requests to C<bug-text-fixedwidth at rt.cpan.org>, or through
422 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-FixedWidth>. I will be notified, and then you'll
423 automatically be notified of progress on your bug as I make changes.
424  
425 =head1 SUPPORT
426  
427 You can find documentation for this module with the perldoc command.
428  
429 perldoc Text::FixedWidth
430  
431 You can also look for information at:
432  
433 =over 4
434  
435 =item * MetaCPAN
436  
437 L<https://metacpan.org/release/Text-FixedWidth>
438  
439 =item * RT: CPAN's request tracker
440  
441 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Text-FixedWidth>
442  
443 =item * Source code
444  
445 L<http://github.com/jhannah/text-fixedwidth>
446  
447 =item * AnnoCPAN: Annotated CPAN documentation
448  
449 L<http://annocpan.org/dist/Text-FixedWidth>
450  
451 =back
452  
453  
454 =head1 COPYRIGHT & LICENSE
455  
456 Copyright 2008-2013 Jay Hannah, all rights reserved.
457  
458 This program is free software; you can redistribute it and/or modify it
459 under the same terms as Perl itself.
460  
461  
462 =cut
463  
464 9 1; # End of Text::FixedWidth
↑ Summary