| 1 | package oEdtk::Record; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = 2.1129; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 0 | ▶ | sub debug { | ||||||||||||||||||||||||||
|
Subroutine
debug: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 11 | 0 | $self->{'_DEBUG'} = 1; | |||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # A RECORD IS A SEQUENCE OF FIELDS. | ||||||||||||||||||||||||||||
| 15 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 0 | my ( $class, @fields ) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | my $template = ''; | |||||||||||||||||||||||||||
| 19 | 0 | foreach my $i ( 0 .. $#fields ) { | |||||||||||||||||||||||||||
| 20 | 0 | my $field = $fields[$i]; | |||||||||||||||||||||||||||
| 21 | 0 | ▶ | if ( !blessed($field) || !$field->isa('oEdtk::Field') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($field) and $field->isa("oEdtk::Field")
MC/DC0%0/2
builtin::blessed($field) and $field->isa("oEdtk::Field")
builtin::blessed($field)
$field->isa("oEdtk::Field")
Truth table0%0/3
A and B
A= builtin::blessed($field)B= $field->isa("oEdtk::Field")
| |||||||||||||||||||||||||||||
| 22 | 0 | die "ERROR: oEdtk::Record::new only accepts oEdtk::Field objects\n"; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | 0 | my $len = $field->get_len(); | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | if ( $len eq '*' && $i < $#fields ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$len eq "*" and $i < $#fields
MC/DC0%0/2
$len eq "*" and $i < $#fields
$len eq "*"
$i < $#fields
Truth table0%0/3
A and B
A= $len eq "*"B= $i < $#fields
| |||||||||||||||||||||||||||||
| 26 | 0 | die "ERROR: oEdtk::Record::new: catch-all field must be the last\n"; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # if ($i != 0) { | ||||||||||||||||||||||||||||
| 30 | # $template .= ' '; | ||||||||||||||||||||||||||||
| 31 | # } | ||||||||||||||||||||||||||||
| 32 | 0 | $template .= "A$len"; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | my $self = { | |||||||||||||||||||||||||||
| 36 | seek_key => "LIGNE.{153}(.{10})", | ||||||||||||||||||||||||||||
| 37 | fields_offset => 10, | ||||||||||||||||||||||||||||
| 38 | fields => \@fields, | ||||||||||||||||||||||||||||
| 39 | template => $template, | ||||||||||||||||||||||||||||
| 40 | bound => {} | ||||||||||||||||||||||||||||
| 41 | }; | ||||||||||||||||||||||||||||
| 42 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 43 | 0 | return $self; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub set_seek_key { | ||||||||||||||||||||||||||
|
Subroutine
set_seek_key: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 0 | my ( $self, $seek_key ) = @_; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $self->{'seek_key'} = $seek_key || "LIGNE.{153}(.{10})"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$seek_key || 'LIGNE.{153}(.{10})'
MC/DC0%0/1
$seek_key || 'LIGNE.{153}(.{10})'
$seek_key
Truth table0%0/2
A
A= $seek_key
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub set_fields_offset { | ||||||||||||||||||||||||||
|
Subroutine
set_fields_offset: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 0 | my ( $self, $fields_offset ) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | $self->{'fields_offset'} = $fields_offset || 10; | ||||||||||||||||||||||||||
|
Condition0%0/2
$fields_offset || 10
MC/DC0%0/1
$fields_offset || 10
$fields_offset
Truth table0%0/2
A
A= $fields_offset
| |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 0 | my ( $self, $line ) = @_; | |||||||||||||||||||||||||||
| 60 | 0 | my @values; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | my $bound = $self->{'bound'}; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # my $fields_offset = $self->{'fields_offset'}; | ||||||||||||||||||||||||||||
| 65 | # if ($line !~ /^.{$fields_offset}(.*)$/) { | ||||||||||||||||||||||||||||
| 66 | # die "ERROR: Line too short\n"; | ||||||||||||||||||||||||||||
| 67 | # } | ||||||||||||||||||||||||||||
| 68 | # $line = $1; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my @vals = unpack( $self->{'template'}, $line ); | |||||||||||||||||||||||||||
| 71 | 0 | my %hvals = (); | |||||||||||||||||||||||||||
| 72 | 0 | foreach my $i ( 0 .. $#{ $self->{'fields'} } ) { | |||||||||||||||||||||||||||
| 73 | 0 | my $field = $self->{'fields'}->[$i]; | |||||||||||||||||||||||||||
| 74 | 0 | my $name = $field->get_name(); | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ( exists( $bound->{$name} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | $hvals{$name} = $field->process( $vals[$i] ); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 0 | return %hvals; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Bind fields onto the record. With a name => new-name map, rename | ||||||||||||||||||||||||||||
| 83 | # the matching fields; with no argument (the fork form used by | ||||||||||||||||||||||||||||
| 84 | # oEUser::Descriptor) bind every field under its current name, so | ||||||||||||||||||||||||||||
| 85 | # that parse() returns them all. Returns the record. | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub bind { | ||||||||||||||||||||||||||
|
Subroutine
bind: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 88 | 0 | my %map = @_; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | my %bound; | |||||||||||||||||||||||||||
| 91 | 0 | foreach my $field ( @{ $self->{'fields'} } ) { | |||||||||||||||||||||||||||
| 92 | 0 | my $name = $field->get_name(); | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | next unless exists( $map{$name} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | my $new = $map{$name}; | |||||||||||||||||||||||||||
| 96 | 0 | $field->set_name($new); | |||||||||||||||||||||||||||
| 97 | 0 | $bound{$new} = 1; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||||||||||
| 100 | 0 | $bound{$name} = 1; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 0 | $self->{'bound'} = { %{ $self->{'bound'} }, %bound }; | |||||||||||||||||||||||||||
| 104 | 0 | return $self; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub bind_all { | ||||||||||||||||||||||||||
|
Subroutine
bind_all: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | my $count = 0; | |||||||||||||||||||||||||||
| 111 | 0 | my $pos = 0; | |||||||||||||||||||||||||||
| 112 | 0 | my %identifiers; | |||||||||||||||||||||||||||
| 113 | 0 | foreach my $field ( @{ $self->{'fields'} } ) { | |||||||||||||||||||||||||||
| 114 | 0 | my $name = $field->get_name(); | |||||||||||||||||||||||||||
| 115 | 0 | $name =~ s/(?:-\d+)?$//; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # Select the longest component. | ||||||||||||||||||||||||||||
| 118 | 0 | my @parts = split( /-/, $name ); | |||||||||||||||||||||||||||
| 119 | 0 | my $id = ( reverse sort { length($a) <=> length($b) } @parts )[0]; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | my $orig = $field->get_name(); | |||||||||||||||||||||||||||
| 122 | 0 | my $len = $field->get_len(); | |||||||||||||||||||||||||||
| 123 | warn "DEBUG: $id \tindex: $count \tpos: $pos \tlength: $len \tfrom $orig\n" | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if $self->{'_DEBUG'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | $pos += $len if ( $len ne '*' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | $field->set_name($id); | |||||||||||||||||||||||||||
| 128 | 0 | $identifiers{$id} = 1; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | $self->{'bound'} = { %{ $self->{'bound'} }, $id => 1 }; | |||||||||||||||||||||||||||
| 131 | 0 | $count++; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2 | 1; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 NAME | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | oEdtk::Record - a fixed-width record made of typed fields | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | use oEdtk::Record; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | my $rec = oEdtk::Record->new(@fields); # oEdtk::Field objects | ||||||||||||||||||||||||||||
| 146 | $rec->bind_all; | ||||||||||||||||||||||||||||
| 147 | my %vals = $rec->parse($line); | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | A record is an ordered sequence of typed fields (L<oEdtk::Field> objects). It | ||||||||||||||||||||||||||||
| 152 | builds an C<unpack> template from the field lengths, unpacks a raw line, and | ||||||||||||||||||||||||||||
| 153 | runs each bound field's C<process> to produce a name-to-value hash. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head1 METHODS | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head2 new | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | my $rec = oEdtk::Record->new(@fields); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Build a record from C<@fields>. Every argument must be an C<oEdtk::Field>, and a | ||||||||||||||||||||||||||||
| 162 | catch-all field (length C<*>) must come last. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head2 debug | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | $rec->debug; | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Turn on the verbose C<bind_all> output. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head2 set_seek_key | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | $rec->set_seek_key($seek_key); | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Set the regex used to find this record in the stream; defaults to the | ||||||||||||||||||||||||||||
| 175 | C<LIGNE...> form. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head2 set_fields_offset | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | $rec->set_fields_offset($offset); | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Set the offset at which the fields start. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head2 parse | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | my %values = $rec->parse($line); | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Unpack C<$line> and run each bound field's C<process>, returning a hash of | ||||||||||||||||||||||||||||
| 188 | name-to-value pairs. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head2 bind | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | $rec->bind(%rename); | ||||||||||||||||||||||||||||
| 193 | $rec->bind; # bind every field under its current name | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Bind fields onto the record. With a name-to-new-name map, rename the matching | ||||||||||||||||||||||||||||
| 196 | fields; with no argument, bind every field so L</parse> returns them all. | ||||||||||||||||||||||||||||
| 197 | Returns the record. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head2 bind_all | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | $rec->bind_all; | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Name each field after its longest C<->-separated component and bind all of | ||||||||||||||||||||||||||||
| 204 | them. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | The oEdtk team. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =cut | ||||||||||||||||||||||||||||