| 1 | package Finance::Currency::Convert::DnB; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | our @ISA = qw/Exporter/; | |||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | our @EXPORT = qw/currency update_currency currencies/; | |||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = '0.2'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | our $currency; | |||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use XML::Simple; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use LWP::Simple; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Slurp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | sub update_currency { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update_currency: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | my $filename = File::Spec->tmpdir() . "/currency_list_" . ((defined $>) ? $> : "") . ".xml"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | #only download XML twice a day | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | if (!-e $filename || time()-43200 < -M $filename || $_[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not -e $filename or time - 43200 < -M $filename or $_[0]
not -e $filename or time - 43200 < -M $filename
MC/DC0%0/3
not -e $filename or time - 43200 < -M $filename or $_[0]
not -e $filename
time - 43200 < -M $filename
$_[0]
Truth table0%0/4
not(A) or B or C
A= not -e $filenameB= time - 43200 < -M $filenameC= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | is_success ($_=getstore('http://www.dnbnor.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml', $filename)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | or die 'Failed to get list of currencies; http error code: ' . $_; | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | my $content = slurp $filename; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | $currency = XMLin($content, KeyAttr => ["kode"]); | |||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub currencies { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
currencies: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | update_currency; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | sort keys %{$currency->{valutakurs}} | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub currency { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
currency: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my ($amount, $from, $to, $decimals) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | $decimals = 2 unless defined $decimals; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | update_currency; | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | my $res; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | my $from_currency = $currency->{valutakurs}->{$from}->{overforsel}->{midtkurs} / $currency->{valutakurs}->{$from}->{enhet} if ($from ne "NOK"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | my $to_currency = $currency->{valutakurs}->{$_}->{overforsel}->{midtkurs} / $currency->{valutakurs}->{$_}->{enhet} if ($_ ne "NOK"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | foreach my $amount ( (ref($amount) eq 'ARRAY') ? @$amount : $amount ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if ($_ eq "NOK") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | $res += $amount * $from_currency; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | elsif ($from eq "NOK") { | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $res += $amount / $to_currency; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | $res += $amount * $from_currency / $to_currency; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | $res = sprintf('%.' . $decimals . 'f', $res); | |||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | ($to) ? return $res : $_ => $res | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | } ($to) ? $to : keys %{$currency->{valutakurs}} | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | Finance::Currency::Convert::DnB - convert currencies with up to date currencies from dnbnor.no | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | use Finance::Currency::Convert::DnB; | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | #get results with default number of decimals which is 2 | ||||||||||||||||||||||||||||||||||||||||||
| 70 | $result = currency 20, "NOK", "GBP"; | ||||||||||||||||||||||||||||||||||||||||||
| 71 | #3 decimals | ||||||||||||||||||||||||||||||||||||||||||
| 72 | $result = currency 20, "NOK", "GBP", 3; | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | #convert several numbers | ||||||||||||||||||||||||||||||||||||||||||
| 75 | $result = currency \@values, "NOK", "GBP"; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | $result = currency [20, 50, 35], "NOK", "GBP"; | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | #store all results in a hash | ||||||||||||||||||||||||||||||||||||||||||
| 79 | my %all_currencies = currency 20, "NOK"; | ||||||||||||||||||||||||||||||||||||||||||
| 80 | print "20 NOK in $_ is $all_currencies{$_}\n" foreach (keys %all_currencies); | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | #get a list of available currencies | ||||||||||||||||||||||||||||||||||||||||||
| 83 | my @currencies = currencies; | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | Finance::Currency::Convert::DnB uses a XML list from dnbnor.no to convert currencies. Caches XML list in a temporary file for quick access. | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | =head1 AVAILABLE METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | =head2 currency | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | $result = convert 20, "NOK", "GBP", 2; | ||||||||||||||||||||||||||||||||||||||||||
| 94 | Amount can also be an array reference, and it will return a total of all elements. | ||||||||||||||||||||||||||||||||||||||||||
| 95 | If conversion currency is excluded, it will return a hash with all results for all currencies. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | If number of decimals is excluded, if will default to 2 decimals. | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | =head2 currencies | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | Returns a list of available currencies sorted alphabetically. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | =head2 update_currency | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | This is an internal function called automatically to update currencies. It is done automatically if the cache is non-existing or if it is older than 12 hours. You can force updating by calling it with a true argument. | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||||||||||||||||