Specifying a Perl 6 Class in a Variable

I have a bunch of Perl 6 tests that start with some basic tests, where I put the class name to test in a variable that uses this variable during the test:

my $package = 'Some::Class'; use-ok $package; my $class = ::($package); can-ok $class, 'new'; 

I did not pay attention to this a bit, but it does not work anymore, because classes are lexically loaded now :

There is no such symbol "Some :: Class"

It's not hard. Download the module without use-ok and to the area where I want ::($package) :

 use Some::Class; ... 

Other solutions (possibly shying away from the ugly EVAL ) have a problem I'm trying to avoid.

But I donโ€™t particularly like it, because the name is displayed twice in the file. I especially liked my previous working idiom, which I ported from Perl 5. If I wanted to change the name of the class, it appeared only once in the file. I could easily generate test tests (although this is not much harder to fix).

Is there a way to get back to the ideal I need? (Although I believe that lexical loading in the next version will be on the way again).

+8
perl6
source share
1 answer

To summarize this problem: you want to load modules using a character instead of hard coding. Using a constant should do this for you:

 constant some-module = 'Some::Module'; use ::(some-module); 

You can also load the module at runtime using require , which allows you to calculate the runtime values:

 my $some-module = 'Some::Module'; require ::($some-module); ::($some-module).foo 

It would be wise to do this by trying use-ok .

For extra credit, you may find the methods in this article useful. http://rakudo.org/2017/03/18/lexical-require-upgrade-info/

+7
source share

All Articles