Skip to content

Instantly share code, notes, and snippets.

Forked from masak/currency.p6
Created Jul 4, 2012
What would you like to do?
Having fun with currencies in Perl 6
enum Currency <EUR USD>;
multi rate(EUR) { 1.252 }
multi rate(USD) { 1.000 }
class Money {
has Real $.amount;
has Currency $.currency;
method convert_to(Currency $currency) {
my $self-rate = rate $.currency;
my $other-rate = rate $currency;
my $amount = $.amount * $self-rate / $other-rate;$amount, :$currency);
method Str() {
"$.amount $.currency"
sub postfix:<EUR> {$^amount, :currency(EUR)) }
sub postfix:<USD> {$^amount, :currency(USD)) }
my $money = 1000\ EUR;
say "$money is $money.convert_to(USD)";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment