Skip to content

Instantly share code, notes, and snippets.

@bbkr
Forked from masak/currency.p6
Created Jul 4, 2012
Embed
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;
Money.new(:$amount, :$currency);
}
method Str() {
"$.amount $.currency"
}
}
sub postfix:<EUR> { Money.new(:$^amount, :currency(EUR)) }
sub postfix:<USD> { Money.new(:$^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