我有一个需要在Perl 5和Perl 6环境中运行的Perl脚本.如果使用Perl6我需要在Perl5上使用"perl6 :: Form"我需要使用"Format".
此代码适用于两个版本或perl而不会出现错误:
BEGIN {
if( $] ge 6){
require Perl6::Form;
Perl6::Form::->import();
}
}
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何在Perl5上运行时"分离"Perl6代码.
if( $] ge 6){ # Perl6
print form
...
...
} else { # perl5
format STDOUT =
...
...
}
Run Code Online (Sandbox Code Playgroud)
当我在Perl5上出错时,这不能干净地工作:
Unquoted string "form" may clash with future reserved word at /usr/bin/script.pl line 628.
Name "main::form" used only once: possible typo at /usr/bin/script.pl line 641.
Run Code Online (Sandbox Code Playgroud)
我简要地看了一下Text::CPP,但我不想依赖于正在安装的编译器.任何建议,将不胜感激.
看起来好像标量本身就是一个项目的列表:
> "foo"[1]
Index out of range. Is: 1, should be in 0..0
in block <unit> at <unknown file> line 5
> "foo"[0]
foo
> "foo"[*-1]
foo
Run Code Online (Sandbox Code Playgroud)
我说的是一个列表,因为列表似乎没有索引的范围:
> (0, 1, 2)[3]
Nil
Run Code Online (Sandbox Code Playgroud)
这里发生了什么.我对[]运营商的了解不多.
Perl 6 POSIX字符类是否尊重LOCALE?我正在玩一个程序,该程序将打印与POSIX char类匹配的所有字符,并且无论我设置了什么区域设置,它似乎总是打印相同的集合。如果我的语言环境是en_US.US-ASCII,我仍然会得到520ish的数字。同样令人烦恼的是,在Mac上执行此操作意味着我没有在其他地方找到酷的语言环境探索工具(或者,它们以不同的名称存在)。
这就是我需要在shell别名中输入的所有命令:
$ perl6 -e 'say join " ", map *.gist, $*VM, $*PERL, $*DISTRO, $*KERNEL'
moar (2016.10) Perl 6 (6.c) macosx (10.10.5) darwin (14.5.0)
Run Code Online (Sandbox Code Playgroud)
并且,该程序:
my $properties = set( <
alnum alpha ascii blank cntrl digit graph lower print graph punct
space upper word xdigit
> );
sub MAIN ( Str $property where * ? $properties = 'digit' ) {
say "NAME is " ~ %*ENV<NAME>;
say "LC_CTYPE is " ~ ( %*ENV<LC_CTYPE> // %*ENV<LC_ALL> …Run Code Online (Sandbox Code Playgroud) 什么是最好的表现方式,在正则表达式中,否定多个单词和构成这些单词的字符的排列?
例如:我不想要
"zero dollar"
"roze dollar"
"eroz dollar"
"one dollar"
"noe dollar"
"oen dollar"
Run Code Online (Sandbox Code Playgroud)
但我确实想要
"thousand dollar"
"million dollar"
"trillion dollar"
Run Code Online (Sandbox Code Playgroud)
如果我写
not m/ [one | zero] \s dollar /
Run Code Online (Sandbox Code Playgroud)
它不会匹配字符的排列,而外部的"非"功能会使正则表达式与正则表达式中没有"美元"的"大爆炸"相匹配.
m/ <- [one] | [zero] > \s dollar/ # this is syntax error.
Run Code Online (Sandbox Code Playgroud)
非常感谢你 !
lisprog
Perl6标准语法相对较大.虽然这有助于表达一旦掌握,但它会对掌握产生障碍.例如,核心构造通常具有支持不同编程范例的多种形式.一个基本的例子是用于创建Pairs的各种语法:
Pair.new('key', 'value'); # The canonical way
'key' => 'value'; # this...
:key<value>; # ...means the same as this
:key<value1 value2>; # But this is key => <value1 value2>
:foo(127); # short for foo => 127
:127foo; # the same foo => 127
Run Code Online (Sandbox Code Playgroud)
请特别注意第一种形式的评论:"规范方式".
另一个例子是以下文档method make:
This is just a little sugar for $/.made = $ast which is a very common operation in actions.
Run Code Online (Sandbox Code Playgroud)
是否存在可以为Perl6程序输出的规范形式,以便在掌握了规范子语法后,可以检查该形式的任何Perl6程序以理解它?
当我使用命名的正则表达式时,我可以打印其内容:
my regex rgx { \w\w };
my $string = 'abcd';
$string ~~ / <rgx> /;
say $<rgx>; # ?ab?
Run Code Online (Sandbox Code Playgroud)
但是如果我想匹配:g或:ex副词,所以有不止一个匹配,它不起作用.下列
my regex rgx { \w\w };
my $string = 'abcd';
$string ~~ m:g/ <rgx> /;
say $<rgx>; # incorrect
Run Code Online (Sandbox Code Playgroud)
给出错误:
Type List does not support associative indexing.
in block <unit> at test1.p6 line 5
Run Code Online (Sandbox Code Playgroud)
我应该如何修改我的代码?
UPD:基于@ piojo的解释,我修改了最后一行,如下所示,这解决了我的问题:
say $/[$_]<rgx> for ^$/.elems;
Run Code Online (Sandbox Code Playgroud)
以下会更容易,但由于某种原因它不起作用:
say $_<verb> for $/; # incorrect
Run Code Online (Sandbox Code Playgroud) 这个问题的延续,可能更奇怪.
我可以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) 我想用我的程序将文字分配给文件末尾的一些变量,但是要先使用这些变量.我想出来的唯一方法是:
my $text;
say $text;
BEGIN {
$text = "abc";
}
Run Code Online (Sandbox Code Playgroud)
是否有更好/更惯用的方式?
我正在考虑使用perl6和Cro来构建一个包含文本内容的网站.是否有关于使用模板工具包(如TT2)和代码示例使用Cro的最佳实践/指导?
假设我有这个脚本:
# prog.p6
my $info = run "uname";
Run Code Online (Sandbox Code Playgroud)
当我跑步时prog.p6,我得到:
$ perl6 prog.p6
Linux
Run Code Online (Sandbox Code Playgroud)
有没有办法存储返回值的字符串版本并阻止它输出到终端?
已经存在类似的问题,但它没有提供具体的答案.