| 1 | package oEdtk::SignedField; | ||
| 2 | |||
| 3 | # Decode a signed amount field encoded using Infinite conventions. | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use base 'oEdtk::Field'; | ||
| 9 | our $VERSION = 2.1129; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my ( $class, $name, $ilen, $flen ) = @_; | ||
| 13 | |||
| 14 | $flen = 0 unless defined($flen); | ||
| 15 | my $self = $class->SUPER::new( $name, $ilen + $flen ); | ||
| 16 | $self->{'intlen'} = $ilen; | ||
| 17 | $self->{'fraclen'} = $flen; | ||
| 18 | return $self; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub process { | ||
| 22 | my ( $self, $data ) = @_; | ||
| 23 | |||
| 24 | $data =~ s/\s+//g; | ||
| 25 | |||
| 26 | if ( $data !~ /^-?\d*[p-y]?$/ ) { | ||
| 27 | warn "INFO: Unexpected numerical value: $data in " . $self->{'name'} . "\n"; | ||
| 28 | } | ||
| 29 | |||
| 30 | if ( $data eq '' ) { | ||
| 31 | $data = 0; | ||
| 32 | } | ||
| 33 | if ( $data =~ s/([p-y])$/ord($1) - ord('p')/e ) { | ||
| 34 | $data *= -1; | ||
| 35 | } | ||
| 36 | |||
| 37 | my $flen = $self->{'fraclen'}; | ||
| 38 | if ( $data !~ /\./ && $flen > 0 ) { | ||
| 39 | $data /= 10**$flen; | ||
| 40 | return sprintf( "%.${flen}f", $data ); | ||
| 41 | } | ||
| 42 | return $data; | ||
| 43 | } | ||
| 44 | |||
| 45 | 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 |