Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl6 Any string port similar to Racket Scheme for reading data?

Tags:

string

eval

raku

In Racket Scheme, there is a data structure called "string port" and you can read data from it. Anything similar in perl6? For examples, I want to achieve these outcomes:

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

The function EVAL is not quite doing the full spectrum 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;}";

Thanks a lot!

lisprog

like image 788
lisprogtor Avatar asked Jan 21 '26 02:01

lisprogtor


1 Answers

EVAL does this.

The problem in your last example, is that double-quoted strings interpolate $ variables and { blocks and similar. In order to represent such things in a string literal, either escape 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;}];
like image 143
smls Avatar answered Jan 23 '26 21:01

smls