Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you only import `for` from `Perl6::Controls`?

Tags:

perl

Test case:

use 5.026;
use Perl6::Controls qw(for);

for (1..10) -> $n {
    say $n;
}

loop {};

Expect:

Can't call method "loop" without a package or object reference

Got:

infinite loop

like image 620
daxim Avatar asked Aug 02 '18 13:08

daxim


2 Answers

use Perl6::Controls qw(for);
BEGIN {
    delete $^H{'Keyword::Simple/keywords'}{"loop"};
}
...

which I stumbled into running your script through B::Deparse.


To pick and choose the keywords you want to keep, you could say

use Perl6::Controls;
BEGIN {
    my @keep = ...;    # e.g.  @keep = qw(for);
    my %keywords;
    @keywords{@keep} = @{$^H{'Keyword::Simple/keywords'}}{@keep};
    $^H{'Keyword::Simple/keywords'} = \%keywords;
}
like image 55
mob Avatar answered Sep 27 '22 21:09

mob


You can't. Looking at the source-code for Perl6::Controls it uses it's own import method to define all the new keywords using Keyword::Declare. It ignores any parameters passed on the use line.

like image 37
JGNI Avatar answered Sep 27 '22 20:09

JGNI