我有4份文件。
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";
}
}文件1.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实例。这样,我只需在file0中创建一个start.pl6实例就可以看到相同的输出;
这里是我所想到的变化:
文件1.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中,行使用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]I file1,行使用lib“./folder2 2”;使用"file2";不工作,但也不会出现错误。我刚得到输出:在0.3s内完成
最后,文件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发布于 2017-11-14 17:42:52
你想做的事对我来说毫无意义。似乎你在任意地将这些模块放在文件夹中。
如果这些模块的名称确实有意义的话,那么我将如何构造它。
C:\perlCode2\start.pl6
C:\perlCode2\lib\file0.pm6
C:\perlCode2\lib\folder1\file1.pm6
C:\perlCode2\lib\folder2\file2.pm6start.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 1\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";
}发布于 2017-11-09 22:06:06
use lib "folder2/file2.pl6";这不像你想的那样。使用lib需要一个目录,其中Perl应该查找模块,而不是某个脚本的路径。
如果您的My.pm6位于./lib (相对于当前工作目录),那么
use lib "lib";
use My;就能做到这一点。您还可以使用绝对路径。
use lib "~/projects/perl6/MyProject/lib";
use My;见库。
https://stackoverflow.com/questions/47204912
复制相似问题