The typical approach in Perl would be a while loop...

my $it = tqdm(100); while (my $i = $it->next) { ... }

Plenty of variations are possible, for example, $it doesn't need to be an object, it could just be a code ref; or, if it is an object, it can overload the <> operator (while(<$it>)), etc.

BTW, Higher-Order Perl is great for this kind of stuff.

Update: One advantage of overloading <> is that a while(<$it>) is automatically translated to while (defined($_ = readline $it)), meaning that only undef terminates the sequence, and not other false values like 0:

use warnings; use strict; { package Iterator; use overload '<>' => sub { my $self = shift; if (wantarray) { my @all; while (defined( my $x = $self->() )) { push @all, $x } return @all; } else { return $self->() } }; sub new { my $class = shift; my @values = @_; my $i=0; return bless sub { if ($i>=@values) { $i=0; return } return $values[$i++]; }, $class; } } use Test::More tests=>2; my $it1 = Iterator->new(-5..5); my @o1; while (<$it1>) { push @o1, $_; } is_deeply \@o1, [-5,-4,-3,-2,-1,0,1,2,3,4,5]; SKIP: { skip "need Perl >= v5.18 for overloaded <> in list context", 1 unless $] ge '5.018'; # [perl #47119] my $it2 = Iterator->new(-5..5); is_deeply [<$it2>], [-5,-4,-3,-2,-1,0,1,2,3,4,5]; }

In reply to Re: Porting tqdm to Perl (updated) by haukex
in thread 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.