| 1 | package Finance::Currency::ParValueSeparate; | ||||||||||
| 2 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | our $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/; | |||||||||
| 5 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 5 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 8 | 5 | my $class = shift; | |||||||||
| 9 | 5 | my %opt = @_; | |||||||||
| 10 | 5 | my $concrete = $class; | |||||||||
| 11 | 5 | my $amount = []; | |||||||||
| 12 | |||||||||||
| 13 | # called base class | ||||||||||
| 14 | 5 | ▶ | if ( $class eq 'Finance::Currency::ParValueSeparate' ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | |||||||||||
| 16 | # which subclass we wanner using? | ||||||||||
| 17 | 3 | ▶ | if ( defined $opt{'currency'} ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 1 | $concrete = "Finance::Currency::ParValueSeparate::".$opt{'currency'}; | |||||||||
| 19 | }else{ | ||||||||||
| 20 | 2 | $concrete = "Finance::Currency::ParValueSeparate::" . shift @_; | |||||||||
| 21 | 2 | ▶ | $amount = ( ref $_[0] eq 'ARRAY' ) ? $_[0] : [ @_ ]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | # try to require the subclass | ||||||||||
| 25 | 3 | eval "require $concrete;"; | |||||||||
| 26 | 3 | ▶ | die "subclass: $concrete could not required!" if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | |||||||||||
| 28 | # called subclass | ||||||||||
| 29 | }else{ | ||||||||||
| 30 | 2 | ▶ | $amount = ( ref $_[0] eq 'ARRAY' ) ? $_[0] : [ @_ ]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 5 | my $self = bless {}, $concrete; | |||||||||
| 34 | 5 | $self->amount( $amount ); | |||||||||
| 35 | 5 | $self->with_dollar( $self->dollar ); | |||||||||
| 36 | 5 | $self->with_cent( $self->cent ); | |||||||||
| 37 | |||||||||||
| 38 | 5 | return $self; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 21 | ▶ | sub _amount_format { | ||||||||
|
Subroutine
_amount_format: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 21 | my $self = shift; | |||||||||
| 43 | 21 | my $amount = shift; | |||||||||
| 44 | 21 | return sprintf( '%.2f', $amount ); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 15 | ▶ | sub amount { | ||||||||
|
Subroutine
amount: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 48 | 15 | my $self = shift; | |||||||||
| 49 | 15 | ▶ | my $amount_ref = ( ref $_[0] eq 'ARRAY' ) ? $_[0] : [ @_ ]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 15 | ▶ | $self->{_AMOUNT} = [ map { $self->_amount_format( $_ ) } @$amount_ref ] | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | if scalar @$amount_ref; | ||||||||||
| 52 | 15 | ▶ | return wantarray ? @{$self->{_AMOUNT}} : $self->{_AMOUNT}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 23 | ▶ | sub with_dollar { | ||||||||
|
Subroutine
with_dollar: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 56 | 23 | my $self = shift; | |||||||||
| 57 | 23 | ▶ | my $with_dollar = ( ref $_[0] eq 'ARRAY' ) ? $_[0] : [ @_ ]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 23 | ▶ | if ( @_ ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 7 | my $valid_dollar; | |||||||||
| 60 | 7 | map { $valid_dollar->{$_}++; } $self->dollar; | |||||||||
| 61 | 7 | $self->{_with_dollar} = [ grep { $valid_dollar->{$_} } @$with_dollar ]; | |||||||||
| 62 | |||||||||||
| 63 | 7 | map { delete $valid_dollar->{$_} } @$with_dollar; | |||||||||
| 64 | 7 | $self->{_without_dollar} = [ keys %$valid_dollar ]; | |||||||||
| 65 | } | ||||||||||
| 66 | 23 | ▶ | return wantarray ? @{$self->{_with_dollar}} : $self->{_with_dollar}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | } | ||||||||||
| 68 | 4 | ▶ | sub without_dollar { | ||||||||
|
Subroutine
without_dollar: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 69 | 4 | my $self = shift; | |||||||||
| 70 | 4 | ▶ | my $without_dollar = ( ref $_[0] eq 'ARRAY' ) ? $_[0] : [ @_ ]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 4 | ▶ | if ( @_ ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 2 | my $valid_dollar; | |||||||||
| 73 | 2 | map { $valid_dollar->{$_}++; } $self->dollar; | |||||||||
| 74 | 2 | $self->{_without_dollar} = [ grep { $valid_dollar->{$_} } @$without_dollar ]; | |||||||||
| 75 | |||||||||||
| 76 | 2 | map { delete $valid_dollar->{$_} } @$without_dollar; | |||||||||
| 77 | 2 | $self->{_with_dollar} = [ keys %$valid_dollar ]; | |||||||||
| 78 | } | ||||||||||
| 79 | 4 | ▶ | return wantarray ? @{$self->{_without_dollar}} : $self->{_without_dollar}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 19 | ▶ | sub with_cent { | ||||||||
|
Subroutine
with_cent: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 83 | 19 | my $self = shift; | |||||||||
| 84 | 19 | ▶ | my $with_cent = ( ref $_[0] eq 'ARRAY' ) ? $_[0] : [ @_ ]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 19 | ▶ | if ( @_ ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 5 | my $valid_cent; | |||||||||
| 87 | 5 | map { $valid_cent->{$_}++; } $self->cent; | |||||||||
| 88 | 5 | $self->{_with_cent} = [ grep { $valid_cent->{$_} } @$with_cent ]; | |||||||||
| 89 | |||||||||||
| 90 | 5 | map { delete $valid_cent->{$_} } @$with_cent; | |||||||||
| 91 | 5 | $self->{_without_cent} = [ keys %$valid_cent ]; | |||||||||
| 92 | } | ||||||||||
| 93 | 19 | ▶ | return wantarray ? @{$self->{_with_cent}} : $self->{_with_cent}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | } | ||||||||||
| 95 | 0 | ▶ | sub without_cent { | ||||||||
|
Subroutine
without_cent: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 96 | 0 | my $self = shift; | |||||||||
| 97 | 0 | ▶ | my $without_cent = ( ref $_[0] eq 'ARRAY' ) ? $_[0] : [ @_ ]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | ▶ | if ( @_ ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | my $valid_cent; | |||||||||
| 100 | 0 | map { $valid_cent->{$_}++; } $self->cent; | |||||||||
| 101 | 0 | $self->{_without_cent} = [ grep { $valid_cent->{$_} } @$without_cent ]; | |||||||||
| 102 | |||||||||||
| 103 | 0 | map { delete $valid_cent->{$_} } @$without_cent; | |||||||||
| 104 | 0 | $self->{_with_cent} = [ keys %$valid_cent ]; | |||||||||
| 105 | } | ||||||||||
| 106 | 0 | ▶ | return wantarray ? @{$self->{_without_cent}} : $self->{_without_cent}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 17 | ▶ | sub only_dollar { | ||||||||
|
Subroutine
only_dollar: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 110 | 17 | my $self = shift; | |||||||||
| 111 | 17 | my $flag = shift; | |||||||||
| 112 | 17 | ▶ | if ( defined $flag ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 2 | ▶ | $self->{_only_dollar} = ( $flag ) ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | } | ||||||||||
| 115 | 17 | return $self->{_only_dollar}; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 4 | ▶ | sub parse { | ||||||||
|
Subroutine
parse: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 119 | 4 | my $self = shift; | |||||||||
| 120 | 4 | $self->amount(@_); | |||||||||
| 121 | 4 | delete $self->{_DOLLAR}; | |||||||||
| 122 | 4 | delete $self->{_CENT}; | |||||||||
| 123 | |||||||||||
| 124 | map { | ||||||||||
| 125 | 4 | my $dollar = int $_; | |||||||||
| 126 | 14 | my ( $cent ) = ( $_ =~ /\.(.*)$/ ); | |||||||||
| 127 | |||||||||||
| 128 | 14 | foreach my $parvalue ( sort { $b<=>$a } $self->with_dollar ){ | |||||||||
| 129 | 70 | my $number = ( $dollar - $dollar % $parvalue ) / $parvalue; | |||||||||
| 130 | 70 | $dollar -= $number * $parvalue; | |||||||||
| 131 | 70 | $self->{_DOLLAR}{$parvalue} += $number; | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | 14 | ▶ | next if $self->only_dollar; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | |||||||||||
| 136 | 14 | foreach my $parvalue ( sort { $b<=>$a } $self->with_cent ){ | |||||||||
| 137 | 84 | my $number = ( $cent - $cent % $parvalue ) / $parvalue; | |||||||||
| 138 | 84 | $cent -= $number * $parvalue; | |||||||||
| 139 | 84 | $self->{_CENT}{$parvalue} += $number; | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | } $self->amount; | ||||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 4 | ▶ | sub dollar_parvalues { | ||||||||
|
Subroutine
dollar_parvalues: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 146 | 4 | my $self = shift; | |||||||||
| 147 | 4 | return sort { $b <=> $a } keys %{$self->{_DOLLAR}}; | |||||||||
| 148 | } | ||||||||||
| 149 | 20 | ▶ | sub number_of_dollar { | ||||||||
|
Subroutine
number_of_dollar: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 150 | 20 | my $self = shift; | |||||||||
| 151 | 20 | my $parvalue = shift; | |||||||||
| 152 | 20 | return $self->{_DOLLAR}{$parvalue}; | |||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | 4 | ▶ | sub cent_parvalues { | ||||||||
|
Subroutine
cent_parvalues: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 156 | 4 | my $self = shift; | |||||||||
| 157 | 4 | return sort { $b <=> $a } keys %{$self->{_CENT}}; | |||||||||
| 158 | } | ||||||||||
| 159 | 24 | ▶ | sub number_of_cent { | ||||||||
|
Subroutine
number_of_cent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 160 | 24 | my $self = shift; | |||||||||
| 161 | 24 | my $parvalue = shift; | |||||||||
| 162 | 24 | return $self->{_CENT}{$parvalue}; | |||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | |||||||||||
| 166 | # subclass override them | ||||||||||
| 167 | 0 | ▶ | sub currency_name { | ||||||||
|
Subroutine
currency_name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 168 | 0 | croak 'base class has no currency_name, use subclass'; | |||||||||
| 169 | } | ||||||||||
| 170 | 0 | ▶ | sub dollar { | ||||||||
|
Subroutine
dollar: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 171 | 0 | croak 'base class has no dollar denomination informations, use subclass'; | |||||||||
| 172 | } | ||||||||||
| 173 | 0 | ▶ | sub cent { | ||||||||
|
Subroutine
cent: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 174 | 0 | croak 'base class has no cent denomination informations, use subclass'; | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | 1 | 1; | |||||||||
| 178 | __END__ | ||||||||||