Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a procedure when we have the name as a string?

Tags:

raku

Is it possible to call a procedure in Perl 6 when we have the name as a string?

    sub AAA { say "ok"; }
    my $sub = "AAA";
    # &$sub(); or something ?

I know that a dispatch table is a better idea:

    my %table; %table<a> = &AAA();
    &(%table<a>); # Execute "AAA"

I ask because calling a method where we have the name as a string is possible:

    say pi."$_"() for <Int Real Str>;
like image 985
Arne Sommer Avatar asked Jan 20 '19 21:01

Arne Sommer


1 Answers

You can use indirect name lookup:

sub AAA { say "ok"; }
my $sub = "AAA";
&::($sub)();
like image 156
ugexe Avatar answered Nov 23 '22 08:11

ugexe