Matthew wrote:

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'.

As someone who has implemented, and used, mini-languages with such a feature, I'd say that the confusion that it would cause does significantly outweigh any potential benefit.

Question: what would be the behavior of:

use base 8;
use base 10;
$a = 10;

I think that $a now holds the value 0d8, because 0o10 is 0d8. That is, "use base 10" would always be a no-op.

A pragma to control the default behavior of stringification would be less problematic -- indeed would be useful -- but even then it's probably more useful to have it as a property of the class/object that holds the value: { my $a = 10 but :stringification-base(16) }.

When you have a large number of constants that you want to hard-code into source code, it's probably better to scope it to an individual expression. Perhaps { my @a = :16< 12 1a 1b > } would get you most of the way.

I have no problem if you want to make it possible to write such a pragma (just subclass the grammar to change the behavior of numeric literals) but it should be something that lives in in a user module with a nice long scary name.

I'd also note that "use base" has an existing, different, meaning in Perl 5.

Reply via email to