问题描述:

#!perl6

use v6;

my $longest = 3;

my @list = <a b c d e f>;

for @list -> $element is rw {

$element = sprintf "%*.*s", $longest, $longest, $element;

$element.say;

}

This works. But in the second and third I get an error-message. How could I make them work?

#!perl6

use v6;

my $longest = 3;

my @list = <a b c d e f>;

for @list <-> $element {

$element = sprintf "%*.*s", $longest, $longest, $element;

$element.say;

}

# ===SORRY!===

# Missing block at line 11, near ""

.

#!perl6

use v6;

my $longest = 3;

my $list = <a b c d e f>;

for $list.list -> $element is rw {

$element = sprintf "%*.*s", $longest, $longest, $element;

$element.say;

}

# Cannot modify readonly value

# in '&infix:<=>' at line 1

# in <anon> at line 8:./perl5.pl

# in main program body at line 1

网友答案:

The <-> is known to not work in Rakudo Perl at the moment; we hope to have it fixed in the next couple of months. (It has to do with a deep parsing issue that requires a better longest-token-matching algorithm than we have at present.)

The statement

my $list = <a b c d e f>;

creates $list as a Seq data type, and currently Seq elements are considered to be immutable. What you really want is for $list to become an Array, as in:

my $list = [<a b c d e f>];

With that in place, the last example works as expected:

[email protected]:~/rakudo$ cat x.p6
#!perl6
use v6;

my $longest = 3;
my $list = [<a b c d e f>];

for $list.list -> $element is rw {
    $element = sprintf "%*.*s", $longest, $longest, $element;
    $element.say;
}

[email protected]:~/rakudo$ ./perl6 x.p6
  a
  b
  c
  d
  e
  f
[email protected]:~/rakudo$ 

Hope this helps!

Pm

相关阅读:
Top