| 1 | package oEdtk::SignedField; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Decode a signed amount field encoded using Infinite conventions. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use base 'oEdtk::Field'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | our $VERSION = 2.1126; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 0 | my ( $class, $name, $ilen, $flen ) = @_; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | $flen = 0 unless defined($flen); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 0 | my $self = $class->SUPER::new( $name, $ilen + $flen ); | |||||||||||||||||||||||||||
| 16 | 0 | $self->{'intlen'} = $ilen; | |||||||||||||||||||||||||||
| 17 | 0 | $self->{'fraclen'} = $flen; | |||||||||||||||||||||||||||
| 18 | 0 | return $self; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 0 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | $data =~ s/\s+//g; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | if ( $data !~ /^-?\d*[p-y]?$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | warn "INFO: Unexpected numerical value: $data in " . $self->{'name'} . "\n"; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if ( $data eq '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | $data = 0; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if ( $data =~ s/([p-y])$/ord($1) - ord('p')/e ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | $data *= -1; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | my $flen = $self->{'fraclen'}; | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ( $data !~ /\./ && $flen > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $data =~ /\./ and $flen > 0
MC/DC0%0/2
not $data =~ /\./ and $flen > 0
not $data =~ /\./
$flen > 0
Truth table0%0/3
not(A) and B
A= not $data =~ /\./B= $flen > 0
| |||||||||||||||||||||||||||||
| 39 | 0 | $data /= 10**$flen; | |||||||||||||||||||||||||||
| 40 | 0 | return sprintf( "%.${flen}f", $data ); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 0 | return $data; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | 1; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 NAME | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | oEdtk::SignedField - signed amount field (Infinite encoding) | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | use oEdtk::SignedField; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | my $field = oEdtk::SignedField->new('MONTANT', 8, 2); | ||||||||||||||||||||||||||||
| 56 | print $field->process('00001234'), "\n"; # '12.34' | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | An C<oEdtk::Field> subclass decoding a signed amount that follows the Infinite | ||||||||||||||||||||||||||||
| 61 | convention: the trailing byte, when it is a letter in the range C<p>..C<y>, | ||||||||||||||||||||||||||||
| 62 | both carries the last digit and marks the amount negative. A value whose shape | ||||||||||||||||||||||||||||
| 63 | does not match the expected form raises a warning. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head1 METHODS | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head2 new | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | my $field = oEdtk::SignedField->new($name, $ilen, $flen); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Construct a field whose byte length is C<$ilen + $flen>. C<$flen> defaults to | ||||||||||||||||||||||||||||
| 72 | C<0>. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head2 process | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | my $value = $field->process($data); | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Strip the blanks from C<$data>; warn when it is not a plain (optionally signed) | ||||||||||||||||||||||||||||
| 79 | number with an optional trailing C<p>..C<y> letter. An empty value becomes | ||||||||||||||||||||||||||||
| 80 | C<0>; a trailing C<p>..C<y> letter replaces the last digit with its value and | ||||||||||||||||||||||||||||
| 81 | negates the amount. When C<$flen> is set the value is scaled and formatted with | ||||||||||||||||||||||||||||
| 82 | C<$flen> decimals. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | The oEdtk team. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||