在这里,Hello $world每个引用符号的解释我的意思是语言.
$world = "W?rl?"
"(Hell)*o $world\n" # <--- plain (Hell)*o, resolve $world, escape \n
'(Hell)*o $world\n' # <--- plain (Hell)*o, plain $world, escape \n
/(Hell)*o $world\n/ # <--- regexp (Hell)*, resolve $world, interpret \n
<(Hell)*o $world\n> # <--- make list ["(Hello*o", "$world\n"]
{(Hell)*o $world\n} # <--- syntax error, this language cant' parse it
Run Code Online (Sandbox Code Playgroud)
Perl 6也足够强大,可以在未来的语言中存在类似的东西
my $emacs_func = (defun perl-backward-to-start-of-continued-exp (lim)
(if (= (preceding-char) ?\))
(forward-sexp -1))
(beginning-of-line)
(if (<= (point) lim)
(goto-char (1+ lim))) …Run Code Online (Sandbox Code Playgroud) say <a b c>[1];
Run Code Online (Sandbox Code Playgroud)
我认为相同的优先级适用于所有引用运算符.这有效:
my $string = '5+8i';
my $number = <<$string>>;
say $number;
Run Code Online (Sandbox Code Playgroud)
这会插入$string并创建allomorphes(在本例中为ComplexStr):
(5+8i)
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试将其编入索引,就像文档中的示例一样,它不会编译:
my $string = '5+8i';
my $number = <<$string>>[0];
say $number;
Run Code Online (Sandbox Code Playgroud)
我不太确定Perl 6在这里发生了什么.也许它认为这是一个超级高手:
===SORRY!=== Error while compiling ...
Cannot use variable $number in declaration to initialize itself
at /Users/brian/Desktop/scratch.pl:6
------> say $?number;
expecting any of:
statement end
statement modifier
statement modifier loop
term
Run Code Online (Sandbox Code Playgroud)
我可以跳过变量:
my $string = '5+8i';
say <<$string>>[0];
Run Code Online (Sandbox Code Playgroud)
但这是一个不同的错误,无法找到收尾报价: …
在Hash文档中,部分Object keys似乎暗示您可以使用任何类型作为哈希键,只要您指出但我在尝试使用数组作为键时遇到问题:
> my %h{Array};
{}
> %h{[1,2]} = [3,4];
Type check failed in binding to parameter 'key'; expected Array but got Int (1)
in block <unit> at <unknown file> line 1
Run Code Online (Sandbox Code Playgroud)
是否有可能做到这一点?
请考虑以下代码。为什么输出的是“ BABABA”而不是“ AAABAA” /“ AABAAAB”?这两个供应源不应该并行运行,并且当其中任何一个事件发生时立即火起来吗?
my $i = 0;
my $supply1 = supply { loop { await Promise.in(3); done if $i++> 5; emit("B"); } };
my $supply2 = supply { loop { await Promise.in(1); done if $i++> 5; emit("A"); } };
react
{
#whenever Supply.merge($supply1, $supply2) -> $x { $x.print }
whenever $supply1 -> $x { $x.print };
whenever $supply2 -> $x { $x.print };
}
Run Code Online (Sandbox Code Playgroud) 要在perl6中从数组中选择多个元素,这很容易:只需使用索引列表即可:
> my @a = < a b c d e f g >;
> @a[ 1,3,5 ]
(b d f)
Run Code Online (Sandbox Code Playgroud)
但是要取消选择这些元素,我必须使用Set:
> say @a[ (@a.keys.Set (-) (1,3,5)).keys.sort ]
(a c e g)
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更简单的方法,因为我使用的数组通常很大?
如何动态地向类添加属性?我尝试了此操作,但是它抱怨缺少方法,因此我不确定为什么,因为我没有尝试添加方法。
use v6.d;
class Foo does Metamodel::AttributeContainer {
submethod BUILD(:$attr) {
my $a = Attribute.new(:name($attr), :type(Str), :package(Foo));
self.add_attribute(self, $a);
}
}
my Foo $foo = Foo.new(:attr('bar'));
$foo.bar = 'baz'; # No such method 'bar' for invocant of type 'Foo'
say $foo.bar;
Run Code Online (Sandbox Code Playgroud) 我有两个元素的列表的列表,就像你用得到的,例如(1..5) Z (20..24),我要制作成一个哈希(在这个例子中,你通过得到什么{1 => 20, 2 => 21, 3 => 22, 4 => 23, 5 =>24}。我能做到“手”,但ISN不太优雅,我相信 Raku 有一种惯用的方法。我想出的不优雅的替代方案是:
my @a = (1..5) Z (20..24);
my %a;
for @a -> @x {
%a{@x[0]} = @x[1];
Run Code Online (Sandbox Code Playgroud) 对于 Duration,为什么我需要手动将 Rat 强制为 Real 而不是 Int?
这是基于实施 Perl 6.d 的 MoarVM 2020.01.1 版构建的 Rakudo 2020.01 版。在 OSX 上。
say $v.WHAT; #(Int)
$v = Duration.new( $v );
say $v; #20
my $w = 20.0;
say $w.WHAT; #(Rat)
$w = Duration.new( $w.Real );
say $w; #20
my $x = 20.0;
say $x.WHAT; #(Rat)
$x = Duration.new( $x );
say $x; #hangs
Run Code Online (Sandbox Code Playgroud) 在启示录中,有一些关于布尔断言的词:
<( code )> # call code as boolean assertion
Run Code Online (Sandbox Code Playgroud)
但是,我无法让它发挥作用。
say "9471" ~~ m:g/ (\d) <($0 > 5)> /
Run Code Online (Sandbox Code Playgroud)
我希望只匹配大于 5 的数字,但出现编译错误。
哪个是正确的语法(如果存在),或者做一些布尔断言的任何替代方法?
是否有可能在 END 块内知道程序是否死亡?在 Perl 中我会这样做
END {
if ( $? == 255 ) {
# ...
}
}
Run Code Online (Sandbox Code Playgroud)