File Coverage

blib/lib/Finance/Currency/Convert.pm
Criterion Covered Total %
statement 27 86 31.4
branch 0 14 0.0
condition 1 3 33.3
subroutine 6 13 46.1
pod 0 11 0.0
total 34 127 26.7


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             #
3             # Copyright (C) 2000-2010, Jan Willamowius , http://www.willamowius.de/
4             # All rights reserved.
5             # This is free software; you can redistribute it and/or
6             # modify it under the same terms as Perl itself.
7             #
8              
9             package Finance::Currency::Convert;
10              
11 1     1   29558 use strict;
  1         3  
  1         49  
12 1     1   5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  1         2  
  1         1783  
13              
14             require Exporter;
15             require AutoLoader;
16              
17             @ISA = qw(Exporter AutoLoader);
18             # Items to export into callers namespace by default. Note: do not export
19             # names by default without a very good reason. Use EXPORT_OK instead.
20             # Do not simply export all your public functions/methods/constants.
21             @EXPORT = qw(
22            
23             );
24             $VERSION = '1.08';
25              
26             my %EuroRates = (
27             BEF => {EUR=>0.0247899055505, BEF => 1},
28             DEM => {EUR=>0.511291881196, DEM => 1},
29             ESP => {EUR=>0.00601012104384, ESP => 1},
30             EUR => {ATS=>13.7603, BEF=>40.3399, DEM=>1.95583, EUR=>1, ESP=>166.386, FIM=>5.94573, FRF=>6.55957, GRD=>340.750, IEP=>.787564, ITL=>1936.27, LUF=>40.3399, NLG=>2.20371, PTE=>200.482, CYP=>0.585274, MTL=>0.429300, SIT=>239.640, SKK=>30.1260, EEK=>15.6466},
31             FRF => {EUR=>0.152449017237, FRF => 1},
32             GRD => {EUR=>0.00293470286134, GRD => 1},
33             IEP => {EUR=>1.26973807843, IEP => 1},
34             ITL => {EUR=>0.000516456899089, ITL => 1},
35             LUF => {EUR=>0.0247899055505, LUF => 1},
36             NLG => {EUR=>0.45378021609, NLG => 1},
37             ATS => {EUR=>0.0726728341679, ATS => 1},
38             PTE => {EUR=>0.00498797897068, PTE => 1},
39             FIM => {EUR=>0.168187926462, FIM => 1},
40             CYP => {EUR=>1.70860144137618, CYP => 1},
41             MTL => {EUR=>2.32937339855579, MTL => 1},
42             SIT => {EUR=>0.00417292605575029, SIT => 1},
43             SKK => {EUR=>0.0331939188740623, SKK => 1},
44             EEK => {EUR=>0.0639116485371, EEK => 1},
45             );
46              
47             sub new() {
48 1     1 0 12 my $proto = shift;
49 1   33     9 my $class = ref($proto) || $proto;
50 1         2 my $self = {};
51 1         3 $self->{CurrencyRates} = \%EuroRates;
52 1         104 $self->{RatesFile} = undef;
53 1         5 $self->{UserAgent} = "Finance::Currency::Convert $VERSION";
54 1         2 bless($self, $class);
55 1         4 return $self;
56             }
57              
58             sub setRate() {
59 0     0 0 0 my $self = shift;
60 0         0 my $source = shift;
61 0         0 my $target = shift;
62 0         0 my $rate = shift;
63 0         0 $self->{CurrencyRates}{$source}{$target} = $rate;
64             }
65              
66             sub setRatesFile() {
67 0     0 0 0 my $self = shift;
68 0         0 $self->{RatesFile} = shift;
69 0         0 $self->readRatesFile();
70             }
71              
72             sub readRatesFile() {
73 0     0 0 0 my $self = shift;
74 0 0       0 return if (!defined $self->{RatesFile});
75              
76 0 0       0 open(RATES, "<$self->{RatesFile}") or return;
77 0         0 $self->{CurrencyRates} = (); # clear current table
78 0         0 while(local $_ = ) {
79 0         0 my ($source, $targetrates) = split(/\|/, $_);
80 0         0 foreach my $target (split(/\:/, $targetrates)) {
81 0         0 my @pieces = split(/\=/, $target);
82 0 0       0 if (scalar(@pieces) > 1) {
83 0         0 $self->setRate($source, $pieces[0], $pieces[1]);
84             }
85             }
86             }
87 0         0 close(RATES);
88             }
89              
90             sub writeRatesFile() {
91 0     0 0 0 my $self = shift;
92 0 0       0 return if (!defined $self->{RatesFile});
93              
94 0 0       0 open(RATES, ">$self->{RatesFile}") or return;
95 0         0 foreach my $sourcerate (sort keys %{$self->{CurrencyRates}}) {
  0         0  
96 0         0 print RATES "$sourcerate|";
97 0         0 foreach my $targetrate (sort keys %{ $self->{CurrencyRates}{$sourcerate}}) {
  0         0  
98 0         0 print RATES "$targetrate=" . $self->{CurrencyRates}{$sourcerate}{$targetrate} . ":";
99             };
100 0         0 print RATES "\n";
101             };
102 0         0 close(RATES);
103             }
104              
105             sub updateRates() {
106 0     0 0 0 my $self = shift;
107 0         0 my @CurrencyList = @_;
108             # test if Finance::Quote is available
109 0         0 eval { require Finance::Quote; };
  0         0  
110 0 0       0 if ($@) {
111 0         0 warn "Finance::Quote not installed - can't use updateRates()\n";
112 0         0 return;
113             };
114             # get the exchange rates
115 0         0 my $q = Finance::Quote->new;
116 0         0 $q->user_agent->agent($self->{UserAgent});
117 0         0 foreach my $source (@CurrencyList) {
118 0         0 foreach my $target (sort keys %{ $self->{CurrencyRates}}) {
  0         0  
119 0         0 $self->setRate($source, $target, $q->currency($source, $target));
120             }
121             }
122 0         0 foreach my $source (sort keys %{ $self->{CurrencyRates}}) {
  0         0  
123 0         0 foreach my $target (@CurrencyList) {
124 0         0 $self->setRate($source, $target, $q->currency($source, $target));
125             }
126             }
127             }
128              
129             sub updateRate() {
130 0     0 0 0 my $self = shift;
131 0         0 my $source = shift;
132 0         0 my $target = shift;
133             # Test if Finance::Quote is available
134 0         0 eval { require Finance::Quote; };
  0         0  
135 0 0       0 if ($@) { return; }; # F::Q not installed
  0         0  
136             # get the exchange rates
137 0         0 my $q = Finance::Quote->new;
138 0         0 $q->user_agent->agent($self->{UserAgent});
139 0         0 $self->setRate($source, $target, $q->currency($source, $target));
140             }
141              
142             sub setUserAgent() {
143 0     0 0 0 my $self = shift;
144 0         0 $self->{UserAgent} = shift;
145             }
146              
147             sub convert() {
148 12     12 0 1466 my $self = shift;
149 12         14 my $amount = shift;
150 12         14 my $source = shift;
151 12         12 my $target = shift;
152 12         56 return $amount * $self->{CurrencyRates}->{$source}{$target};
153             }
154              
155             sub convertFromEUR() {
156 4     4 0 15 my $self = shift;
157 4         6 my $amount = shift;
158 4         7 my $target = shift;
159 4         8 return $self->convert($amount, "EUR", $target);
160             }
161              
162             sub convertToEUR() {
163 4     4 0 872 my $self = shift;
164 4         7 my $amount = shift;
165 4         5 my $source = shift;
166 4         8 return $self->convert($amount, $source, "EUR");
167             }
168              
169             1;
170              
171             __END__