在Perl 5中,我可以创建一个字符串的文件句柄,并从字符串中读取或写入,就好像它是一个文件一样.这非常适合使用测试或模板.
例如:
use v5.10; use strict; use warnings;
my $text = "A\nB\nC\n";
open(my $fh, '<', \$text);
while(my $line = readline($fh)){
print $line;
}
Run Code Online (Sandbox Code Playgroud)
我怎么能在Perl 6中做到这一点?以下不为Perl 6的工作(至少对于上运行我的Perl6的实例MoarVM 2015.01从Rakudo星2015年1月发布 64位的CentOS 6.5):
# Warning: This code does not work
use v6;
my $text = "A\nB\nC\n";
my $fh = $text;
while (my $line = $fh.get ) {
$line.say;
}
# Warning: Example of nonfunctional code
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
No such method 'get' for invocant of type 'Str'
in block <unit> …Run Code Online (Sandbox Code Playgroud) {
sub a {
print 1;
}
}
a;
Run Code Online (Sandbox Code Playgroud)
一个错误,是吗?
a 不应该从外面获得.
它在Perl 6*中有效吗?
*对不起,我还没有安装它.
Perl 6具有出色的内置命令行解析功能MAIN.但是,我遇到了一个似乎微不足道的问题,但我无法弄清楚.
一个简单的MAIN:
sub MAIN(Int :n(:$num)) {
say "You passed: " ~ $num;
}
Run Code Online (Sandbox Code Playgroud)
然后我可以将我的脚本称为:
$ ./test.p6 -n=1
Run Code Online (Sandbox Code Playgroud)
要么:
$ ./test.p6 --num=1
Run Code Online (Sandbox Code Playgroud)
但不能用:
$ ./test.p6 -n 1 # or even -n1
Run Code Online (Sandbox Code Playgroud)
要么:
$ ./test.p6 --num 1
Run Code Online (Sandbox Code Playgroud)
假设我想要a,b和c中的2个字母的所有排列.
我可以:
my @perm = <a b c>.combinations(2)».permutations;
say @perm;
# [((a b) (b a)) ((a c) (c a)) ((b c) (c b))]
Run Code Online (Sandbox Code Playgroud)
这很接近,但不完全是我需要的.我如何"扁平化"这样以便得到:
# [(a b) (b a) (a c) (c a) (b c) (c b)]
Run Code Online (Sandbox Code Playgroud)
?
这有点意外的行为,可能会咬人初学者.首先,这是打算吗?其次,Perl 6使用什么来猜测要创建哪个对象?它是否开始认为它是Block或Hash并稍后更改,还是最终决定?
你可以用大括号和胖箭头构造一个哈希:
my $color-name-to-rgb = {
'red' => 'FF0000',
};
put $color-name-to-rgb.^name; # Hash
Run Code Online (Sandbox Code Playgroud)
使用另一个Pair符号也会创建哈希.
my $color-name-to-rgb = {
:red('FF0000'),
};
Run Code Online (Sandbox Code Playgroud)
但是,如果没有胖箭,我会得到一个Block:
my $color-name-to-rgb = {
'red', 'FF0000',
};
put $color-name-to-rgb.^name; # Block
Run Code Online (Sandbox Code Playgroud)
还有其他方法来定义哈希,但我问的是这个特定的语法,而不是寻找我已经知道的解决方法.
$ perl6 -v
This is Rakudo version 2017.04.3 built on MoarVM version 2017.04-53-g66c6dda
implementing Perl 6.c.
Run Code Online (Sandbox Code Playgroud) 在Bar.pm中,我声明了一个具有权限(作者)和版本的类:
class Bar:auth<Camelia>:ver<4.8.12> {
}
Run Code Online (Sandbox Code Playgroud)
如果我在程序中使用它,我如何查看我正在使用的模块版本,编写者以及模块加载器如何找到它?与往常一样,文档链接很重要.
这个问题也被问到perl6用户,但在出现满意答案(或文档链接)之前就已经死了.
这个问题的另一个问题是许多人没有将这些信息添加到他们的类或模块定义中.它显示在META.json文件中,但不显示在代码中.
我使用Perl 6的cro创建了一个存根服务,但由于"不支持ALPN"而导致错误.
$ cro stub http ds4 ds4
Stubbing a HTTP Service 'ds4' in 'ds4'...
First, please provide a little more information.
Secure (HTTPS) (yes/no) [no]: yes
Support HTTP/1.1 (yes/no) [yes]:
Support HTTP/2.0 (yes/no) [yes]:
Support Web Sockets (yes/no) [no]: yes
$ cd ds4
$ cro run
? Starting ds4 (ds4)
Endpoint HTTPS will be at https://localhost:20000/
? Restarting ds4 (ds4)
? ds4 HTTP/2 is requested, but ALPN is not supported
? ds4 in method new at /home/zoffix/rakudo/install/share/perl6/site/sources/D142088174DCE80630FC7C31793703D9D56E26D6 (Cro::HTTP::Server) line …Run Code Online (Sandbox Code Playgroud) 随着.does我可以检查一个类型都有,我已经知道了作用.我想获得角色列表.继承有.^mro但我没有看到类似于元模型中的角色.
除此之外,给定一个"类型",如何判断它是被定义为类还是角色?
我定义了一个multi sub有两个签名:
multi sub mie(Str $s, Int $i) { $s x $i }
multi sub mie(Int $s, Int $i) { ... }
say &mie.signature; # ;; Mu | is raw)
Run Code Online (Sandbox Code Playgroud)
我想得到这个签名multi sub,但上面的结果不是我的预期.
正如文档所说,contains是一个多方法,有4个签名:
multi method contains(Str:D: Cool:D $needle)
multi method contains(Str:D: Str:D $needle)
multi method contains(Str:D: Cool:D $needle, Int(Cool:D) $pos)
multi method contains(Str:D: Str:D $needle, Int:D $pos)
Run Code Online (Sandbox Code Playgroud)
但是当我试图获得包含的签名时:
say "a string".^methods.pairs.values[8].value.signature;
Run Code Online (Sandbox Code Playgroud)
它只输出一个签名:
(Str: | is raw)
Run Code Online (Sandbox Code Playgroud)
在REPL中,当我调用contains没有参数的方法时,它输出以下错误:
> "a string".contains() …Run Code Online (Sandbox Code Playgroud)