Based on your idea, I made my own Perl Eurocalculator with the following changes:
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!
In reply to Calc::Euro (Re: The Euro)
by Juerd
in thread The Euro
by simon.proctor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |