I only started learning Perl 6 within the last week, so please bear with me. :-)

I had a script working nicely, then I added another statement (line D in the code below) and received a run-time error message like this:

The iterator of this Seq is already in use/consumed by another Seq (you might solve this by adding .cache on usages of the Seq, or by assigning the Seq into an array)

From the documentation I gather that I have somehow created a Seq object and then attempted to iterate it a second time. But I don’t understand where or how the Seq object is being created and used.

I managed to pare down the code to the following SSCCE:

use v6; my @paths = data(); my @dirs.push: $_.split('/') for @paths; # A # @dirs.perl; # B say $_.join('/') for @dirs; # C my $depth = @dirs.map(*.elems).min; # D sub data { my $text = q:to/END/; /aardvark/bison/camel/dromedary /aardvark/bison/camel/dromedary/elephant END return $text.lines; }

Lines A, C, and D are all required to produce the error. Uncommenting line B removes the error (by caching, apparently, although I’m not sure exactly what is cached). So I have 3 questions:

  1. Where are the Seq object(s) being created and used?
  2. I tried examining the contents of @dirs using say @dirs.perl;, but that had the same effect as uncommenting line B. Is there a way to examine the contents of @dirs (like using Data::Dump in Perl5) without introducing changes?
  3. Adding line B removes the error, but feels like a kludge. What is the correct way of writing this sort of code?

In case it matters:

11:03 >perl6 -v This is Rakudo Star version 2019.03.1 built on MoarVM version 2019.03 implementing Perl 6.d. 12:14 >

Thanks,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to [Perl6] Seq iterator already consumed by Athanasius

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.