{PATCH] Re: Lexical scoping bug with EXPR for EXPR?
[p5sagit/p5-mst-13.2.git] / lib / Locale / Currency.pm
CommitLineData
47a334e9 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);
6b6e008c 61$VERSION = sprintf("%d.%02d", q$Revision: 2.0 $ =~ /(\d+)\.(\d+)/);
47a334e9 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.
6b6e008c 227Currency codes are not currently supported by this module,
228in the same way Locale::Country supports multiple codesets.
47a334e9 229
230=item *
231
232There are three cases where there is more than one
233code for the same currency name.
234Kwacha has two codes: mwk for Malawi, and zmk for Zambia.
235The Russian Ruble has two codes: rub and rur.
236The Belarussian Ruble has two codes: byr and byb.
237The currency2code() function only returns one code, so
238you might not get back the code you expected.
239
240=back
241
242=head1 SEE ALSO
243
244=over 4
245
246=item Locale::Country
247
248ISO codes for identification of country (ISO 3166).
6b6e008c 249
250=item Locale::Script
251
252ISO codes for identification of written scripts (ISO 15924).
47a334e9 253
254=item ISO 4217:1995
255
256Code for the representation of currencies and funds.
257
258=item http://www.bsi-global.com/iso4217currency
259
260Official web page for the ISO 4217 maintenance agency.
261This has the latest list of codes, in MS Word format. Boo.
262
263=back
264
265=head1 AUTHOR
266
267Michael Hennecke E<lt>hennecke@rz.uni-karlsruhe.deE<gt>
268and
6b6e008c 269Neil Bowers E<lt>neil@bowers.comE<gt>
47a334e9 270
271=head1 COPYRIGHT
272
273Copyright (c) 2001 Michael Hennecke and
274Canon Research Centre Europe (CRE).
275
276This module is free software; you can redistribute it and/or
277modify it under the same terms as Perl itself.
278
279=cut
280
281#-----------------------------------------------------------------------
282
283#=======================================================================
284# initialisation code - stuff the DATA into the CODES hash
285#=======================================================================
286{
287 my $code;
288 my $currency;
289
290
291 while (<DATA>)
292 {
293 next unless /\S/;
294 chop;
295 ($code, $currency) = split(/:/, $_, 2);
296 $CODES{$code} = $currency;
297 $CURRENCIES{"\L$currency"} = $code;
298 }
299}
300
3011;
302
303__DATA__
304adp:Andorran Peseta
305aed:UAE Dirham
306afa:Afghani
307all:Lek
308amd:Armenian Dram
309ang:Netherlands Antillean Guilder
310aoa:Kwanza
311aon:New Kwanza
312aor:Kwanza Reajustado
313ars:Argentine Peso
314ats:Schilling
315aud:Australian Dollar
316awg:Aruban Guilder
317azm:Azerbaijanian Manat
318
319bam:Convertible Marks
320bbd:Barbados Dollar
321bdt:Taka
322bef:Belgian Franc
323bgl:Lev
324bgn:Bulgarian Lev
325bhd:Bahraini Dinar
326bhd:Dinar
327bif:Burundi Franc
328bmd:Bermudian Dollar
329bnd:Brunei Dollar
330bob:Boliviano
331bov:MVDol
332brl:Brazilian Real
333bsd:Bahamian Dollar
334btn:Ngultrum
335bwp:Pula
336byb:Belarussian Ruble
337byr:Belarussian Ruble
338bzd:Belize Dollar
339
340cad:Candian Dollar
341cdf:Franc Congolais
342chf:Swiss Franc
343clf:Unidades de Formento
344clp:Chilean Peso
345cny:Yuan Renminbi
346cop:Colombian Peso
347crc:Costa Rican Colon
348cup:Cuban Peso
349cve:Cape Verde Escudo
350cyp:Cyprus Pound
351czk:Czech Koruna
352
353dem:German Mark
354djf:Djibouti Franc
355dkk:Danish Krone
356dop:Dominican Peso
357dzd:Algerian Dinar
358
359ecs:Sucre
360ecv:Unidad de Valor Constante (UVC)
361eek:Kroon
362egp:Egyptian Pound
363ern:Nakfa
364esp:Spanish Peseta
365etb:Ethiopian Birr
366eur:Euro
367
368fim:Markka
369fjd:Fiji Dollar
370fkp:Falkland Islands Pound
371frf:French Franc
372
373gbp:Pound Sterling
374gel:Lari
375ghc:Cedi
376gip:Gibraltar Pound
377gmd:Dalasi
378gnf:Guinea Franc
379grd:Drachma
380gtq:Quetzal
381gwp:Guinea-Bissau Peso
382gyd:Guyana Dollar
383
384hkd:Hong Kong Dollar
385hnl:Lempira
386hrk:Kuna
387htg:Gourde
388huf:Forint
389
390idr:Rupiah
391iep:Irish Pound
392ils:Shekel
393inr:Indian Rupee
394iqd:Iraqi Dinar
395irr:Iranian Rial
396isk:Iceland Krona
397itl:Italian Lira
398
399jmd:Jamaican Dollar
400jod:Jordanian Dinar
401jpy:Yen
402
403kes:Kenyan Shilling
404kgs:Som
405khr:Riel
406kmf:Comoro Franc
407kpw:North Korean Won
408krw:Won
409kwd:Kuwaiti Dinar
410kyd:Cayman Islands Dollar
411kzt:Tenge
412
413lak:Kip
414lbp:Lebanese Pound
415lkr:Sri Lanka Rupee
416lrd:Liberian Dollar
417lsl:Loti
418ltl:Lithuanian Litas
419luf:Luxembourg Franc
420lvl:Latvian Lats
421lyd:Libyan Dinar
422
423mad:Moroccan Dirham
424mdl:Moldovan Leu
425mgf:Malagasy Franc
426mkd:Denar
427mmk:Kyat
428mnt:Tugrik
429mop:Pataca
430mro:Ouguiya
431mtl:Maltese Lira
432mur:Mauritius Rupee
433mvr:Rufiyaa
434mwk:Kwacha
435mxn:Mexican Nuevo Peso
436myr:Malaysian Ringgit
437mzm:Metical
438
439nad:Namibia Dollar
440ngn:Naira
441nio:Cordoba Oro
442nlg:Netherlands Guilder
443nok:Norwegian Krone
444npr:Nepalese Rupee
445nzd:New Zealand Dollar
446
447omr:Rial Omani
448
449pab:Balboa
450pen:Nuevo Sol
451pgk:Kina
452php:Philippine Peso
453pkr:Pakistan Rupee
454pln:Zloty
455pte:Portuguese Escudo
456pyg:Guarani
457
458qar:Qatari Rial
459
460rol:Leu
461rub:Russian Ruble
462rur:Russian Ruble
463rwf:Rwanda Franc
464
465sar:Saudi Riyal
466sbd:Solomon Islands Dollar
467scr:Seychelles Rupee
468sdd:Sudanese Dinar
469sek:Swedish Krona
470sgd:Singapore Dollar
471shp:St. Helena Pound
472sit:Tolar
473skk:Slovak Koruna
474sll:Leone
475sos:Somali Shilling
476srg:Surinam Guilder
477std:Dobra
478svc:El Salvador Colon
479syp:Syrian Pound
480szl:Lilangeni
481
482thb:Baht
483tjr:Tajik Ruble
484tmm:Manat
485tnd:Tunisian Dollar
486top:Pa'anga
487tpe:Timor Escudo
488trl:Turkish Lira
489ttd:Trinidad and Tobago Dollar
490twd:New Taiwan Dollar
491tzs:Tanzanian Shilling
492
493uah:Hryvnia
494uak:Karbovanets
495ugx:Uganda Shilling
496usd:US Dollar
497usn:US Dollar (Next day)
498uss:US Dollar (Same day)
499uyu:Peso Uruguayo
500uzs:Uzbekistan Sum
501
502veb:Bolivar
503vnd:Dong
504vuv:Vatu
505
506wst:Tala
507
508xaf:CFA Franc BEAC
509xag:Silver
510xau:Gold
511xba:European Composite Unit
512xbb:European Monetary Unit
513xbc:European Unit of Account 9
514xb5:European Unit of Account 17
515xcd:East Caribbean Dollar
516xdr:SDR
517xeu:ECU (until 1998-12-31)
518xfu:UIC-Franc
519xfo:Gold-Franc
520xof:CFA Franc BCEAO
521xpd:Palladium
522xpf:CFP Franc
523xpt:Platinum
524
525yer:Yemeni Rial
526yum:New Dinar
527
528zal:Financial Rand
529zar:Rand
530zmk:Kwacha
531zrn:New Zaire
532zwd:Zimbabwe Dollar