Arimaafan
Arimaafan

Reputation: 167

Import simple module

I have a function written in a module file (.pm), and want to use it in a Perl6 file (.pl6). The two files are in the same folder:

C:\Users\Christian\Dropbox\ChristianPrivatefiler\Programmering\Perl6\perlCode

I tried to use the answer to Perl6: implicit and explicit import but my code returned this error:

===SORRY!=== Could not find chrmodule1 at line 5 in:
    C:\Users\Christian\Dropbox\ChristianPrivatefiler\Programmering\Perl6\modules
    C:\Users\Christian\.perl6
    C:\rakudo\share\perl6\site
    C:\rakudo\share\perl6\vendor
    C:\rakudo\share\perl6
    CompUnit::Repository::AbsolutePath<84241584>
    CompUnit::Repository::NQP<86530680>
    CompUnit::Repository::Perl5<86530720> [Finished in 0.436s]

Here is the .pm file, chrmodule1.pm:

module chrmodule1 {
    sub foo is export(:DEFAULT, :one) {
        say 'in foo';
    }
}

Here is the .pl6 file, testOfCode3.pl6:

use v6;
use lib 'modules';
use chrmodule1;

foo();

Upvotes: 3

Views: 360

Answers (1)

raiph
raiph

Reputation: 32464

The second line of testOfCode3.pl6 should be use lib 'perlCode';.


You wrote:

I have a function written in a module file (.pm), and want to use it in a Perl6 file (.pl6). The two files are in the same folder:

C:\Users\Christian\Dropbox\ChristianPrivatefiler\Programmering\Perl6\perlCode

So, you've stored the module in a folder called perlCode.

When you run testOfCode3.pl6 you get an error:

===SORRY!=== Could not find chrmodule1 at line 5 in:
    C:\Users\Christian\Dropbox\ChristianPrivatefiler\Programmering\Perl6\modules

So, the Rakudo Perl 6 compiler looked for chrmodule in a folder called modules. Why? Because you told it to:

Here is the .pl6 file, testOfCode3.pl6:

use v6;
use lib 'modules';

A use lib ... statement tells a Perl 6 compiler where to look first for modules. You've added modules, so the Rakudo Perl 6 compiler looked in the modules folder first.

It didn't find your module there so it continued on, looking elsewhere. Hence the lines listing C:\Users\Christian\.perl6 etc.

In the end it never finds your module because your module is in perlCode and you haven't told the compiler to look there. (And it refuses to just look in the current directory for solid security reasons.)

Upvotes: 5

Related Questions