考虑这个我创建哈希的程序.我想在其中更改两个值:
my $hash = %(
wallet => 100,
gave => 0,
received => 0,
);
for ^1 { $hash<wallet gave> Z+= <-1 1> };
dd $hash;
Run Code Online (Sandbox Code Playgroud)
像这样,最后一行for没有做任何事情,也没有警告.哈希值保持不变:
Hash $hash = ${:gave(0), :received(0), :wallet(100)}
Run Code Online (Sandbox Code Playgroud)
添加另一个语句会更改行为:
my $hash = %(
wallet => 100,
gave => 0,
received => 0,
);
for ^1 { $hash<wallet gave> Z+= <-1 1>; True };
dd $hash;
Run Code Online (Sandbox Code Playgroud)
现在inplace编辑做了它的事情,但是有一个警告(虽然当我找到它的用途时我争论"无用"):
Useless use of constant value True in sink context
Hash $hash = ${:gave(1), :received(0), :wallet(99)}
Run Code Online (Sandbox Code Playgroud)
如果我没有Z+= …
将一个添加到Ints的交汇点:
put any( 1, 3, 7 ) + 1;
Run Code Online (Sandbox Code Playgroud)
现在你有一个Ints增加了一个的交汇点:
any(2, 4, 8)
Run Code Online (Sandbox Code Playgroud)
所以,2 == any(2, 4, 8)是的.
建立字符串连接并附加到这些字符串:
put any( <h H> ) ~ 'amadryas';
Run Code Online (Sandbox Code Playgroud)
你会得到一个不同于'hamadryas'或'Hamadryas'的不同结果:
any("h", "H")amadryas
Run Code Online (Sandbox Code Playgroud)
我期待的是:
any( 'hamadryas', 'Hamadryas' );
Run Code Online (Sandbox Code Playgroud)
这些操作有什么不同,即使它们应该是相似的,它们也能给出不同的行为?
如果为数组分配未定义的值,它将包含该未定义的值,如果您不想迭代未定义的值,则会使用循环复杂化:
my @bar = 1, 2, Any;
for @bar -> $baz {
put $baz;
}
Run Code Online (Sandbox Code Playgroud)
这给出了以下输出,包括对未定义值的警告:
Run Code Online (Sandbox Code Playgroud)1 2 Use of uninitialized value $baz of type Any in string context. Methods .^name, .perl, .gist, or .say can be used to stringify it to something meaningful. in block at for.p6 line 4
我知道我可以通过多种方式明确地处理这个问题,例如:
for @bar -> $baz {
next unless $baz; # Skip this iteration
put $baz;
}
for @bar.grep: *.defined { # Just iterate over the defined values
put $baz;
} …Run Code Online (Sandbox Code Playgroud) 在阅读某些文件后,我注意到他们使用的课程,功能,符号,方法,以及我作为电子工程师所知道的事情.然后,他们有我从未听说过的概念,例如角色和副词.如果我不理解命名法,我不能很好地理解文档,可能会得到非常意外的结果,也不能很好地利用语言.
我无法在任何地方找到他们的定义,包括StackOverflow中的标签......任何指针都会受到赞赏.
我有一个简单的测试文件,如下所示:
use v6.c;
use NativeCall;
sub fcntl(int32, int32 --> int32) is native { * }
sub close(int32 --> int32) is native { * }
my $fd := fcntl($*OUT.native-descriptor, 0);
say $fd;
close($fd);
Run Code Online (Sandbox Code Playgroud)
返回的文件描述符是-1,这不是我想要的.但是当我在REPL中运行相同的代码时,我得到了我正在寻找的东西:
> use NativeCall
Nil
> sub fcntl(int32, int32 --> int32) is native { * }
sub fcntl (int32 $, int32 $ --> int32) { #`(Sub+{Callable[int32]}+{NativeCall::Native[Sub+{Callable[int32]},Str]}|17126514527616) ... }
> sub close(int32 --> int32) is native { * }
sub close (int32 $ --> int32) { #`(Sub+{Callable[int32]}+{NativeCall::Native[Sub+{Callable[int32]},Str]}|17126514527904) ... } …Run Code Online (Sandbox Code Playgroud) Perl 6的具有实用的功能,允许人们得到任何波德声明符块附加到一个子程序(或类,角色等),使用该WHY方法:
#|(Some enlightening words about myfunc.)
sub myfunc (Int $i) { say "You provided an integer: $i"; };
#=(Some more words about myfunc.)
say &myfunc.WHY;
Run Code Online (Sandbox Code Playgroud)
这显示:
Some enlightening words about myfunc.
Some more words about myfunc.
Run Code Online (Sandbox Code Playgroud)
不幸的是,当一个子程序有多个候选者时,不能只调用.WHY子程序名:
#|(myfunc accepts an integer.)
multi myfunc (Int $i) { say "You provided an integer $i"; };
#|(myfunc accepts a string.)
multi myfunc (Str $s) { say "You provided a string $s"; };
say &myfunc.WHY;
Run Code Online (Sandbox Code Playgroud)
结果: …
从不是例程的块返回CATCH移相器中的值的语法是什么?
sub foo() {
<1 2 3>.map: -> $a {
die 'oops';
CATCH { default { 'foo' } }
}
}
sub bar() {
<1 2 3>.map: -> $a {
die 'oops';
CATCH { default { return 'bar' } }
}
}
say foo(); # (Nil, Nil, Nil)
say bar(); # Attempt to return outside of immediatelly-enclosing Routine (i.e. `return` execution is outside the dynamic scope of the Routine where `return` was used)
Run Code Online (Sandbox Code Playgroud)
编辑:所需的输出是:
say baz(); # (baz baz baz) …Run Code Online (Sandbox Code Playgroud) Perl 6的shell将命令发送到"shell",但没有说明是什么.我一直在我的机器上打击,但我不知道我是否可以依赖它.
$ perl6 -e 'shell( Q/echo $SHELL/ )'
/bin/bash
$ csh
% perl6 -e 'shell( Q/echo $SHELL/ )'
/bin/bash
% zsh
$ perl6 -e 'shell( Q/echo $SHELL/ )'
/bin/bash
Run Code Online (Sandbox Code Playgroud)
在文档记录的时候,在Unix上这很容易,但是Windows上的cmd.exe或PowerShell(或者如果安装了bash)呢?我认为这是cmd.exe,但记录的答案会很好.
我在2018.01玩这个:
my $proc = Proc.new: :out;
my $f = $proc.clone;
$f.spawn: 'ls';
put $f.out.slurp;
Run Code Online (Sandbox Code Playgroud)
它说不能这样做.奇怪的是,错误消息是关于我没有使用的例程和不同的类:
Cannot resolve caller stdout(Proc::Async: :bin); none of these signatures match:
(Proc::Async:D $: :$bin!, *%_)
(Proc::Async:D $: :$enc, :$translate-nl, *%_)
in block <unit> at proc-out.p6 line 3
Run Code Online (Sandbox Code Playgroud) 现在,我有一个MAIN可以接受一个或多个字符串参数的子.但我使用两个单独的参数MAIN来做到这一点:
sub MAIN (
Str:D $file,
*@files,
) {
@files.prepend: $file;
# Rest of the program
}
Run Code Online (Sandbox Code Playgroud)
现在我想知道是否有更惯用的方法来实现这一点,因为我目前的解决方案感觉有点笨重,而不是非常Perly.