导入简单模块

Import simple module

我在模块文件 (.pm) 中编写了一个函数,并想在 Perl6 文件 (.pl6) 中使用它。这两个文件在同一个文件夹中:

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

我尝试使用 的答案,但我的代码返回了这个错误:

===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]

这是 .pm 文件,chrmodule1.pm:

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

这里是 .pl6 文件,testOfCode3.pl6:

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

foo();

testOfCode3.pl6的第二行应该是use lib 'perlCode';.


您写道:

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

因此,您已将模块存储在名为 perlCode 的文件夹中。

当你 运行 testOfCode3.pl6 你得到一个错误:

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

因此,Rakudo Perl 6 编译器在名为 modules 的文件夹中查找 chrmodule。为什么?因为你告诉它:

Here is the .pl6 file, testOfCode3.pl6:

use v6;
use lib 'modules';

use lib ... 语句告诉 Perl 6 编译器首先在哪里寻找模块。您添加了 modules,因此 Rakudo Perl 6 编译器首先查看 modules 文件夹。

它在那里没有找到你的模块所以它继续寻找 elsewhere。因此,行列表 C:\Users\Christian\.perl6

最后它永远找不到你的模块,因为你的模块在 perlCode 而你没有告诉编译器去那里找。 (出于可靠的安全原因,它拒绝只查看当前目录。)