# π Currency converter written in Perl 6

Parse the string with a currency converting request such as β10 EUR in USDβ and print the result.

The task of understanding free text is quite complicated. For the currency conversion, we can create a simple regex that matches the most common quires.

Letβs ignore the way the exchange rate data are obtained and use the hard-coded values:

`my %EUR =    AUD => 1.4994,  CAD => 1.4741,    CHF => 1.1504,  CNY => 7.7846,    DKK => 7.4439,  GBP => 0.89148,    ILS => 4.1274,  JPY => 131.94,    RUB => 67.471,  USD => 1.1759;`

This hash contains the exchange rates of a few currencies to Euro. Thus, it is possible to directly use it to get the results for any pair with EUR, such as:

`10 EUR in GBP20 ILS toEUR`

For other combinations, a cross-rate can be used. For example, the request to convert JPY to CHF uses the values of JPY-to-EUR and EUR-to-CHF conversion.

Here is a regex that parses the textual request:

`\$request ~~     /(<[\d .]>+) \s* (<:alpha> ** 3) .* (<:alpha> ** 3)/;`

Letβs also make a loop to accept multiple requests from the keyboard.

Here is the program:

`while (my \$request = prompt('> ')) {    \$request ~~         /(<[\d .]>+) \s* (<:alpha> ** 3) .* (<:alpha> ** 3)/;    my (\$amount, \$from, \$to) = \$0, \$1, \$2;        my \$result = 0;    if \$to eq 'EUR' {       \$result = \$amount / %EUR{\$from};    }    elsif \$from eq 'EUR' {       \$result = \$amount * %EUR{\$to};    }    else {       \$result = \$amount * %EUR{\$to} / %EUR{\$from};    }    say "\$amount \$from = \$result \$to";}`

After the regex match, the values are copied to the three variables:Β `\$amount`,Β `\$from`, andΒ `\$to`. TheΒ `if`β`elsif`β`else` chain is used to choose how the new amount is calculated: either as direct or cross rate.

Run the program and enter different requests with both integer and floating-point values for different combinations of the currency codes listed in theΒ `%EUR` hash.

Here are a few ideas of how to improve the program:

1. Check if the entered currency code exists.
2. Make the request case-insensitive.
3. Create another hash with exchange rates against USD and choose it, whenever possible, to avoid cross-calculations.