我刚刚建立了Rakudo和Parrot,以便我可以玩它并开始学习Perl 6.我下载了Perl 6书并愉快地输入了第一个演示程序(网球锦标赛示例).
当我尝试运行该程序时,出现错误:
Divide by zero
current instr.: '' pc -1 ((unknown file):-1)
Run Code Online (Sandbox Code Playgroud)
我perl6在构建目录中有我的二进制文件.我在rakudo构建目录下添加了一个脚本目录:
rakudo
|- perl6
\- scripts
|- perlbook_02.01
\- scores
Run Code Online (Sandbox Code Playgroud)
如果我尝试从我的脚本目录运行一个简单的hello world脚本,我会得到同样的错误:
#!/home/daotoad/rakudo/perl6
use v6;
say "Hello nurse!";
Run Code Online (Sandbox Code Playgroud)
但是,如果我从rakudo它运行的目录运行它.
听起来我需要设置一些环境变量,但我不知道给它们的是什么和什么值.
有什么想法吗?
更新:
我宁愿不安装rakudo,我宁愿只是从build目录运行.这将允许我在我尝试不同的Perl6版本时(Rakudo*即将推出)将我对系统的更改保持在最低限度.
README文件鼓励我认为这是可能的:
Run Code Online (Sandbox Code Playgroud)$ cd rakudo $ perl Configure.pl --gen-parrot $ make这将在当前(rakudo)目录中创建"perl6"或"perl6.exe"可执行文件.然后可以使用如下命令从构建目录运行程序:
Run Code Online (Sandbox Code Playgroud)$ ./perl6 hello.pl
在重新阅读时,我发现了一个事实,即在构建目录之外运行脚本之前必须安装rakudo:
一旦构建,Rakudo的
make install目标将把Rakudo及其库安装到用于创建它的Parrot安装中.在执行此步骤之前,make上面创建的"perl6"可执行文件只能从Rakudo的构建目录的根目录中可靠地运行.make install执行完后,可以从任何目录运行已安装的可执行文件(只要用于创建它的Parrot安装保持不变).
所以看起来我需要安装rakudo来玩Perl 6.
接下来的问题是,安装rakudo的地方?README说用于构建的Parrot安装.
我--gen-parrot在我的构建中使用了该选项,它看起来像是安装的rakudo/parrot-install.所以rakudo会安装到我的rakudo\parrot-install?
阅读Makefile,支持这个结论.我跑了make install,它确实安装了parrot_install …
#!perl6
use v6;
my $m = 70;
my $n = 30;
( $m div $n ).say;
Run Code Online (Sandbox Code Playgroud)
第一个例子有效,但第二个没有.我想这是因为在第二个例子中,变量值是字符串.如果我的猜测是正确的,我怎么能将字符串变量更改为整数变量?
#!perl6
use v6;
my $m = '70';
my $n = '30';
( $m div $n ).say;
# No applicable candidates found to dispatch to for 'infix:<div>'.
# Available candidates are:
# :(Int $a, Int $b)
# in main program body at line 7:./perl5.pl
Run Code Online (Sandbox Code Playgroud) 鉴于此程序:
#!/bin/env perl6
sub MAIN ($filename='test.fq', :$seed=floor(now) )
{
say "Seed is $seed";
}
Run Code Online (Sandbox Code Playgroud)
当我在没有任何命令行参数的情况下运行它时,它可以正常工作。但是,当我给它的命令行参数时seed,它的值是True:
./seed.p6 --seed 1234
Seed is True
Run Code Online (Sandbox Code Playgroud)
为什么数字1234被解释为布尔值?
my @r = split("", "hi");
say @r.elems;
--> output: 4
Run Code Online (Sandbox Code Playgroud)
split 在数组中添加了两个额外的元素,一个在开头,另一个在结尾.
我必须在每次拆分后进行移位和弹出以纠正此问题.
是否有更好的方法来分割字符串?
当我regex使用捕获组创建变量时,整个匹配是正常的,但捕获组是Nil.
my $str = 'nn12abc34efg';
my $atom = / \d ** 2 /;
my $rgx = / ($atom) \w+ ($atom) /;
$str ~~ / $rgx / ;
say ~$/; # 12abc34
say $0; # Nil
say $1; # Nil
Run Code Online (Sandbox Code Playgroud)
如果我修改程序以避免$rgx,一切都按预期工作:
my $str = 'nn12abc34efg';
my $atom = / \d ** 2 /;
my $rgx = / ($atom) \w+ ($atom) /;
$str ~~ / ($atom) \w+ ($atom) /;
say ~$/; # 12abc34
say $0; # …Run Code Online (Sandbox Code Playgroud) 如何明确定义regex(编号或命名的捕获组),它总是匹配零个字符?
> "abc" ~~ m/()abc/
===SORRY!=== Error while compiling:
Null regex not allowed
> my regex null {}
===SORRY!=== Error while compiling:
Null regex not allowed
> my regex null {<[]>}
regex null {<[]>}
> "abc" ~~ m/<null>abc/
False
Run Code Online (Sandbox Code Playgroud)
当然我可以使用类似下面的内容,但我正在寻找一种惯用的方法.
> my regex null {a ** 0}
regex null {a ** 0}
> "abc" ~~ m/<null>abc/
?abc?
null => ??
Run Code Online (Sandbox Code Playgroud)
UPD:这有效,但这是最好的方法吗?
> my regex null { '' }
regex null { '' }
> "abc" ~~ m/<null>abc/
?abc? …Run Code Online (Sandbox Code Playgroud) 使用Perl6解析二进制结构的最佳选择是什么.
在Perl5中,我们在Perl6上有打包/解包方法它们似乎是实验性的
是否可以使用Perl6语法来解析二进制数据,假设我有一个文件,其中包含以下二进制格式的记录:
struct record {
short int ut_type;
char ut_line[UT_LINESIZE];
char ut_id[4];
char ut_user[UT_NAMESIZE];
char ut_host[UT_HOSTSIZE];
}
Run Code Online (Sandbox Code Playgroud)
是否可以使用Perl6语法解析此文件?
一些背景:我想扩展JSON::Tiny以允许更轻松的列表解析.这类似于Perl 5中的relaxed标志JSON::XS.具体来说,我希望可以选择在列表末尾添加逗号.例如,{ "a" : 1, "b" : 2, }.注意之后的逗号2,正常的JSON语法规范(或JSON::Tiny)不允许这样做.
通过检查源代码,它似乎可以像扩展其中一个模块JSON :: Tiny :: Grammar一样简单,它JSON::Tiny在内部使用,然后覆盖其两个规则:
grammar JSON::Relaxed::Grammar is JSON::Tiny::Grammar {
rule pairlist { <pair> * %% \, } # override this rule
rule arraylist { <value> * %% [ \, ] } #overide this rule
}
Run Code Online (Sandbox Code Playgroud)
请注意,唯一的修改JSON::Tiny::Grammar是引入%%运算符而不是%操作符pairlist和arraylist规则.
这样的扩展将允许代码重用.(复制所有代码的替代方案JSON::Tiny是最后的选择.)
问题:现在的问题是如何在 …
复制FindBin::libsPerl 6中的行为.
(1) Start from `$Bin`.
(2) Search for `./lib` dir's above it.
(3) prefix them to the search list.
Run Code Online (Sandbox Code Playgroud)
在P6中,这需要管理$*REPO,我认为需要使用CompUnit::RepositoryRegistry,但我在modules.perl6.org(可能因为它是核心)或docs.perl6.org上找不到任何文档.
问:CompUnit::RepositoryRegistry使用一些新目录作为前缀管理列表是正确的$*REPO吗?
问:如果是,记录在哪里CU::RR?
问:如果没有,我应该使用什么?
谢谢
我已经定义了自己的元模型类来创建一种特殊的类.现在,我希望这些类能够自动注册一个特殊的经理.基本上,这就像这样(只有compose在每次加载类'模块时才会调用):
use MyManager;
class MyHOW is Metamodel::ClassHOW {
method compose ( Mu \type ) {
self.add_parent( type, MyParentClass );
callsame;
registerMyClass( type );
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有类似的东西:
use v6;
use MyClass;
myclass Foo { ... }
Run Code Online (Sandbox Code Playgroud)
在一个模块中.然后有一个管理器对象,它扫描require名称与特定模式匹配的存储库/文件系统和模块.之后,它需要知道myclass每个模块中定义了什么.它可以扫描加载模块的符号表.但是如果加载的文件包含多个模块或根本没有模块,这将无法工作 - 如上例所示.
到目前为止,看起来INIT移相器会提供解决方案,但我很难找到如何从composer方法中获取类的主体块.
perl6 ×10
raku ×4
command-line ×1
grammar ×1
inheritance ×1
rakudo ×1
rakudo-star ×1
repository ×1
types ×1
variables ×1