在Perl 5中,我可以说
my $meth = 'halt_and_catch_fire';
my $result = $obj->$meth();
Run Code Online (Sandbox Code Playgroud)
这对于迭代方法名称列表来执行操作非常方便.我已经设法搞清楚在Perl 6中我不能只说
my $result = $obj.$meth();
Run Code Online (Sandbox Code Playgroud)
有一件事做的工作
my $result = $obj.^can($meth)[0]($obj);
Run Code Online (Sandbox Code Playgroud)
但这似乎完全可怕.我该怎么办呢?
在perl6中,我想将一个数组分配给另一个数组并使得结果数组成为不同的实体,但似乎既没有直接赋值也没有克隆可以做我想要的.有没有办法用一个表达式复制数组而不是编写循环例程?
To exit type 'exit' or '^D'
> my @a=<a b c d e>
[a b c d e]
> my @b = <1 2 3 4 5 6 7>
[1 2 3 4 5 6 7]
> my @c = @a
[a b c d e]
> @c[3]
d
> @c[3]=3;
3
> @c
[a b c 3 e]
> @a
[a b c d e]
> @c === @a
False
> @c == @a
True # this is …Run Code Online (Sandbox Code Playgroud) 另一个关于hash作为论据的问题trans.在下面的代码中,简单地hash给出了一个不正确的结果,但是替换它keys并values使其正确.怎么了?
my @alph1 = <a+ b+ c+ d+ e+ f+>;
my @alph2 = <A_ B_ C_ D_ E_ F_>;
my %h;
%h{ @alph1 } = @alph2;
my $str = 'a+bc de+f';
my $text = $str.trans(%h);
say $text; # A_BC DE_F (incorrect)
$text = $str.trans(%h.keys => %h.values);
say $text; # A_bc dE_f (correct)
Run Code Online (Sandbox Code Playgroud) 这就是我想要做的.它应该很简单,但我无法弄清楚如何正确地做到这一点.
> my @search_keys = <bb cc dd>
[bb cc dd]
> my $search_junc = @search_keys.join('|')
bb|cc|dd
> "bb" eq $search_junc
False
Run Code Online (Sandbox Code Playgroud) 只是想知道是否有人知道为什么Perl6的日志函数返回Num类型而不是Rat类型.
say (e*e).log.WHAT;
> (Num)
say (2/3).WHAT;
> (Rat)
Run Code Online (Sandbox Code Playgroud) This file will be foo.pm6:
sub bar { "quux" }
say "Loaded";
Run Code Online (Sandbox Code Playgroud)
And this one requirer.p6:
require "foo.pm6";
say bar;
Run Code Online (Sandbox Code Playgroud)
require fails silently, not loading foo.pm6, and bar is not found. This also fails:
require foo;
say bar;
Run Code Online (Sandbox Code Playgroud)
with the same error, about not finding bar. This file:
require ::"foo";
say bar;
Run Code Online (Sandbox Code Playgroud)
Fails even strangely, with MVMArray: Can't shift from an empty array
UPDATE: it fails silently because it stops when it finds an unknown symbol, bar, …
python是否有任何方法可以轻松快速地创建CLI实用程序,而无需大量的参数解析样板?
在Perl 6中,MAIN子的签名自动解析命令行参数.
有没有办法在没有大量样板的情况下在Python中做类似的事情?如果没有,最好的方法是什么?我正在考虑一个函数装饰器,它将执行一些内省并做正确的事情.如果没有什么比这更好的了,我会想到的东西就像我下面的东西.这是一个好主意吗?
@MagicMain
def main(one, two=None, *args, **kwargs):
print one # Either --one or first non-dash argument
print two # Optional --arg with default value (None)
print args # Any other non-dash arguments
print kwargs # Any other --arguments
if __name__ == '__main__':
main(sys.argv)
Run Code Online (Sandbox Code Playgroud) python method-signature perl6 command-line-arguments magic-methods
据我所知,动态变量在运行时被查找.我想使用它们来启用类似于球拍参数的参数化.
为此,我必须设置一个应该可以覆盖的默认值,但不一定是可更改的.我目前的方法相当简单:
my $*param ::= 42;
sub parameterized-function { say $*param };
parameterized-function();
do {
my $*param ::= 15;
parameterized-function();
}
Run Code Online (Sandbox Code Playgroud)
哪个工作正常 - 除了它在外部范围引入参数的名称.除了感觉不整洁之外,my $*param = 15;如果在文件级别上使用,这会产生导致混乱的副作用.
我想要做的是检查参数是否已在调用堆栈上定义,顺序如下:
sub parameterized-function { if defined($*param) { say 42 } else { say $*param } };
Run Code Online (Sandbox Code Playgroud)
那么,是否有可能进行这样的检查,如果是这样,它是如何完成的?
为什么在这里substr-rw切断尾随6?
#!/usr/bin/env perl6
use v6;
my $str = '123';
$str ~= '.' x 30;
$str ~= '456';
say $str; # 123..............................456
$str.substr-rw( 0, 10 ) = '';
say $str; # ........................45
Run Code Online (Sandbox Code Playgroud)
perl6 --version
This is Rakudo version 2016.03-38-g8df1a69 built on MoarVM version 2016.03-46-g50c7f6a implementing Perl 6.c.
Run Code Online (Sandbox Code Playgroud) 我有一个格式的文件:
- foo bar - baz
one two three - or four
and another line
- next job
do this - and that
Run Code Online (Sandbox Code Playgroud)
我的语法是
grammar tasks {
regex TOP { \n* <oneTask>+ \n* }
regex oneTask { ^^ \- (<oneSection> <endSection>)+ }
regex oneSection { \N+ } # this is not quite working
regex endSection { \n+ }
Run Code Online (Sandbox Code Playgroud)
}
在正则表达式oneSection中,我如何编写"我想匹配' - '只有当它不在一行开头时"的事实?
我把文件放入一个字符串并解析这个字符串:
my $content = slurp("taskFile");
my $result = tasks.parse($content);
Run Code Online (Sandbox Code Playgroud)
这不太合适.
<[\N] - [\-]> does not make the match …Run Code Online (Sandbox Code Playgroud)