To give a context, this question is part of my continuing quest to port Python's tqdm style (see Part 1 here: Porting tqdm to Perl). So basically I want to wrap an array or a list of values in a for() loop like this:

for("some", "el", "ems") { ... }
with some kind of iterator using this syntax:
for (wrapper("some", "elems")) { # some code }

So that for each iteration, as Perl executes some code, each element retrieval also gets to run my code, so I can do something. In the case of tqdm, I want to measure how long some code run so I can give feedback to user on how long the whole loop is going to take. Thus, in essence, adding a progress indicator just by wrapping the list of values given to for().

In Python, this is easy to do because for() already expects an iterator.

In Perl, I can use the array tie mechanism to do this:

tie @ary, "My::Class", "some", "el", "ems"; for (@ary) { some_code($_); }

The order of code being executed will be:

My::Class::TIEARRAY
My::Class::FETCHSIZE
My::Class::FETCH(0) -> "some"
some_code("some")
My::Class::FETCHSIZE
My::Class::FETCH(1) -> "el"
some_code("el")
My::Class::FETCHSIZE
My::Class::FETCH(2) -> "ems"
some_code("ems")

So far so good. However, I also want a nice syntax like in the Python version. Condensing the above syntax to what I want is still not possible: for (tie @ary, "My::Class", "some", "el", "ems") { ... } # NOPE

This makes for() only loops over a single value, the tied object.

for (do { tie @ary, "My::Class", "some", "el", "ems"; @ary }) { ... } # NOPE

This will FETCH() all elements first before giving them to for().

Any ideas? So far I'm thinking of creating a custom C<for()> function.

EDIT: Added some clarification/additional explanation.


In reply to Getting for() to accept a tied array in one statement by perlancar

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.