如何明确定义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) 可能我做的事情完全错了,但有没有办法修改和组合regexes
使用子程序?下面的程序不会编译.
sub a(Regex $r1) {
return rx/ <$r1> a /
}
my regex letter_b { b };
my $new = a(letter_b);
say $new;
Run Code Online (Sandbox Code Playgroud) 我想知道,Perl 6文档是否以某种格式提供给电子阅读器(epub/mobi/fb2).我试图从这个网页制作一个epub ,其中所有的文档都方便地组合成一个文件.不幸的是,可用的转换器忽略了精彩的4级结构,所以我得到了一个没有书签的庞大的epub,这是不可能导航的.
那么,有没有人知道在哪里可以找到,或者可以通过书签使自己成为带有书签的Perl 6文档中的epub/mobi ?
快速的谷歌搜索引导我到python docs的epub.:)
这个问题的延续,可能更奇怪.
我可以regexes
使用例如连接两个sub
吗?(当然,我明白了,怎么做一个regex
)
以下代码完全错误,但我希望它可以解释我想要做的事情:
my Regex sub s12 ( $c, $v) {
return / <{$c}> <{$v}> /
}
my regex consonant { <[a .. z] -[aeiou]> }
my regex vowel { <[aeiou]> }
my regex open_syllable { &s12( &consonant, &vowel ) }
"bac" ~~ m:g/ <open_syllable> /;
say $/; # should be 'ba'
Run Code Online (Sandbox Code Playgroud) 我正在尝试Hash
使用非字符串键,在我的情况下使用数组或列表.
> my %sum := :{(1, 3, 5) => 9, (2, 4, 6) => 12}
{(1 3 5) => 9, (2 4 6) => 12}
Run Code Online (Sandbox Code Playgroud)
现在,我不明白以下几点.
如何检索现有元素?
> %sum{(1, 3, 5)}
((Any) (Any) (Any))
> %sum{1, 3, 5}
((Any) (Any) (Any))
Run Code Online (Sandbox Code Playgroud)
如何添加新元素?
> %sum{2, 4} = 6
(6 (Any))
Run Code Online (Sandbox Code Playgroud) 我想用我的程序将文字分配给文件末尾的一些变量,但是要先使用这些变量.我想出来的唯一方法是:
my $text;
say $text;
BEGIN {
$text = "abc";
}
Run Code Online (Sandbox Code Playgroud)
是否有更好/更惯用的方式?
抱歉有一个愚蠢的问题,但我是初学者,所以我无法解决这个问题.
我处理文件,其中数据块被=
不同长度的连续符号行划分,然后是\n
.我想将这些块作为单独的记录读取,$/ = "=+\n";
但是它没有检测到===
分隔符.
所以我的代码是:
$ cat prog1
#!/usr/bin/perl
use v5.22;
use strict;
use warnings;
$/ = "=+\n";
$\="+++\n";
while (<>){
chomp;
print;
}
Run Code Online (Sandbox Code Playgroud)
我的数据:
$ cat data1
line1
line2
===
line4
line5
Run Code Online (Sandbox Code Playgroud)
我的输出:
$ ./prog1 data1
line1
line2
===
line4
line5
+++
Run Code Online (Sandbox Code Playgroud) > so (Any)
False
Run Code Online (Sandbox Code Playgroud)
但
> so [1, Any]
True
Run Code Online (Sandbox Code Playgroud)
如何制作False
?
UPD:这似乎有效,但我不确定这是正确的方法.
> so [1, Any].all
False
Run Code Online (Sandbox Code Playgroud) 我正在尝试安装Readline.
(该系统是Linux Mint Mate 18.3,Lubuntu 17.10也是如此).
$ zef install Readline
Run Code Online (Sandbox Code Playgroud)
安装过程开始,但稍后将中止以下消息:
===> Searching for: Readline
===> Searching for missing dependencies: LibraryCheck
===> Testing: LibraryCheck:ver<0.0.6>:auth<github:jonathanstowe>
===> Testing [OK] for LibraryCheck:ver<0.0.6>:auth<github:jonathanstowe>
===> Testing: Readline:ver<0.0.2>:auth<github:drforr>
# Failed test 'initialize'
# at t/02-base.t line 10
# Cannot locate native library 'libreadline.so': libreadline.so: cannot open shared object file: No such file or directory
# Failed test 'macro-dumper lives'
# at t/02-base.t line 18
# Cannot locate native library 'libreadline.so': libreadline.so: cannot open shared object file: …
Run Code Online (Sandbox Code Playgroud) 这是我的测试程序:
use Readline;
shell 'clear';
my $r = Readline.new;
loop {
my $a = $r.readline("> ");
{say ''; last} if not defined $a;
$r.add-history( $a );
say $a;
}
Run Code Online (Sandbox Code Playgroud)
输入任何字符串后,它将退出并显示以下消息:
> abc
Internal error: unhandled encoding
in method CALL-ME at /opt/rakudo-pkg/share/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03 (NativeCall) line 587
in method readline at /home/evb/.perl6/sources/D8BAC826F02BBAA2CCDEFC8B60D90C2AF8713C3F (Readline) line 1391
in block <unit> at abc.p6 line 7
Run Code Online (Sandbox Code Playgroud)
如果我评论该行shell 'clear';
,一切都OK.