在Racket Scheme中,有一个称为"字符串端口"的数据结构,您可以从中读取数据.perl6中有类似的东西吗?例如,我想实现这些结果:
my $a = "(1,2,3,4,5)"; # if you read from $a, you get a list that you can use;
my $aList=readStringPort($a);
say $aList.WHAT; # (List)
say $aList.elems; # 5 elements
my $b = "[1,2,3]"; # you get an array to use if you read from it;
my $c = "sub ($one) {say $one;}";
$c("Big Bang"); # says Big Bang
Run Code Online (Sandbox Code Playgroud)
EVAL功能并不能完成所有任务:
> EVAL "1,2,3"
(1 2 3)
> my $a = EVAL "1,2,3"
(1 2 3)
> $a.WHAT
(List)
> my $b = EVAL "sub ($one) {say $one;}";
===SORRY!=== Error while compiling:
Variable '$one' is not declared. Did you mean '&one'?
------> my $b = EVAL "sub (?$one) {say $one;}";
Run Code Online (Sandbox Code Playgroud)
非常感谢!
lisprog
EVAL 做这个.
最后一个例子中的问题是双引号字符串插入$变量和{块等.为了在字符串文字中表示这样的东西,要么用反斜杠转义它们......
my $b = EVAL "sub (\$one) \{say \$one;}";
Run Code Online (Sandbox Code Playgroud)
...或使用非插值字符串文字:
my $b = EVAL 'sub ($one) {say $one;}';
my $b = EVAL Q[sub ($one) {say $one;}];
Run Code Online (Sandbox Code Playgroud)