tqdm is a Python progress bar/meter library which has a simple interface: you just wrap an iterable:

for i in range(100): ...

with:

for i in tqdm(range(100)): ...

and voila, you have a progress bar. I wonder how we can do something similar in Perl. If we create a tqdm() function which returns a tied array, e.g.:

for (tqdm(1..100)) { ... }

then Perl will FETCH all the items first before starting the loop, defeating the progress measuring.

We can make tqdm() function return a tied scalar for every element, but that will be more inefficient and introduce additional side effects.

We can have tqdm() return a tied scalar, something like:

for (my $i = tqdm(100); $i < 100; $i++) { ... }

but: 1) $i is still a tied scalar; 2) C-style loop is less nice and less often used; 3) the above syntax is repetitive.

Any ideas?

UPDATE 2019-04-16: We did it! In Perl we can indeed support the same syntax by using a combination of tied array and lvalue wrapper. Read all about it in Getting for() to accept a tied array in one statement.


In reply to Porting tqdm to Perl 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.