Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl Monk, Perl Meditation
 
PerlMonks  

Using up-to-date currency conversion rates

by alexm (Chaplain)
on Sep 05, 2008 at 17:11 UTC ( #709343=CUFP: print w/ replies, xml ) Need Help??

A friend of mine is living this year in the USA, and she asked me how to program with Perl some unit conversions. The currency conversion is volatile since the ratio changes a lot, so I came up with this:

use strict; use warnings; use Locale::Currency; use File::Basename; use Cache::FileCache; use Finance::Quote; die "usage: $0 amount currency_from currency_to\n" unless @ARGV == 3; my $amount = shift @ARGV; my ($currency_from, $currency_to) = map { uc } @ARGV; for my $code ($currency_from, $currency_to) { die "sorry, currency $code not in ISO 4217\n" unless defined code2currency($code); } my ($filename) = fileparse($0, '.pl'); my $cache = Cache::FileCache->new({ cache_root => "$ENV{HOME}/.$filename", default_expires_in => '1 day', }); my $quote = Finance::Quote->new(); my $ratio = $cache->get("$currency_from:$currency_to"); $ratio = $quote->currency($currency_from, $currency_to) unless defined $ratio; die "sorry, cannot convert from $currency_from to $currency_to\n" unless defined $ratio; $cache->set("$currency_from:$currency_to", $ratio); print "$amount $currency_from = ", $amount * $ratio, " $currency_to\n" +;

She liked it, and so do I because it's so simple that it doesn't need comments at all. Just try:

$ perl exchange.pl 100 usd eur 100 USD = 70.22 EUR

Update: added ISO 4217 currency code check per graff's request.

Comment on Using up-to-date currency conversion rates
Select or Download Code
Re: Using up-to-date currency conversion rates
by graff (Chancellor) on Sep 06, 2008 at 17:25 UTC
    Thanks(++) for a really nice example of using Cache::FileCache -- exactly the kind of thing that's good to learn in general. The only thing I would add is some way to get the list of know currency abbreviations (and their full-length names), so that you could trap an unknown currency spec before sending it off to Yahoo via Finance::Quote -- and you could even show the clueless user what the available currency codes/names are.

    There's bound to be some easy-to-fetch reference list for that; or maybe you could take the time to pull in the contents of http://uk.finance.yahoo.com/currency-converter, which appears to contain the full menu of available choices (hence, the set that Finance::Quote is able to service), and parse out the contents of that menu.

      The only thing I would add is some way to get the list of know currency abbreviations (and their full-length names)

      Great idea. Such functionality, were it to be written , should be submitted to Finance::Quote's author for possible inclusion.

        I agree, but the only way to have the actual list of working currency codes would be to fetch them from Yahoo or any other similar site. As I said, most ISO currency codes are not usually published in financial sites, thus the currency list would have to be considered volatile.

      add is some way to get the list of know currency abbreviations (and their full-length names), so that you could trap an unknown currency spec before sending it off to Yahoo via Finance::Quote

      I updated code with Locale::Currency::code2currency() checks to avoid querying Yahoo with invalid ISO 4217 codes. However, there are some valid codes that are not available either, like ADP (Andorran Peseta).

      show the clueless user what the available currency codes/names are

      The problem is that most of the 203 codes in ISO 4217 are not available via Finance::Quote, so the list would not be very accurate in terms of helping a clueless user.

      Update: fixed broken module links.

      ... currency abbreviations (and their full-length names)
      Geography::Countries does this, but it's outdated (not updated in 5 years), and attempts to contact the developer has been unsuccessful so far.

      However, there are developer releases of the same module, such as this, which could be useful. The diff between the two looks like this:

      Update: I have confused country codes with currency codes. Sorry.
      --
      No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

        AFAICS, Geography::Countries deals only with country codes, not currency abbreviations (they're different ISO standards), so I don't see how does it help.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://709343]
Approved by kyle
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2014-04-23 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (557 votes), past polls