Is it possible to overload constants in Perl 6? Here is the Perl 5 example I'm looking at.
In particular I would like to get a string of the literal value used, e.g. if the code was
my $x = .1e-003 ;
I need ".1e-003"
instead of 0.0001
.
I just added such a module:
https://github.com/FROGGS/p6-overload-constant
USAGE:
use v6;
sub decimal { $^a.flip }
use overload::constant &decimal;
say .1e-003 # "300-e1."
You can change how a value stringifies by mixing in an appropriate role with the but
operator, ie
0.0001 but role { method Str { ".1e-003" } }
which can be shortened to
0.0001 but ".1e-003"
Note that providing a method Stringy
instead of Str
might actually be more appropriate from a semantic point of view, but I do not think Rakudo as of today handles that distinction correctly in all case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With