2017-06-25 22 views
3

在球拍方案中,有一個名爲「串口」的數據結構,您可以從中讀取數據。在perl6中有類似的東西嗎?對於例子,我要實現這些目標: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 

功能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;}"; 

非常感謝!

lisprog

+0

我明白了!我現在明白了。謝謝你! – lisprogtor

回答

5

EVAL做到這一點。

在你的上例中的問題,是雙引號字符串插值$變量和{塊及類似。爲了表示一個字符串這樣的事情,無論是用反斜槓逃脫他們...

my $b = EVAL "sub (\$one) \{say \$one;}"; 

...或使用非插值字符串字面量:

my $b = EVAL 'sub ($one) {say $one;}'; 
my $b = EVAL Q[sub ($one) {say $one;}];