This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
I sometimes outsmart myself.
[perl5.git] / lib / Locale / Currency.pm
CommitLineData
47a334e9
JH
1#-----------------------------------------------------------------------
2
3=head1 NAME
4
5Locale::Currency - ISO three letter codes for currency identification (ISO 4217)
6
7=head1 SYNOPSIS
8
9 use Locale::Currency;
10
11 $curr = code2currency('usd'); # $curr gets 'US Dollar'
12 $code = currency2code('Euro'); # $code gets 'eur'
13
14 @codes = all_currency_codes();
15 @names = all_currency_names();
16
17=cut
18
19#-----------------------------------------------------------------------
20
21package Locale::Currency;
22use strict;
23require 5.002;
24
25#-----------------------------------------------------------------------
26
27=head1 DESCRIPTION
28
29The C<Locale::Currency> module provides access to the ISO three-letter
30codes for identifying currencies and funds, as defined in ISO 4217.
31You can either access the codes via the L<conversion routines>
32(described below),
33or with the two functions which return lists of all currency codes or
34all currency names.
35
36There are two special codes defined by the standard which aren't
37understood by this module:
38
39=over 4
40
41=item XTS
42
43Specifically reserved for testing purposes.
44
45=item XXX
46
47For transactions where no currency is involved.
48
49=back
50
51=cut
52
53#-----------------------------------------------------------------------
54
55require Exporter;
56
57#-----------------------------------------------------------------------
58# Public Global Variables
59#-----------------------------------------------------------------------
60use vars qw($VERSION @ISA @EXPORT);
61$VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
62@ISA = qw(Exporter);
63@EXPORT = qw(&code2currency &currency2code
64 &all_currency_codes &all_currency_names );
65
66#-----------------------------------------------------------------------
67# Private Global Variables
68#-----------------------------------------------------------------------
69my %CODES = ();
70my %CURRENCIES = ();
71
72
73#=======================================================================
74
75=head1 CONVERSION ROUTINES
76
77There are two conversion routines: C<code2currency()> and C<currency2code()>.
78
79=over 8
80
81=item code2currency()
82
83This function takes a three letter currency code and returns a string
84which contains the name of the currency identified. If the code is
85not a valid currency code, as defined by ISO 4217, then C<undef>
86will be returned.
87
88 $curr = code2currency($code);
89
90=item currency2code()
91
92This function takes a currency name and returns the corresponding
93three letter currency code, if such exists.
94If the argument could not be identified as a currency name,
95then C<undef> will be returned.
96
97 $code = currency2code('French Franc');
98
99The case of the currency name is not important.
100See the section L<KNOWN BUGS AND LIMITATIONS> below.
101
102=back
103
104=cut
105
106#=======================================================================
107sub code2currency
108{
109 my $code = shift;
110
111
112 return undef unless defined $code;
113 $code = lc($code);
114 if (exists $CODES{$code})
115 {
116 return $CODES{$code};
117 }
118 else
119 {
120 #---------------------------------------------------------------
121 # no such currency code!
122 #---------------------------------------------------------------
123 return undef;
124 }
125}
126
127sub currency2code
128{
129 my $curr = shift;
130
131
132 return undef unless defined $curr;
133 $curr = lc($curr);
134 if (exists $CURRENCIES{$curr})
135 {
136 return $CURRENCIES{$curr};
137 }
138 else
139 {
140 #---------------------------------------------------------------
141 # no such currency!
142 #---------------------------------------------------------------
143 return undef;
144 }
145}
146
147#=======================================================================
148
149=head1 QUERY ROUTINES
150
151There are two function which can be used to obtain a list of all
152currency codes, or all currency names:
153
154=over 8
155
156=item C<all_currency_codes()>
157
158Returns a list of all three-letter currency codes.
159The codes are guaranteed to be all lower-case,
160and not in any particular order.
161
162=item C<all_currency_names()>
163
164Returns a list of all currency names for which there is a corresponding
165three-letter currency code. The names are capitalised, and not returned
166in any particular order.
167
168=back
169
170=cut
171
172#=======================================================================
173sub all_currency_codes
174{
175 return keys %CODES;
176}
177
178sub all_currency_names
179{
180 return values %CODES;
181}
182
183#-----------------------------------------------------------------------
184
185=head1 EXAMPLES
186
187The following example illustrates use of the C<code2currency()> function.
188The user is prompted for a currency code, and then told the corresponding
189currency name:
190
191 $| = 1; # turn off buffering
192
193 print "Enter currency code: ";
194 chop($code = <STDIN>);
195 $curr = code2currency($code);
196 if (defined $curr)
197 {
198 print "$code = $curr\n";
199 }
200 else
201 {
202 print "'$code' is not a valid currency code!\n";
203 }
204
205=head1 KNOWN BUGS AND LIMITATIONS
206
207=over 4
208
209=item *
210
211In the current implementation, all data is read in when the
212module is loaded, and then held in memory.
213A lazy implementation would be more memory friendly.
214
215=item *
216
217This module also includes the special codes which are
218not for a currency, such as Gold, Platinum, etc.
219This might cause a problem if you're using this module
220to display a list of currencies.
221Let Neil know if this does cause a problem, and we can
222do something about it.
223
224=item *
225
226ISO 4217 also defines a numeric code for each currency.
227Currency codes are not currently supported by this module.
228
229=item *
230
231There are three cases where there is more than one
232code for the same currency name.
233Kwacha has two codes: mwk for Malawi, and zmk for Zambia.
234The Russian Ruble has two codes: rub and rur.
235The Belarussian Ruble has two codes: byr and byb.
236The currency2code() function only returns one code, so
237you might not get back the code you expected.
238
239=back
240
241=head1 SEE ALSO
242
243=over 4
244
245=item Locale::Country
246
247ISO codes for identification of country (ISO 3166).
248Supports alpha-2, alpha-3, and numeric codes.
249The currency codes use the alpha-2 codeset.
250
251=item ISO 4217:1995
252
253Code for the representation of currencies and funds.
254
255=item http://www.bsi-global.com/iso4217currency
256
257Official web page for the ISO 4217 maintenance agency.
258This has the latest list of codes, in MS Word format. Boo.
259
260=back
261
262=head1 AUTHOR
263
264Michael Hennecke E<lt>hennecke@rz.uni-karlsruhe.deE<gt>
265and
266Neil Bowers E<lt>neilb@cre.canon.co.ukE<gt>
267
268=head1 COPYRIGHT
269
270Copyright (c) 2001 Michael Hennecke and
271Canon Research Centre Europe (CRE).
272
273This module is free software; you can redistribute it and/or
274modify it under the same terms as Perl itself.
275
276=cut
277
278#-----------------------------------------------------------------------
279
280#=======================================================================
281# initialisation code - stuff the DATA into the CODES hash
282#=======================================================================
283{
284 my $code;
285 my $currency;
286
287
288 while (<DATA>)
289 {
290 next unless /\S/;
291 chop;
292 ($code, $currency) = split(/:/, $_, 2);
293 $CODES{$code} = $currency;
294 $CURRENCIES{"\L$currency"} = $code;
295 }
296}
297
2981;
299
300__DATA__
301adp:Andorran Peseta
302aed:UAE Dirham
303afa:Afghani
304all:Lek
305amd:Armenian Dram
306ang:Netherlands Antillean Guilder
307aoa:Kwanza
308aon:New Kwanza
309aor:Kwanza Reajustado
310ars:Argentine Peso
311ats:Schilling
312aud:Australian Dollar
313awg:Aruban Guilder
314azm:Azerbaijanian Manat
315
316bam:Convertible Marks
317bbd:Barbados Dollar
318bdt:Taka
319bef:Belgian Franc
320bgl:Lev
321bgn:Bulgarian Lev
322bhd:Bahraini Dinar
323bhd:Dinar
324bif:Burundi Franc
325bmd:Bermudian Dollar
326bnd:Brunei Dollar
327bob:Boliviano
328bov:MVDol
329brl:Brazilian Real
330bsd:Bahamian Dollar
331btn:Ngultrum
332bwp:Pula
333byb:Belarussian Ruble
334byr:Belarussian Ruble
335bzd:Belize Dollar
336
337cad:Candian Dollar
338cdf:Franc Congolais
339chf:Swiss Franc
340clf:Unidades de Formento
341clp:Chilean Peso
342cny:Yuan Renminbi
343cop:Colombian Peso
344crc:Costa Rican Colon
345cup:Cuban Peso
346cve:Cape Verde Escudo
347cyp:Cyprus Pound
348czk:Czech Koruna
349
350dem:German Mark
351djf:Djibouti Franc
352dkk:Danish Krone
353dop:Dominican Peso
354dzd:Algerian Dinar
355
356ecs:Sucre
357ecv:Unidad de Valor Constante (UVC)
358eek:Kroon
359egp:Egyptian Pound
360ern:Nakfa
361esp:Spanish Peseta
362etb:Ethiopian Birr
363eur:Euro
364
365fim:Markka
366fjd:Fiji Dollar
367fkp:Falkland Islands Pound
368frf:French Franc
369
370gbp:Pound Sterling
371gel:Lari
372ghc:Cedi
373gip:Gibraltar Pound
374gmd:Dalasi
375gnf:Guinea Franc
376grd:Drachma
377gtq:Quetzal
378gwp:Guinea-Bissau Peso
379gyd:Guyana Dollar
380
381hkd:Hong Kong Dollar
382hnl:Lempira
383hrk:Kuna
384htg:Gourde
385huf:Forint
386
387idr:Rupiah
388iep:Irish Pound
389ils:Shekel
390inr:Indian Rupee
391iqd:Iraqi Dinar
392irr:Iranian Rial
393isk:Iceland Krona
394itl:Italian Lira
395
396jmd:Jamaican Dollar
397jod:Jordanian Dinar
398jpy:Yen
399
400kes:Kenyan Shilling
401kgs:Som
402khr:Riel
403kmf:Comoro Franc
404kpw:North Korean Won
405krw:Won
406kwd:Kuwaiti Dinar
407kyd:Cayman Islands Dollar
408kzt:Tenge
409
410lak:Kip
411lbp:Lebanese Pound
412lkr:Sri Lanka Rupee
413lrd:Liberian Dollar
414lsl:Loti
415ltl:Lithuanian Litas
416luf:Luxembourg Franc
417lvl:Latvian Lats
418lyd:Libyan Dinar
419
420mad:Moroccan Dirham
421mdl:Moldovan Leu
422mgf:Malagasy Franc
423mkd:Denar
424mmk:Kyat
425mnt:Tugrik
426mop:Pataca
427mro:Ouguiya
428mtl:Maltese Lira
429mur:Mauritius Rupee
430mvr:Rufiyaa
431mwk:Kwacha
432mxn:Mexican Nuevo Peso
433myr:Malaysian Ringgit
434mzm:Metical
435
436nad:Namibia Dollar
437ngn:Naira
438nio:Cordoba Oro
439nlg:Netherlands Guilder
440nok:Norwegian Krone
441npr:Nepalese Rupee
442nzd:New Zealand Dollar
443
444omr:Rial Omani
445
446pab:Balboa
447pen:Nuevo Sol
448pgk:Kina
449php:Philippine Peso
450pkr:Pakistan Rupee
451pln:Zloty
452pte:Portuguese Escudo
453pyg:Guarani
454
455qar:Qatari Rial
456
457rol:Leu
458rub:Russian Ruble
459rur:Russian Ruble
460rwf:Rwanda Franc
461
462sar:Saudi Riyal
463sbd:Solomon Islands Dollar
464scr:Seychelles Rupee
465sdd:Sudanese Dinar
466sek:Swedish Krona
467sgd:Singapore Dollar
468shp:St. Helena Pound
469sit:Tolar
470skk:Slovak Koruna
471sll:Leone
472sos:Somali Shilling
473srg:Surinam Guilder
474std:Dobra
475svc:El Salvador Colon
476syp:Syrian Pound
477szl:Lilangeni
478
479thb:Baht
480tjr:Tajik Ruble
481tmm:Manat
482tnd:Tunisian Dollar
483top:Pa'anga
484tpe:Timor Escudo
485trl:Turkish Lira
486ttd:Trinidad and Tobago Dollar
487twd:New Taiwan Dollar
488tzs:Tanzanian Shilling
489
490uah:Hryvnia
491uak:Karbovanets
492ugx:Uganda Shilling
493usd:US Dollar
494usn:US Dollar (Next day)
495uss:US Dollar (Same day)
496uyu:Peso Uruguayo
497uzs:Uzbekistan Sum
498
499veb:Bolivar
500vnd:Dong
501vuv:Vatu
502
503wst:Tala
504
505xaf:CFA Franc BEAC
506xag:Silver
507xau:Gold
508xba:European Composite Unit
509xbb:European Monetary Unit
510xbc:European Unit of Account 9
511xb5:European Unit of Account 17
512xcd:East Caribbean Dollar
513xdr:SDR
514xeu:ECU (until 1998-12-31)
515xfu:UIC-Franc
516xfo:Gold-Franc
517xof:CFA Franc BCEAO
518xpd:Palladium
519xpf:CFP Franc
520xpt:Platinum
521
522yer:Yemeni Rial
523yum:New Dinar
524
525zal:Financial Rand
526zar:Rand
527zmk:Kwacha
528zrn:New Zaire
529zwd:Zimbabwe Dollar