Perl6 Any Racket Scheme like string port for reading data?
Racket Scheme has a data structure called "string port" and you can read data from it. Something like this in perl6? For example, I want to achieve these results:
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 does not fulfill the full range of tasks:
> 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;}";
Thank you so much!
lisprog
+3
source to share
1 answer
EVAL
does it.
The problem in your last example is that with double quotes to interpolate $
variables and {
blocks and the like. To represent things like this in a string literal, or avoid them with backslashes ...
my $b = EVAL "sub (\$one) \{say \$one;}";
... or use a non-interpolating string literal:
my $b = EVAL 'sub ($one) {say $one;}';
my $b = EVAL Q[sub ($one) {say $one;}];
+5
source to share