Search code examples
requireraku

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


Solution

  • $ cat XXX.rakumod
    unit class XXX;
    
    $ cat ZZZ.rakumod
    module ZZZ {
        require XXX;
        XXX.new;
        say "OK";
    }
    
    $ raku -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.rakumod
    unit class XXX;
    
    $ cat ZZZ.rakumod
    module ZZZ {
        require XXX;
        ::("XXX").new;
        say "OK";
    }
    
    $ raku -I. -e 'use ZZZ;'
    OK