Skip to content

Instantly share code, notes, and snippets.

@moritz
Created April 12, 2012 12:29
Show Gist options
  • Save moritz/2366919 to your computer and use it in GitHub Desktop.
Save moritz/2366919 to your computer and use it in GitHub Desktop.
Perl 6: dynamically generate exports in Rakudo
module A {
BEGIN {
my $name = 'foo';
my $x = sub { say 'OH HAI from &foo' } but role { method name { $name } };
trait_mod:<is>(:export, $x);
}
}
$ ./perl6 -e 'use A; foo'
OH HAI from &foo
@Xliff
Copy link

Xliff commented Aug 31, 2019

Wow, this still works! However, my version runs very slow on the latest rakudo. Is there a better mechanism?!

$ cat test.pm6
module A { 
        BEGIN { 
                my $name = 'foo'; 
                my $a = sub { say 'OHAI!' } but role { 
                        method name { $name } 
                }; 
                trait_mod:<is>(:export, $a) 
        } 
}
$ perl6 --stagestats -I. -e 'use test; foo'
Stage start      :   0.000
Stage parse      :  19.197
Stage syntaxcheck:   0.000
Stage ast        :   0.000
Stage optimize   :   0.001
Stage mast       :   0.003
Stage mbc        :   0.000
Stage moar       :   0.000
OHAI!
$ perl6 --version
This is Rakudo version 2019.07.1-171-g2a5b642de built on MoarVM version 2019.07.1-72-g352ae27e4
implementing Perl 6.d.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment