perl - perl6/rakudo: Problem with writing on the loop-variable -
#!perl6 use v6; $longest = 3; @list = <a b c d e f>; @list -> $element rw { $element = sprintf "%*.*s", $longest, $longest, $element; $element.say; }
this works. in second , third error-message. how make them work?
#!perl6 use v6; $longest = 3; @list = <a b c d e f>; @list <-> $element { $element = sprintf "%*.*s", $longest, $longest, $element; $element.say; } # ===sorry!=== # missing block @ line 11, near ""
.
#!perl6 use v6; $longest = 3; $list = <a b c d e f>; $list.list -> $element rw { $element = sprintf "%*.*s", $longest, $longest, $element; $element.say; } # cannot modify readonly value # in '&infix:<=>' @ line 1 # in <anon> @ line 8:./perl5.pl # in main program body @ line 1
regarding second example
the <->
may not have worked in rakudo perl used, has been fixed in more recent versions. (it had deep parsing issue required better longest-token-matching algorithm had @ time.)
regarding third example
the statement
my $list = <a b c d e f>;
creates $list
seq
data type, , seq
elements considered immutable. want $list
become array
, in:
my $list = [<a b c d e f>];
with in place, last example works expected:
pmichaud@orange:~/rakudo$ cat x.p6 #!perl6 use v6; $longest = 3; $list = [<a b c d e f>]; $list.list -> $element rw { $element = sprintf "%*.*s", $longest, $longest, $element; $element.say; } pmichaud@orange:~/rakudo$ ./perl6 x.p6 b c d e f pmichaud@orange:~/rakudo$
hope helps!
pm
Comments
Post a Comment