Getting "value without a container" error

Got this:

for $config.IO.slurp.lines <-> $l {
  $l .= trim;
  ...
}

Get this:

t/01-basic.rakutest ..3/5
Parameter '$l' expects a writable container (variable) as an argument,
but got '# karabiner config file' (Str) as a value without a container.
  in sub generate_file at...

I've read the docs on containers but it didn't shed any light on what I can do in this situation aside from maybe assigning $l to a scalar variable, which seems hacky. Is there a way I can containerize $l?


Solution 1:

The issue is really that .lines does not produce containers. So with <->, you would bind to the value, rather than a container. There are several ways to solve this, by containerizing as you suggested:

for $config.IO.slurp.lines -> $l is copy {
    $l .= trim;
    ...
}

But that only makes sense if you want to do more changes to $l. If this is really just about trimming the line that you receive, you could do this on the fly:

for $config.IO.slurp.lines>>.trim -> $l {
    ...
}

Or, if you need to do more pre-processing $l, use a .map:

for $config.IO.slurp.lines.map({
    .trim.subst("foo","bar",:g)
}) -> $l {
    ...
}