Module Perl6:在其他模块内使用模块

Module Perl6:在其他模块内使用模块,module,filepath,raku,Module,Filepath,Raku,我有4个文件 C:\perlCode2\start.pl6 C:\perlCode2\file0.pm6 C:\perlCode2\folder1\file1.pm6 C:\perlCode2\folder2\file2.pm6 start.pl6用于运行我的程序。3个模块文件包含或生成start.pl6最终使用的数据。我使用atom.io来运行代码 代码如下: start.pl6: use v6; use lib "."; use file0; use lib "folder1"; use

我有4个文件

  • C:\perlCode2\start.pl6
  • C:\perlCode2\file0.pm6
  • C:\perlCode2\folder1\file1.pm6
  • C:\perlCode2\folder2\file2.pm6
start.pl6用于运行我的程序。3个模块文件包含或生成start.pl6最终使用的数据。我使用atom.io来运行代码

代码如下:

start.pl6:

use v6;
use lib ".";
use file0;
use lib "folder1";
use file1;
use lib "folder2";
use file2;

say 'start';
my $file0 = file0.new();
say $file0.mystr;
my $file1 = file1.new();
say $file1.mystr;
my $file2 = file2.new();
say $file2.mystr;
say 'end';
use v6;
use lib ".";
use file0;

say 'start';
my $file0 = file0.new();
say $file0.mystr;
say 'end';
文件0.pm6:

class file0 is export {
  has Str $.mystr = "file 0";

  submethod BUILD() {
    say "hello file 0";
  }
}
use lib "folder1";
use "file1.pl6"; 

class file0 is export {
  has Str $.mystr = "file 0";

  submethod BUILD() {
    say "hello file 0";
    my $file1 = file1.new();
    $!mystr = $!mystr ~ "\n" ~ $file1.mystr; 
  }
}
文件1.pm6:

class file1 is export {
  has Str $.mystr = "file 1";
}
use lib "../folder2";
use "file2.pl6"; 

class file1 is export {
  has Str $.mystr = "file 1";

  submethod BUILD() {
    my $file2 = file2.new();
    $!mystr = $!mystr ~ "\n" ~ $file2.mystr; 
        # I want to instantiate file2 inside the constructor, 
        # so I can be sure the line
        # $!mystr = $!mystr ~ "\n" ~ $file2.mystr; 
        # takes effect before i call any of file0's methods;
  }
}
文件2.pm6:

class file2 is export {
  has Str $.mystr = "file 2";
}
输出:

start
hello file 0
file 0
file 1
file 2
end
[Finished in 0.51s]
start
hello file 0
file 0
file 1
file 2
end
我不想在start.pl6中创建所有3个模块文件的实例,而是想在file1中创建file2的实例,在file0中创建file1的实例。这样,我只需要在start.pl6中创建一个file0的实例就可以看到相同的输出

以下是我想到的变化:

文件1.pm6:

class file1 is export {
  has Str $.mystr = "file 1";
}
use lib "../folder2";
use "file2.pl6"; 

class file1 is export {
  has Str $.mystr = "file 1";

  submethod BUILD() {
    my $file2 = file2.new();
    $!mystr = $!mystr ~ "\n" ~ $file2.mystr; 
        # I want to instantiate file2 inside the constructor, 
        # so I can be sure the line
        # $!mystr = $!mystr ~ "\n" ~ $file2.mystr; 
        # takes effect before i call any of file0's methods;
  }
}
文件0.pm6:

class file0 is export {
  has Str $.mystr = "file 0";

  submethod BUILD() {
    say "hello file 0";
  }
}
use lib "folder1";
use "file1.pl6"; 

class file0 is export {
  has Str $.mystr = "file 0";

  submethod BUILD() {
    say "hello file 0";
    my $file1 = file1.new();
    $!mystr = $!mystr ~ "\n" ~ $file1.mystr; 
  }
}
在file0中,行 使用lib“folder1”; 使用“file1.pl6”; 产生以下错误:

===SORRY!=== Error while compiling C:\perlCode2\file0.pm6 (file0)
'use lib' may not be pre-compiled
at C:\perlCode2\file0.pm6 (file0):2
------> use lib "folder1/file1.pl6"<HERE>;
[Finished in 0.584s]
输出:

start
hello file 0
file 0
file 1
file 2
end
[Finished in 0.51s]
start
hello file 0
file 0
file 1
file 2
end
这和你想象的不一样。use lib需要一个目录,Perl应该在其中查找模块,而不是某些脚本的路径

如果My.pm6位于./lib(相对于当前工作目录),则

这就是诀窍。也可以使用绝对路径

use lib "~/projects/perl6/MyProject/lib";
use My;

看。

你想做的对我来说毫无意义。看起来你是在随意地把这些模块放在文件夹里


如果这些模块的名称真的有意义,那么我可以在这里对其进行结构分析

C:\perlCode2\start.pl6
C:\perlCode2\lib\file0.pm6
C:\perlCode2\lib\folder1\file1.pm6
C:\perlCode2\lib\folder2\file2.pm6
start.pl6:

use v6;
use lib ".";
use file0;
use lib "folder1";
use file1;
use lib "folder2";
use file2;

say 'start';
my $file0 = file0.new();
say $file0.mystr;
my $file1 = file1.new();
say $file1.mystr;
my $file2 = file2.new();
say $file2.mystr;
say 'end';
use v6;
use lib ".";
use file0;

say 'start';
my $file0 = file0.new();
say $file0.mystr;
say 'end';
使用v6;
结束时说“[完成于{(现在-$*INIT-INSTANT).fmt(“%0.2fs”)}”;
使用lib'lib';
使用file0;
说“开始”;
my$file0=file0.new;
比如$file0.mystr;
说“结束”;
lib\file0.pm6:

class file0 is export {
  has Str $.mystr = "file 0";

  submethod BUILD() {
    say "hello file 0";
  }
}
use lib "folder1";
use "file1.pl6"; 

class file0 is export {
  has Str $.mystr = "file 0";

  submethod BUILD() {
    say "hello file 0";
    my $file1 = file1.new();
    $!mystr = $!mystr ~ "\n" ~ $file1.mystr; 
  }
}
使用folder1::file1;
类文件0已导出{
有Str$.mystr=“文件0”;
子方法调整(){
说“你好文件0”;
$!mystr~=“\n”~folder1::file1.new.mystr;
}
}
lib\folder1\file1.pm6:

class file1 is export {
  has Str $.mystr = "file 1";
}
use lib "../folder2";
use "file2.pl6"; 

class file1 is export {
  has Str $.mystr = "file 1";

  submethod BUILD() {
    my $file2 = file2.new();
    $!mystr = $!mystr ~ "\n" ~ $file2.mystr; 
        # I want to instantiate file2 inside the constructor, 
        # so I can be sure the line
        # $!mystr = $!mystr ~ "\n" ~ $file2.mystr; 
        # takes effect before i call any of file0's methods;
  }
}
使用folder2::file2;
folder1::file1类是导出类{
有Str$.mystr=“文件1”;
子方法调整(){
$!mystr~=“\n”~folder2::file2.new.mystr;
}
}
lib\folder2\file2.pm6

class folder2::file2正在导出{
有Str$.mystr=“文件2”;
}


您是否尝试过将
使用库“folder2/file2.pl6”
替换为
使用库“./folder2/file2.pm6”
(在
文件1.pm6
中)?是的,奇怪的是,我没有收到错误。我只是得到“[在0.331s内完成]”。在我的测试中,我不知道它是如何工作的。根据错误消息,
use lib…
just plain不能在模块中使用。测试非常简单:
mkdir lib;echo“use lib'lib';”>test.pm6;perl6-I.-Mtest-e“say'success'”
如果不将“use lib”放入模块,则将其放入“use”脚本中这是module.Gotcha。我误解了你要把代码放在哪里。模块名应该把我包括进去。@Holli这是不是意味着你不能在另一个模块中使用模块?不,当然不是。这只是意味着模块不关心路径名。你需要做的就是告诉Perl从哪里开始寻找模块。你可以通过e我是在消费代码(又称脚本)中使用“use lib”,还是将PERL6LIB环境变量设置为指向一个目录,Perl将再次开始搜索模块。谢谢!这正是我要找的:)