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:~$


In reply to Calc::Euro (Re: The Euro) by Juerd
in thread The Euro by simon.proctor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.