lelf: the unpuzzle is .map produces a Seq, which is evaluated lazily. You call `foo` with a code block that closes over the $out handle, foo() creates a Seq with a map call that calls that closure and returns it. MAIN then returns that Seq and it gets sunk somewhere and only *then* does the `fun($_)` stuff gets run, which is past the point of leaving MAIN and past the point of when the handle got closed. You