Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

raku - What could be the reason that `require` doesn't work in some places?

Loading a module (ABC) with require works in one module of a distribution while it fails in another module of the distribution. What could be the reason that loading ABC with require fails in one place?

require Name::ABC;
my $new = Name::ABC.new(); # dies: You cannot create an instance of this type (ABC)

perl6 -v
This is Rakudo Star version 2019.03.1 built on MoarVM version 2019.03
implementing Perl 6.d.

The the required module: App::DBBrowser::Subqueries

App::DBBrowser::Union, line 80: OK *

App::DBBrowser::Join, lines 66 and 191: OK *

App::DBBrowser::Table::Extensions, line 49: OK *

App::DBBrowser, line 690: You cannot create an instance of this type (Subqueries) *

App::DBBrowser::CreateTable, line 112: You cannot create an instance of this type (Subqueries) *

* version 0.0.1

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
$ cat XXX.pm6
unit class XXX;

$ cat ZZZ.pm6
module ZZZ {
    require XXX;
    XXX.new;
    say "OK";
}

$ perl6 -I. -e 'use ZZZ;'
===SORRY!===
You cannot create an instance of this type (XXX)

From the documentation:

require loads a compunit and imports definite symbols at runtime.

You are doing a runtime load of a module while also expecting the symbols for that module to exist at compile time. Instead you should use indirect name lookup (as shown at the bottom of the documentation page linked earlier):

$ cat XXX.pm6
unit class XXX;

$ cat ZZZ.pm6
module ZZZ {
    require XXX;
    ::("XXX").new;
    say "OK";
}

$ perl6 -I. -e 'use ZZZ;'
OK

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...