Search code examples
for-looppass-by-referencerakurakudo

perl6/rakudo: dereferencing-question


#!perl6
use v6;

my $list = 'a' .. 'f';

sub my_function( $list ) {
    for ^$list.elems -> $e {
        $list[$e].say;
    }
}

my_function( $list );

First I tried this in perl5-style, but it didn't work:

for @$list -> $e {
    $e.say;
}
# Non-declarative sigil is missing its name at line ..., near "@$list -> "

How could I do this in perl6?


Solution

  • You don't dereference variables like this in Perl 6. Just use for $list

    But that proably won't do what you want to do. 'a'..'f' doesn't construct a list in Perl 6, but rather a built-in data type called Range. You can check that with say $list.WHAT. To turn it into a list and iterate over each element, you'd use for $list.list