blib/lib/Parse/FixedLength.pm

filter: Statementstmt 85.3% 268 / 314 Branchbran 68.8% 128 / 186 Conditioncond 48.3% 43 / 89 MC/DCmcdc 12.2% 5 / 41 Subroutinesub 80.5% 29 / 36 Podpod 100% 12 / 12 totaltotal 71.5% 485 / 678 CC 139 SCAR 33.2
CC
139
Cov
75%
_parse_format
82.8
dumper
56.0
new
28.3
CRAP 27.8
↑ Summary
1 package Parse::FixedLength;
2 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4  
5 #-----------------------------------------------------------------------
6 # Public Global Variables
7 #-----------------------------------------------------------------------
8 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use vars qw($VERSION $DELIM $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 $VERSION = '5.39';
11 17 $DELIM = ":";
12 17 $DEBUG = 0;
13  
14 #=======================================================================
15 21 ▶sub import {
Subroutine import: called CC=5 SCAR=16.4
16 21 my $proto = shift;
17 21 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0210
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
18 21 for (@_) {
19 4 ▶ $class->import($class->_all_modules()), last if $_ eq ':all';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq ":all" 1 3 4
20 3 ▶ eval "use ${class}::$_";
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶ confess $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
22 }
23 }
24  
25 1 ▶sub _all_modules {
Subroutine _all_modules: called CC=7 SCAR=19.5
26 1 my $self = shift;
27 1 ▶ eval "use File::Spec";
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶ confess $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
29 1 my %modules;
30 1 for my $dir (@INC) {
31 9 my $pfl_dir = File::Spec->catdir($dir, 'Parse', 'FixedLength');
32 9 ▶ next unless -d $pfl_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $pfl_dir 8 1 9
33 1 ▶ opendir(DIR, $pfl_dir) or confess "Can't read $pfl_dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $pfl_dir or confess("Can't read ${pfl_dir}: $!") 1 0 1
34 1 for (readdir DIR) {
35 3 my $module = $_;
36 3 ▶ next unless $module =~ s/\.pm$//;
Branch: 1 branch
BranchTrueFalseTotal
unless $module =~ s/\.pm$// 2 1 3
37 1 $modules{$module} = undef;
38 }
39 1 closedir DIR;
40 }
41 1 keys %modules;
42 }
43  
44 27 ▶sub new {
Subroutine new: called CC=24 SCAR=33.4
Pod documented
45 # Do the cargo cult OO construction thing
46 27 my $proto = shift;
47 27 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0270
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
48 27 my $format = shift;
49 27 ▶ unless (ref $format) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $format) 1 26 27
50 1 my $newclass = "${class}::${format}";
51 1 my $result = eval { $newclass->new(@_)};
52 1 ▶ return $result unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 1 1
53 1 ▶ confess $@ unless $@ =~ /Can't locate object method/;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /Can't locate object method/ 0 1 1
54 # Assume we need to require this format
55 1 $class->import($format);
56 1 return $newclass->new(@_);
57 }
58 26 ▶ confess "Format argument not an array ref"
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($format, "ARRAY") 0 26 26
59 unless UNIVERSAL::isa($format, 'ARRAY');
60 26 my $self = bless {}, $class;
61 26 ▶ my $params = shift || {};
Condition100%2/2
shift() || {}
l!l
1412
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
62 26 ▶ confess "Params argument not a hash ref"
Branch: 1 branch
BranchTrueFalseTotal
if defined $params and not UNIVERSAL::isa($params, "HASH") 0 26 26
Condition33%1/3
defined $params and not UNIVERSAL::isa($params, "HASH")
!ll&&!rl&&r
0260
MC/DC0%0/2
defined $params and not UNIVERSAL::isa($params, "HASH")
defined $params not UNIVERSAL::isa($params, "HASH")
Truth table33%1/3
A and not(B)
A= defined $paramsB= not UNIVERSAL::isa($params, "HASH")
ABresult
0X0
100
111
63 if defined $params and ! UNIVERSAL::isa($params, 'HASH');
64 26 ▶ my $delim = exists $params->{'delim'} ? $params->{'delim'} : $DELIM;
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{'delim'} ? : 4 22 26
65 26 $self->{DELIM} = $delim;
66 26 my $delim_re = qr/\Q$delim/;
67 26 ▶ confess "Delimiter argument must be one character" unless length($delim)==1;
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length $delim == 1 0 26 26
68 26 ▶ if (exists $$params{all_lengths}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"all_lengths"}) 1 25 26
69 1 my $all = $$params{all_lengths};
70 1 ▶ confess "all_lengths must be a positive integer"
Branch: 1 branch
BranchTrueFalseTotal
unless $all and $all =~ /^\d+$/ and $all > 0 0 1 1
Condition33%2/6
$all and $all =~ /^\d+$/ and $all > 0
!ll&&!rl&&r
001
$all and $all =~ /^\d+$/
!ll&&!rl&&r
001
MC/DC0%0/3
$all and $all =~ /^\d+$/ and $all > 0
$all $all =~ /^\d+$/ $all > 0
Truth table25%1/4
A and B and C
A= $allB= $all =~ /^\d+$/C= $all > 0
ABCresult
0XX0
10X0
1100
1111
71 unless $all and $all =~ /^\d+$/ and $all > 0;
72 1 $format = [ map { local $_=$_; s/$delim_re.*//;
73 4 "${_}${delim}$$params{all_lengths}"
74 } @$format ];
75 }
76 26 ▶ my $spaces = $params->{'spaces'} ? 'a' : 'A';
Branch: 1 branch
BranchTrueFalseTotal
$params->{'spaces'} ? : 0 26 26
77 26 my $is_hsh = $self->{IS_HSH} = _chk_format_type($format, $delim_re);
78  
79 # Convert hash-like array to delimited array
80 68 $format = [ map { $$format[$_].$delim.$$format[$_+1] }
81 26 ▶ grep { not $_ % 2 } 0..$#$format
Branch: 1 branch
BranchTrueFalseTotal
if $is_hsh 12 14 26
82 ] if $is_hsh;
83 26 my ($names, $alengths, $hlengths, $justify, $length, $fmts) =
84 _parse_format($format, $delim_re, $params);
85  
86 22 $self->{NAMES} = $names;
87 22 $self->{UNPACK} = join '', @{$fmts}{@$names};
88 22 ( $self->{PACK} = $self->{UNPACK} ) =~ tr/a/A/;
89 22 $self->{LENGTH} = $length;
90 22 @$self{qw(TFIELDS TNAMES)} = ([], []);
91 # Save justify fields no matter what for benefit of dumper()
92 22 ▶ if (%$justify) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$justify) 13 9 22
93 13 $self->{JFIELDS} = $justify;
94 13 ▶ $self->{JUST} = 1 unless $$params{no_justify};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"no_justify"} 13 0 13
95 13 @$self{qw(TFIELDS TNAMES TPAD)} = _trim_info($self);
96 13 ▶ $self->{TRIM} = 1 if $$params{trim};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"trim"} 1 12 13
97 }
98 22 $self->{LENGTHS} = $hlengths;
99 22 $self->{FMTS} = $fmts;
100 $self->{DEBUG} = exists $$params{'debug'} ?
101 22 ▶ ref($$params{'debug'}) ? $$params{'debug'} : \*STDOUT : $DEBUG;
Branch: 2 branches
BranchTrueFalseTotal
exists $params->{'debug'} ? : 0 22 22
ref $params->{'debug'} ? : 0 0 0
102  
103 # Make slot to parse data into
104 22 ▶ my $ref = $params->{href} || {};
Condition100%2/2
$params->{'href'} || {}
l!l
121
MC/DC100%1/1
$params->{'href'} || {}
$params->{'href'}
Truth table100%2/2
A
A= $params->{'href'}
Aresult
11
00
105 22 @$ref{@{$self->names}} = undef;
106 22 $self->{DATA} = $ref;
107 22 ▶ $self->hash_to_obj($self->{DATA}) unless $params->{no_bless};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"no_bless"} 22 0 22
108  
109 22 $self;
110 }
111  
112 # Determine which format we have, the delimited array ref
113 # or the hash-like array ref.
114 # There must be delimiters in either all of the elements or none in
115 # alternating elements with an even number of elements.
116 # Assume what we have from the first element.
117 26 ▶sub _chk_format_type {
Subroutine _chk_format_type: called CC=9 SCAR=22.7
118 26 my ($format, $delim) = @_;
119 26 ▶ my $is_hsh = 1 unless $$format[0] =~ $delim;
Branch: 1 branch
BranchTrueFalseTotal
unless $format->[0] =~ /$delim/ 12 14 26
120 26 ▶ confess"Odd number of name/length pairs or missing delimiter on first field"
Branch: 1 branch
BranchTrueFalseTotal
if $is_hsh and @$format % 2 0 26 26
Condition66%2/3
$is_hsh and @$format % 2
!ll&&!rl&&r
14120
MC/DC0%0/2
$is_hsh and @$format % 2
$is_hsh @$format % 2
Truth table66%2/3
A and B
A= $is_hshB= @$format % 2
ABresult
0X0
100
111
121 if $is_hsh and @$format % 2;
122 26 for my $i (0..$#$format) {
123 186 my $field = $$format[$i];
124 186 ▶ if ($field =~ $delim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /$delim/) { } 98 88 186
125 98 ▶ confess "Field $field contains delimiter" if $is_hsh and not $i % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $is_hsh and not $i % 2 0 98 98
Condition66%2/3
$is_hsh and not $i % 2
!ll&&!rl&&r
50480
MC/DC0%0/2
$is_hsh and not $i % 2
$is_hsh not $i % 2
Truth table66%2/3
A and not(B)
A= $is_hshB= not $i % 2
ABresult
0X0
100
111
126 88 ▶ } else { confess "Field $field is missing delimiter" unless $is_hsh }
Branch: 1 branch
BranchTrueFalseTotal
unless $is_hsh 0 88 88
127 }
128 26 return $is_hsh;
129 }
130  
131 26 ▶sub _parse_format {
Subroutine _parse_format: called CC=36 SCAR=44.2
132 26 my ($format, $delim, $params) = @_;
133 26 my (@names, @lengths, %lengths, %justify, %dups, %fmts);
134 26 my $dups_ok = $$params{autonum};
135 26 my $all_dups_ok;
136 26 ▶ if ($dups_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dups_ok) 6 20 26
137 6 ▶ if (UNIVERSAL::isa($dups_ok, 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($dups_ok, 'ARRAY')) { } 5 1 6
138 5 @dups{@$dups_ok} = undef;
139 1 } else { $all_dups_ok = 1 }
140 }
141 26 my $length = 0;
142 26 my $nxt = 1;
143 26 for (@$format) {
144 111 my ($name, $tmp_len, $start, $end) = split $delim;
145 111 ▶ _chk_start_end($name, $nxt, $start, $end) unless $$params{no_validate};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"no_validate"} 105 6 111
146 110 $name = _chk_dups(
147 $name, \@names, \%fmts,
148 \%lengths, \%justify, \%dups, $dups_ok, $all_dups_ok
149 );
150 108 push @names, $name;
151 # The results of the inner-parens is not guaranteed unless the
152 # outer parens match, so we do it this way
153 108 ▶ if ( $tmp_len =~ /^\d/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tmp_len =~ /^\d/) { } 106 2 108
elsif ($tmp_len =~ /^(\w)((?:\d+)?)$/) { } 2 0 2
154 106 ▶ my ($len, $is_just, $chr) = $tmp_len =~ /^(\d+)((?:R(.?))?)$/
Branch: 1 branch
BranchTrueFalseTotal
() = $tmp_len =~ /^(\d+)((?:R(.?))?)$/ or confess("Bad length $tmp_len for field $name") 106 0 106
155 or confess "Bad length $tmp_len for field $name";
156 106 ▶ $len > 0 or confess "Length must be > 0 for field $name";
Branch: 1 branch
BranchTrueFalseTotal
$len > 0 or confess("Length must be > 0 for field $name") 106 0 106
157 106 ▶ unless ( $$params{no_validate} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{"no_validate"}) 100 6 106
158 100 ▶ if (defined $end) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $end) 35 65 100
159 35 ▶ confess "Bad length or end for field $name"
Branch: 1 branch
BranchTrueFalseTotal
unless $end == $start + $len - 1 1 34 35
160 unless $end == $start + $len - 1;
161 }
162 }
163 105 ▶ $justify{$name} = ($chr eq '') ? ' ' : $chr if $is_just;
Branch: 2 branches
BranchTrueFalseTotal
if $is_just 14 91 105
$chr eq '' ? : 8 6 14
164 105 $lengths{$name} = $len;
165 105 push @lengths, $len;
166 105 $length += $len;
167 105 ▶ $nxt = $end + 1 if defined $end;
Branch: 1 branch
BranchTrueFalseTotal
if defined $end 40 65 105
168 105 ▶ $fmts{$name} = ( $params->{spaces} ? "a" : "A" ) . $len;
Branch: 1 branch
BranchTrueFalseTotal
$params->{'spaces'} ? : 0 105 105
169 } elsif ( $tmp_len =~ /^(\w)((?:\d+)?)$/ ) {
170 2 my ($type, $repeat) = ($1, $2);
171 2 ▶ my $len = $type =~ /[AaZ]/ && $repeat
Condition40%15/37
$type =~ /[AaZ]/ && $repeat || $type =~ /b/i && int $repeat / 16 - 0.01 + 1 || $type =~ /h/i && int $repeat / 2 - 0.01 + 1 || $type =~ /c/i && 1 || $type =~ /[sSnv]/ && 2 || $type =~ /[lLNV]/ && 4 || $type =~ /q/i && 8 || undef
l!l
20
$type =~ /[AaZ]/ and $repeat or $type =~ /b/i and int $repeat / 16 - "0.01" + 1 or $type =~ /h/i and int $repeat / 2 - "0.01" + 1 or $type =~ /c/i and 1 or $type =~ /[sSnv]/ and 2 or $type =~ /[lLNV]/ and 4 or $type =~ /q/i and 8
l!l&&r!l&&!r
200
$type =~ /[AaZ]/ and $repeat or $type =~ /b/i and int $repeat / 16 - "0.01" + 1 or $type =~ /h/i and int $repeat / 2 - "0.01" + 1 or $type =~ /c/i and 1 or $type =~ /[sSnv]/ and 2 or $type =~ /[lLNV]/ and 4
l!l&&r!l&&!r
200
$type =~ /[AaZ]/ and $repeat or $type =~ /b/i and int $repeat / 16 - "0.01" + 1 or $type =~ /h/i and int $repeat / 2 - "0.01" + 1 or $type =~ /c/i and 1 or $type =~ /[sSnv]/ and 2
l!l&&r!l&&!r
200
$type =~ /[AaZ]/ and $repeat or $type =~ /b/i and int $repeat / 16 - "0.01" + 1 or $type =~ /h/i and int $repeat / 2 - "0.01" + 1 or $type =~ /c/i and 1
l!l&&r!l&&!r
110
$type =~ /[AaZ]/ and $repeat or $type =~ /b/i and int $repeat / 16 - "0.01" + 1 or $type =~ /h/i and int $repeat / 2 - "0.01" + 1
l!l&&r!l&&!r
101
$type =~ /[AaZ]/ and $repeat or $type =~ /b/i and int $repeat / 16 - "0.01" + 1
l!l&&r!l&&!r
011
$type =~ /[AaZ]/ and $repeat
!ll&&!rl&&r
200
$type =~ /b/i and int $repeat / 16 - "0.01" + 1
!ll&&!rl&&r
101
$type =~ /h/i and int $repeat / 2 - "0.01" + 1
!ll&&!rl&&r
100
$type =~ /c/i and 1
!ll
01
$type =~ /[sSnv]/ and 2
!ll
00
$type =~ /[lLNV]/ and 4
!ll
00
$type =~ /q/i and 8
!ll
00
MC/DC0%0/10
$type =~ /[AaZ]/ && $repeat || $type =~ /b/i && int $repeat / 16 - 0.01 + 1 || $type =~ /h/i && int $repeat / 2 - 0.01 + 1 || $type =~ /c/i && 1 || $type =~ /[sSnv]/ && 2 || $type =~ /[lLNV]/ && 4 || $type =~ /q/i && 8 || undef
$type =~ /[AaZ]/ $repeat $type =~ /b/i int $repeat / 16 - "0.01" + 1 $type =~ /h/i int $repeat / 2 - "0.01" + 1 $type =~ /c/i $type =~ /[sSnv]/ $type =~ /[lLNV]/ $type =~ /q/i
Truth table4%2/47
A and B or C and D or E and F or G or H or I or J
A= $type =~ /[AaZ]/B= $repeatC= $type =~ /b/iD= int $repeat / 16 - "0.01" + 1E= $type =~ /h/iF= int $repeat / 2 - "0.01" + 1G= $type =~ /c/iH= $type =~ /[sSnv]/I= $type =~ /[lLNV]/J= $type =~ /q/i
ABCDEFGHIJresult
11XXXXXXXX1
0X11XXXXXX1
1011XXXXXX1
0X0X11XXXX1
0X1011XXXX1
100X11XXXX1
101011XXXX1
0X0X0X1XXX1
0X0X101XXX1
0X100X1XXX1
0X10101XXX1
100X0X1XXX1
100X101XXX1
10100X1XXX1
1010101XXX1
0X0X0X01XX1
0X0X1001XX1
0X100X01XX1
0X101001XX1
100X0X01XX1
100X1001XX1
10100X01XX1
10101001XX1
0X0X0X001X1
0X0X10001X1
0X100X001X1
0X1010001X1
100X0X001X1
100X10001X1
10100X001X1
101010001X1
0X0X0X00011
0X0X1000011
0X100X00011
0X101000011
100X0X00011
100X1000011
10100X00011
10101000011
0X0X0X00000
0X0X1000000
0X100X00000
0X101000000
100X0X00000
100X1000000
10100X00000
10101000000
172 || $type =~ /b/i && int((($repeat/16)-.01) + 1)
173 || $type =~ /h/i && int((($repeat/2)-.01) + 1)
174 || $type =~ /c/i && 1
175 || $type =~ /[sSnv]/ && 2
176 || $type =~ /[lLNV]/ && 4
177 || $type =~ /q/i && 8
178 || undef;
179 2 ▶ unless ( $$params{no_validate} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{"no_validate"}) 2 0 2
180 2 ▶ if (defined $end) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $end) 2 0 2
181 2 ▶ confess "Bad length or end for field $name"
Branch: 1 branch
BranchTrueFalseTotal
unless $end == $start + $len - 1 0 2 2
182 unless $end == $start + $len - 1;
183 }
184 }
185 2 $length += $len;
186 2 $lengths{$name} = $len;
187 2 push @lengths, $len;
188 2 ▶ $nxt = $end + 1 if defined $end;
Branch: 1 branch
BranchTrueFalseTotal
if defined $end 2 0 2
189 2 $fmts{$name} = $tmp_len;
190 } else {
191 0 ▶ unless ( eval {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { my(@foo) = unpack($tmp_len, "junk"); die "Too Many fields" unless @foo == 1; 1 } }) 0 0 0
192 0 my @foo = unpack($tmp_len, "junk");
193 0 ▶ die "Too Many fields" unless @foo == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @foo == 1 0 0 0
194 0 1;
195 })
196 {
197 0 confess "Bad format $tmp_len for field $name";
198 }
199 0 $fmts{$name} = $tmp_len;
200 }
201 }
202 22 return \@names, \@lengths, \%lengths, \%justify, $length, \%fmts;
203 }
204  
205 # Check for duplicate field name, and if a duplicate,
206 # either die or return new autonumbered field name
207 110 ▶sub _chk_dups {
Subroutine _chk_dups: called CC=11 SCAR=24.1
208 110 my ($name, $names, $fmts, $lengths,
209 $justify, $dups, $dups_ok, $all_dups_ok) = @_;
210 110 ▶ if (exists $$lengths{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $lengths->{$name}) { } 8 102 110
211 confess "Duplicate field $name in format"
212 8 ▶ if !$dups_ok or !$all_dups_ok && !exists $$dups{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $dups_ok and $all_dups_ok || exists $dups->{$name} 2 6 8
Condition100%6/6
$dups_ok and $all_dups_ok || exists $dups->{$name}
!ll&&!rl&&r
116
$all_dups_ok or exists $dups->{$name}
l!l&&r!l&&!r
151
MC/DC100%3/3
$dups_ok and $all_dups_ok || exists $dups->{$name}
$dups_ok $all_dups_ok exists $dups->{$name}
Truth table100%4/4
A and B or C
A= $dups_okB= $all_dups_okC= exists $dups->{$name}
ABCresult
0XX0
1000
11X1
1011
213 102 ▶ } else { return $name unless $$dups{$name} }
Branch: 1 branch
BranchTrueFalseTotal
unless $dups->{$name} 100 2 102
214 # If this is the first duplicate found, fix the previous field
215 8 ▶ unless ($$dups{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dups->{$name}) 6 2 8
216 6 my $new_name = "${name}_".++$$dups{$name};
217 6 ▶ confess "Can't autonumber field $name" if exists $$lengths{$new_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $lengths->{$new_name} 0 6 6
218 6 ▶ for (@$names) { $_ = $new_name if $_ eq $name }
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $name 6 12 18
219 6 $$lengths{$new_name} = $$lengths{$name};
220 6 delete $$lengths{$name};
221 6 $$fmts{$new_name} = $$fmts{$name};
222 6 delete $$fmts{$name};
223 6 ▶ if (exists $$justify{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $justify->{$name}) 0 6 6
224 0 $$justify{$new_name} = $$justify{$name};
225 0 delete $$justify{$name};
226 }
227 }
228 8 return "${name}_".++$$dups{$name};
229 }
230  
231 105 ▶sub _chk_start_end {
Subroutine _chk_start_end: called CC=7 SCAR=20.1
232 105 my ($name, $prev, $start, $end) = @_;
233 105 ▶ if (defined $start) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start) 40 65 105
234 40 ▶ $start=~/^\d+$/ or confess "Start position not a number in field $name";
Branch: 1 branch
BranchTrueFalseTotal
$start =~ /^\d+$/ or confess("Start position not a number in field $name") 40 0 40
235 40 ▶ $start == $prev or confess "Bad start position in field $name";
Branch: 1 branch
BranchTrueFalseTotal
$start == $prev or confess("Bad start position in field $name") 39 1 40
236 39 ▶ defined $end or confess "End position missing in field $name";
Branch: 1 branch
BranchTrueFalseTotal
defined $end or confess("End position missing in field $name") 39 0 39
237 39 ▶ $end =~ /^\d+$/ or confess "End position not a number in field $name";
Branch: 1 branch
BranchTrueFalseTotal
$end =~ /^\d+$/ or confess("End position not a number in field $name") 39 0 39
238 39 ▶ $end < $start and confess "End position < start in field $name";
Branch: 1 branch
BranchTrueFalseTotal
$end < $start and confess("End position < start in field $name") 0 39 39
239 }
240 }
241  
242 13 ▶sub _trim_info {
Subroutine _trim_info: called CC=3 SCAR=11.0
243 13 my $parser = shift;
244 13 my (@tfields, @tnames, @tpad);
245 13 my $i = 0;
246 13 for my $name (@{$parser->names}) {
247 61 ▶ if (exists $parser->{JFIELDS}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parser->{"JFIELDS"}{$name}) 13 48 61
248 13 push @tfields, $i;
249 13 push @tnames, $name;
250 13 push @tpad, qr/^\Q$parser->{JFIELDS}{$name}\E+/;
251 }
252 61 } continue { $i++ }
253 13 return \@tfields, \@tnames, \@tpad;
254 }
255  
256 #=======================================================================
257 18 ▶sub parse {
Subroutine parse: called CC=5 SCAR=17.8
Pod documented
258 18 my $parser = shift;
259 18 my $data = $parser->{DATA};
260 18 my $names = $parser->{NAMES};
261 18 @{$data}{@$names} = unpack($parser->{UNPACK}, $_[0]);
262 18 ▶ $parser->trim($data) if $parser->{TRIM};
Branch: 1 branch
BranchTrueFalseTotal
if $parser->{"TRIM"} 1 17 18
263 18 ▶ if (my $fh = $parser->{DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $parser->{"DEBUG"}) 0 18 18
264 0 print $fh "# Debug parse\n";
265 0 for my $name (@$names) {
266 0 print $fh "[$name][$data->{$name}]\n";
267 }
268 0 print $fh "\n";
269 }
270 18 ▶ wantarray ? @$data{@$names} : $data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 18 18
271 }
272 #=======================================================================
273 0 ▶sub parse_hash {
Subroutine parse_hash: not called CC=1 SCAR=6.9
Pod documented
274 0 return %{ scalar(shift->parse(@_)) };
275 }
276 0 ▶sub parse_newref {
Subroutine parse_newref: not called CC=1 SCAR=6.9
Pod documented
277 0 return { shift->parse_hash(@_) };
278 }
279 #=======================================================================
280 7 ▶sub pack {
Subroutine pack: called CC=7 SCAR=21.5
Pod documented
281 7 my $parser = shift;
282 7 ▶ my $href = shift || $parser->{DATA};
Condition33%1/3
shift() || $parser->{'DATA'}
l!l&&r!l&&!r
700
MC/DC0%0/2
shift() || $parser->{'DATA'}
shift() $parser->{'DATA'}
Truth table33%1/3
A || B
A= shift()B= $parser->{'DATA'}
ABresult
1X1
011
000
283 7 ▶ if ($parser->{JUST}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser->{"JUST"}) 7 0 7
284 7 ▶ while (my ($name, $chr) = each %{$parser->{JFIELDS}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$parser{"JFIELDS"};}) 7 7 14
285 7 (my $field = $$href{$name}) =~ s/^\s+|\s+$//g;
286 7 ▶ $field =~ s/^${chr}+// if $chr ne ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $chr ne " " 5 2 7
287 7 my $len = $parser->length($name);
288 # Should we warn if we're truncating the field?
289 7 $$href{$name} = substr(($chr x $len) . $field, -$len);
290 }
291 }
292 # Print debug output after justifying fields
293 7 ▶ if ($parser->{DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser->{"DEBUG"}) 0 7 7
294 0 print "# Debug pack\n";
295 0 for my $name (@{$parser->{NAMES}}) {
296 0 print "[$name][$$href{$name}]\n";
297 }
298 0 print "\n";
299 }
300 7 CORE::pack $parser->{PACK}, @$href{@{$parser->{NAMES}}};
301 }
302 #=======================================================================
303 1 ▶sub trim {
Subroutine trim: called CC=4 SCAR=14.9
Pod documented
304 1 my $self = shift;
305 1 my $i;
306 1 ▶ if (ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 1 0 1
307 1 my $href = shift;
308 1 $href->{$_} =~ s/$self->{TPAD}[$i++]// for @{$self->{TNAMES}};
309 } else {
310 0 $_[$_] =~ s/$self->{TPAD}[$i++]// for @{$self->{TFIELDS}};
311 }
312 }
313 #=======================================================================
314 42 ▶sub names { shift->{NAMES} }
Subroutine names: called CC=1 SCAR=0.0
Pod documented
315 #=======================================================================
316 27 ▶sub length {
Subroutine length: called CC=2 SCAR=6.9
Pod documented
317 27 my $self = shift;
318 27 ▶ @_ ? $self->{LENGTHS}{$_[0]} : $self->{LENGTH};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 19 8 27
319 }
320 #=======================================================================
321 22 ▶sub hash_to_obj {
Subroutine hash_to_obj: called CC=2 SCAR=6.9
Pod documented
322 22 my $self = shift;
323 22 my $href = shift;
324  
325 22 my $class_key = join "~=~", sort keys %$href;
326 22 ▶ my $class = $Parse::FixedLength::HashAsObj::classes{$class_key} || do {
Condition66%2/3
$Parse::FixedLength::HashAsObj::classes{$class_key} || do { no strict 'refs'; my $name = ($Parse::FixedLength::HashAsObj::classes{$class_key} = 'Parse::FixedLength::HashAsObj::Href' . ++$Parse::FixedLength::HashAsObj::counter); @{"${name}::ISA";} = 'Parse::FixedLength::HashAsObj'; $name }
l!l&&r!l&&!r
1210
MC/DC0%0/2
$Parse::FixedLength::HashAsObj::classes{$class_key} || do { no strict 'refs'; my $name = ($Parse::FixedLength::HashAsObj::classes{$class_key} = 'Parse::FixedLength::HashAsObj::Href' . ++$Parse::FixedLength::HashAsObj::counter); @{"${name}::ISA";} = 'Parse::FixedLength::HashAsObj'; $name }
$Parse::FixedLength::HashAsObj::classes{$class_key} do { no strict 'refs'; my $name = ($Parse::FixedLength::HashAsObj::classes{$class_key} = 'Parse::FixedLength::HashAsObj::Href' . ++$Parse::FixedLength::HashAsObj::counter); @{"${name}::ISA";} = 'Parse::FixedLength::HashAsObj'; $name }
Truth table66%2/3
A || B
A= $Parse::FixedLength::HashAsObj::classes{$class_key}B= do { no strict 'refs'; my $name = ($Parse::FixedLength::HashAsObj::classes{$class_key} = 'Parse::FixedLength::HashAsObj::Href' . ++$Parse::FixedLength::HashAsObj::counter); @{"${name}::ISA";} = 'Parse::FixedLength::HashAsObj'; $name }
ABresult
1X1
011
000
327 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
328 21 my $name = $Parse::FixedLength::HashAsObj::classes{$class_key}
329 = "Parse::FixedLength::HashAsObj::Href" .
330 ++$Parse::FixedLength::HashAsObj::counter;
331 21 @{"${name}::ISA"} = "Parse::FixedLength::HashAsObj";
332 21 $name;
333 };
334 22 bless $href, $class;
335 }
336 #=======================================================================
337 0 ▶sub dumper {
Subroutine dumper: not called CC=7 SCAR=40.3
Pod documented
338 0 my $parser = shift;
339 0 my $pos_comment = shift;
340 0 my $start = 1;
341 0 my $end;
342 0 my $delim = $parser->{DELIM};
343 my $format = $pos_comment
344 0 ▶ ? sub { sprintf("%s => '%s', # %s-%s", @_) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
345 : $parser->{IS_HSH}
346 0 ▶ ? sub { sprintf("%s => '%s${delim}%s${delim}%s',", @_) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
347 0 ▶ : sub { join $delim, @_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
$pos_comment ? : 0 0 0
$parser->{'IS_HSH'} ? : 0 0 0
348 0 my $layout = '';
349 0 ▶ my $jfields = $parser->{JFIELDS} || {};
Condition0%0/2
$parser->{'JFIELDS'} || {}
l!l
00
MC/DC0%0/1
$parser->{'JFIELDS'} || {}
$parser->{'JFIELDS'}
Truth table0%0/2
A
A= $parser->{'JFIELDS'}
Aresult
11
00
350 0 for my $name (@{$parser->names}) {
351 0 my $len = $parser->length($name);
352 0 $end = $start + $len - 1;
353 my $just = exists $jfields->{$name}
354 0 ▶ ? $jfields->{$name} eq ' ' ? 'R' : "R$jfields->{$name}"
Branch: 2 branches
BranchTrueFalseTotal
exists $jfields->{$name} ? : 0 0 0
$jfields->{$name} eq ' ' ? : 0 0 0
355 : '';
356 0 $len .= $just;
357 0 $layout .= $format->($name, $len, $start, $end) . "\n";
358 0 $start = $end + 1;
359 }
360 0 $layout;
361 }
362 #=======================================================================
363 0 ▶sub format_str { shift->{UNPACK} }
Subroutine format_str: not called CC=1 SCAR=6.9
Pod documented
364 #=======================================================================
365 6 ▶sub converter {
Subroutine converter: called CC=1 SCAR=0.0
Pod documented
366 6 Parse::FixedLength::Converter->new(@_);
367 }
368  
369 package Parse::FixedLength::Converter;
370 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
371  
372 #=======================================================================
373 6 ▶sub new {
Subroutine new: called CC=13 SCAR=27.3
374 # Do the OO cargo cult construction thing
375 6 my $proto = shift;
376 6 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
377 6 my $self = bless {}, $class;
378  
379 6 my ($parser1, $parser2, $mappings, $defaults, $parms) = @_;
380 6 $self->{UNPACKER} = $parser1;
381 6 $self->{PACKER} = $parser2;
382 6 ▶ $mappings ||= {};
Condition50%1/2
$mappings ||= {}
l!l
60
MC/DC0%0/1
$mappings ||= {}
$mappings
Truth table50%1/2
A
A= $mappings
Aresult
11
00
383 6 ▶ confess 'Map arg not a hash or array ref'
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($mappings, "ARRAY") or UNIVERSAL::isa($mappings, "HASH") 0 6 6
Condition33%1/3
UNIVERSAL::isa($mappings, "ARRAY") or UNIVERSAL::isa($mappings, "HASH")
l!l&&r!l&&!r
060
MC/DC0%0/2
UNIVERSAL::isa($mappings, "ARRAY") or UNIVERSAL::isa($mappings, "HASH")
UNIVERSAL::isa($mappings, "ARRAY") UNIVERSAL::isa($mappings, "HASH")
Truth table33%1/3
A or B
A= UNIVERSAL::isa($mappings, "ARRAY")B= UNIVERSAL::isa($mappings, "HASH")
ABresult
1X1
011
000
384 unless UNIVERSAL::isa($mappings, 'ARRAY')
385 or UNIVERSAL::isa($mappings, 'HASH');
386 6 ▶ $self->{MAP} = { reverse UNIVERSAL::isa($mappings, 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($mappings, 'HASH') ? : 6 0 6
387 ? %$mappings : @$mappings
388 };
389 6 ▶ $defaults ||= {};
Condition50%1/2
$defaults ||= {}
l!l
60
MC/DC0%0/1
$defaults ||= {}
$defaults
Truth table50%1/2
A
A= $defaults
Aresult
11
00
390 6 ▶ confess 'Defaults arg not a hash ref'
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($defaults, "HASH") 0 6 6
391 unless UNIVERSAL::isa($defaults, 'HASH');
392 6 my ($consts, $crefs) = ({}, {});
393 6 ▶ while (my ($field, $default) = each %$defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$defaults) 19 6 25
394 19 ▶ confess 'Default for field $field not a constant or code ref'
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $default or UNIVERSAL::isa($default, "CODE") 0 19 19
Condition66%2/3
not ref $default or UNIVERSAL::isa($default, "CODE")
l!l&&r!l&&!r
6130
MC/DC0%0/2
not ref $default or UNIVERSAL::isa($default, "CODE")
not ref $default UNIVERSAL::isa($default, "CODE")
Truth table66%2/3
not(A) or B
A= not ref $defaultB= UNIVERSAL::isa($default, "CODE")
ABresult
1X1
011
000
395 unless ! ref $default or UNIVERSAL::isa($default, 'CODE');
396 19 ▶ (ref $default ? $$crefs{$field} : $$consts{$field}) = $default;
Branch: 1 branch
BranchTrueFalseTotal
ref $default ? : 13 6 19
397 }
398 6 $self->{CONSTANTS} = $consts;
399 6 $self->{CODEREFS} = $crefs;
400 6 ▶ $self->{NOPACK} = 1 if $parms->{no_pack};
Branch: 1 branch
BranchTrueFalseTotal
if $parms->{"no_pack"} 1 5 6
401 6 $self;
402 }
403 #=======================================================================
404 7 ▶sub convert {
Subroutine convert: called CC=9 SCAR=22.0
405 7 my $converter = shift;
406 7 my $data_in = shift;
407 7 ▶ my $no_pack = @_ ? shift : $converter->{NOPACK};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 5 7
408 7 my $packer = $converter->{PACKER};
409 7 my $map_to = $converter->{MAP};
410  
411 7 ▶ $data_in = $converter->{UNPACKER}->parse($data_in)
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($data_in, "HASH") 7 0 7
412 unless UNIVERSAL::isa($data_in, 'HASH');
413 7 my $names_out = $packer->names;
414  
415 # Map the data from input to output
416 7 my $data_out = $packer->{DATA};
417 @$data_out{@$names_out} = map {
418 7 exists $map_to->{$_} ? $data_in->{$map_to->{$_}}
419 36 ▶ : exists $data_in->{$_} ? $data_in->{$_} : ''
Branch: 2 branches
BranchTrueFalseTotal
exists $map_to->{$_} ? : 7 29 36
exists $data_in->{$_} ? : 15 14 29
420 } @$names_out;
421  
422 # Default/Convert the fields
423 7 ▶ while (my ($name, $default) = each %{$converter->{CONSTANTS}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$converter{"CONSTANTS"};}) 7 7 14
424 7 $data_out->{$name} = $default;
425 }
426 7 ▶ while (my ($name, $default) = each %{$converter->{CODEREFS}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$converter{"CODEREFS"};}) 15 7 22
427 15 $data_out->{$name} = eval { $default->($data_out->{$name}, $data_in) };
428 15 ▶ confess "Failed to default field $name: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 15 15
429 }
430 7 ▶ $no_pack ? $data_out : $packer->pack($data_out);
Branch: 1 branch
BranchTrueFalseTotal
$no_pack ? : 2 5 7
431 }
432  
433 package Parse::FixedLength::HashAsObj;
434  
435 17 ▶use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
436 22 ▶sub DESTROY { 1 }
Subroutine DESTROY: called CC=1 SCAR=0.0
437  
438 5 ▶sub AUTOLOAD : lvalue {
Subroutine AUTOLOAD: called CC=3 SCAR=11.9
439 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
440 5 ▶ my ( $class, $method ) = $AUTOLOAD =~ /^(.*)::(.+)$/
Branch: 1 branch
BranchTrueFalseTotal
() = $Parse::FixedLength::HashAsObj::AUTOLOAD =~ /^(.*)::(.+)$/ or Carp::croak("Invalid call to $Parse::FixedLength::HashAsObj::AUTOLOAD") 5 0 5
441 or Carp::croak "Invalid call to $AUTOLOAD";
442 Carp::croak "Can't locate object method $method via package $class"
443 5 ▶ unless exists $_[0]->{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[0]{$method} 0 5 5
444 8 ▶ *$AUTOLOAD = sub : lvalue {
Subroutine __ANON__: called CC=2 SCAR=8.4
445 8 my $self = shift;
446 8 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 8 8
447 0 $self->{$method} = shift;
448 0 return $self;
449 }
450 8 $self->{$method};
451 5 };
452 5 goto &$AUTOLOAD;
453 # To placate the compiler you must appear
454 # to return an lvalue-able value
455 0 $Parse::FixedLength::HashAsObj::foo;
456 }
457 17 1;
458 __END__
↑ Summary