I am trying to add «€» as an alias for the «$» scalar, and doing it with a Slang is the way to do it I think. But perl6.doc doesn't mention Slangs at all.
I have read the following:
- https://perlgeek.de/en/article/mutable-grammar-for-perl-6 (from 2008)
- https://mouq.github.io/slangs.html
And looked at the Slang::Roman and Slang::Tuxic modules.
The result is this file (ScalarEU.pm6):
use nqp;
unit module ScalarEU2;
sub EXPORT(|)
{
my role Euscalar
{
token sigil:sym<$> { '€' | '$' }
}
my Mu $MAIN-grammar := nqp::atkey(%*LANG, 'MAIN');
my $grammar := $MAIN-grammar.HOW.mixin($MAIN-grammar, Euscalar);
$*LANG.define_slang('MAIN', $grammar, $*LANG.actions);
{}
}
Then a program (called hello) using it:
use lib "lib";
use ScalarEU;
sub MAIN ($name)
{
say "Hello, €name!";
}
But it doesn't work, or rather, doesn't do what it should:
$ ./hello Tom
Hello, €name!
(I wrote the program this way so that it doesn't crash.)
I haven't added an action class, but the way the "token sigil" is set up shouldn't require that? But that assumption is based on an 11 year article, and may be wrong.
Also, https://github.com/rakudo/rakudo/issues/2404 says that $*LANG is obsolete, and to use $?LANG instead. REPL agrees:
> $*LANG
Dynamic variable $*LANG not found
> $?LANG
(low-level object `Perl6::Grammar`)
But programs can use both, without errors. (I have tried.)