【问题标题】:Perl6: use module inside other modulePerl6:在其他模块中使用模块
【发布时间】:2017-11-09 14:53:28
【问题描述】:

我有 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';

file0.pm6:

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

  submethod BUILD() {
    say "hello file 0";
  }
}

file1.pm6:

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

file2.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.pl6 中创建所有 3 个模块文件的实例,而是在 file1 中创建 file2 的实例,在 file0 中创建 file1 的实例。这样我只需要在 start.pl6 中创建一个 file0 的实例就可以看到相同的输出;

以下是我想到的更改:

file1.pm6:

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;
  }
}

file0.pm6:

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 中,行 使用库“文件夹1”; 使用“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]

我 file1,行 使用库“../folder2”; 使用“文件2”; 不起作用,但也没有给出错误。我只是得到输出: [0.31s完成]

最后,文件 start.pl6 应该如下所示以产生输出:

start.pl6:

use v6;
use lib ".";
use file0;

say 'start';
my $file0 = file0.new();
say $file0.mystr;
say 'end';

输出:

start
hello file 0
file 0
file 1
file 2
end

【问题讨论】:

  • 您是否尝试将use lib "folder2/file2.pl6" 替换为use lib "../folder2/file2.pm6"(在file1.pm6 中)?
  • 是的,奇怪的是我没有收到错误。我只是得到“[在 0.331 秒内完成]”。

标签: module filepath raku


【解决方案1】:

你试图做的事情对我来说毫无意义。看来您将这些模块随意放在文件夹中。


如果这些模块的名称确实有意义,那么我可能会如何构建它。

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;

END say "[Finished in {(now - $*INIT-INSTANT).fmt("%0.2fs")}";

use lib 'lib';
use file0;

say 'start';
my $file0 = file0.new;
say $file0.mystr;
say 'end';

lib\file0.pm6

use folder1::file1;

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

  submethod TWEAK() {
    say "hello file 0";
    $!mystr ~= "\n" ~ folder1::file1.new.mystr; 
  }
}

lib\folder1\file1.pm6

use folder2::file2;

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

  submethod TWEAK() {
    $!mystr ~= "\n" ~ folder2::file2.new.mystr;
  }
}

lib\folder2\file2.pm6

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

【讨论】:

  • 谢谢!这正是我想要的:)
【解决方案2】:
use lib "folder2/file2.pl6";

这并不像你认为的那样。 use lib 需要一个 directory,Perl 应该在其中查找模块,而不是某些脚本的路径。

如果您的 My.pm6 在 ./lib 中(相对于当前工作目录),那么

use lib "lib";
use My;

成功了。也可以使用绝对路径

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

lib

【讨论】:

  • 在我的测试中,我看不出它是如何工作的。根据错误信息,use lib ... just plain 不能在模块中使用。测试很简单:mkdir lib; echo "use lib 'lib';" &gt; test.pm6; perl6 -I. -Mtest -e "say 'success'"
  • 你没有将“use lib”放入模块中,而是将其放入“使用”模块的脚本中。
  • 明白了。我误解了你说的把代码放在哪里。模块名称应该已经提示我了。
  • @Holli 这是否意味着您不能在另一个模块中使用一个模块?
  • 不,当然不是。这只是意味着模块不关心路径名。你需要做的就是告诉 Perl 从哪里开始开始 寻找 for 模块。您可以通过在使用代码(也称为脚本)中使用“使用 lib”或通过将 PERL6LIB 环境变量设置为指向 Perl 将开始搜索模块的目录来执行此操作。
猜你喜欢
  • 2019-07-18
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多