in reply to The Euro

Based on your idea, I made my own Perl Eurocalculator with the following changes:

  1. Has a cloneable object-oriented interface
  2. The euro is a currency too, so toCurrency isn't really clear => uses to_national
  3. Includes the Luxembourgian Franc (there are _12_ euro countries)
  4. Uses the official three-letter abbreviations
  5. Can handle user-defined rates
  6. Has both from_euro and to_national
  7. Update (200201021239+0100) Corrected wrong values: DEM (typo), FIM (typo), ATS

Without further ado, here's the module:

package Calc::Euro; use strict; use Carp; my %rates = qw( LUF 40.3399 ATS 13.7603 BEF 40.3399 NLG 2.20371 FIM 5.94573 FRF 6.55957 DEM 1.95583 GRD 340.75 IEP 0.787564 ITL 1936.27 PTE 200.482 ESP 166.386 ); sub new { my ($proto, $currency) = @_; my $rate = defined $currency && ( $rates{uc $currency} || 0 + $currency ) || ( ref $proto eq __PACKAGE__ ? $$proto : croak("Invalid currency") ); return bless \$rate, ref($proto) || $proto; } sub to_euro { my ($self, $amount) = @_; return $amount / $$self; } sub to_national { my ($self, $amount) = @_; return $amount * $$self; } sub from_euro { goto &to_national } sub from_national { goto &to_euro } sub clone { goto &new }

And of course a script to prove it works ;)

#!/usr/bin/perl -w use strict; use Calc::Euro; my $eurocalc = Calc::Euro->new('NLG'); print $eurocalc->to_national(1), "\n"; # 2.20371 my $anothercalc = $eurocalc->clone(); print $eurocalc->to_national(1), "\n"; # 2.20371 $eurocalc = Calc::Euro->new('BEF'); print $anothercalc->from_euro($eurocalc->to_euro(20)), "\n"; # approx 1 my $foo = Calc::Euro->new(1.5); print $foo->to_national(2), "\n"; # 3

Have fun!

2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Replies are listed 'Best First'.
Re: Calc::Euro (Re: The Euro)
by tstock (Curate) on Jan 02, 2002 at 08:22 UTC
    for the sake of simplicity, how about adding a EUR => 1 in the hash and getting rid of the National vs Euro distinction :)

    eurCalc($value, $from, $to)

    Tiago
      Well in the spirit of that request, heres an additional sub which you can add to the module. You must add the additional key value pair of 'EUR' => 1 to the rates hash or you'll have problems doing any euro conversions :)

      sub euroCalc { my ($self,$amount, $from,$to) = @_; croak("Invalid start currency") unless defined($rates{uc $from}); croak("Invalid target currency") unless defined($rates{uc $to}); # What was it in Euros # $amount = ($amount * $rates{$to}) / $rates{$from}; # Now convert to target currency # return $amount * $rates{$to}; # Or in one line return ($amount * $rates{$to}) / $rates{$from}; }


      And some test prints

      use strict; use Calc::Euro; my $eurocalc = Calc::Euro->new('NLG'); print $eurocalc->to_national(1), "\n"; # 2.20371 print $eurocalc->euroCalc($eurocalc->euroCalc(1,'NLG','EUR'),'EUR','NL +G'), "\n"; print $eurocalc->euroCalc(1,'NLG','BEF'), "\n"; print $eurocalc->euroCalc(1,'NLG','EUR'), "\n"; print $eurocalc->euroCalc(1,'EUR','NLG'), "\n";


      So you notice that you can go from one currency to Euros and then to your target currency. Might prove handy to someone.

      Have fun :)
        That'd be euro_calc, then ;) (perlstyle).
        Update (200201021252+0100) I like freestyle even better: $eurocalc->freestyle() :)

        BTW, I'd probably have used (from, to, amount) or auto-detect the order.

        2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$