Today I propose a pragma that changes how numbers are presented in Perl
6. The idea arises from a discussion on the freenode channel #perl6,
available here: http://irclog.perlgeek.de/perl6/2010-09-01#i_2773432
The most important thing to remember is that the presentation of numbers
in Perl 6 is different from the number itself, the way we use numbers in
our code is different from how Perl 6 handles them internally. We only
chose for numbers in our code to be presented and treated as being in
base 10 because that's how most of us count.
The pragma I propose is use base, which takes one positional argument
that denotes what base you want Perl 6 to interpret and present numbers
in your code. For example, use base 16 tells Perl 6 to handle all
numbers as if they're in base 16, and to present them as base 16. Here,
I present an example use:
my $a = 10;
say $a;
This code does what it always does. It puts the number 0d10 into $a, and
then say outputs `10'. But this code:
use base 16;
my $a = 10;
say $a;
puts the number 0x10 into $a, and outputs `10'. Here, say $a.fmt('%d')
would output `16'.
Now, this may cause some confusion in some cases, particularly with the
radix conversion :10() and such. In the default base 10, :10($a) is
interpreted to mean that $a is in base 10, and to convert it to how Perl
6 currently presents numbers, base 10. Naturally, this is pretty
useless. In base 16 mode, :10($a) is interpreted to mean that $a is in
base 16 (because the 10 here is now 0x10), and to convert it to base 16,
and is just as useless. In base 16 mode, :A($a) would be used instead
to convert base 10 to base 16.
If you see any significant problems or have suggestions, then feel free
to reply. I feel this will help with making non-10 bases easier to deal
with in applications where its necessary.
-lue