in reply to Porting tqdm to Perl

Maybe you should better explain what tqdm actually does, I have to guess in the following:

IIRC are (should be?) all iterators in Python objects inheriting from a class Iteratable.

tqdm can just take one such object and return another enhanced iteratable one. (please correct me if I'm wrong)

Perl has a multitude of different iterators, finding a standard procedure to change them should be difficult.

I think the best way to go is to write tqdm into the loop (if it does what I understand)

for (1..100) { tqdm; # same as tqdm($_); ... }

Now the problem you'd be facing is extra code for initialisation, i.e. how can you tell that the loop was freshly entered or not?

IMHO the default answer in Perl is to initialize that tqdm right before the loop starts.

for my $outer (1..10) { my $tqdm = ProgressBar->new(); # or another constructor for (1..100) { &$tqdm; ... } }

Of course TIMTOWTDI, but I hope this helps.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^2: Porting tqdm to Perl
by perlancar (Hermit) on Dec 21, 2018 at 14:07 UTC

    In an answer to haukex, I've included a link to a blog post that mentions how tqdm might be used. The tqdm PyPI page itself (which I linked in the original post) is also very informative already. But as usual I probably didn't communicate clearly what exactly I want to accomplish, so let me reiterate: how would one create a progress bar library in Perl that is roughly as simple to use to an existing script. By simple I mean with as minimal change as possible, adding as few characters of code as possible. And with existing script, this implies that some uses for() and some uses while() (I personally use for() far more often than while()).

    In this interface:

    for (1..100) { tqdm; # same as tqdm($_); ... }

    we can just assume that the first time tqdm() is first invoked is the start of the process. But tqdm() didn't receive the target number (100).

      and how does tqdm handle infinite iterators?

      or how would you handle readline without knowing the number of lines?

      or circular iterators?

      > But as usual I probably didn't communicate clearly what exactly I want to accomplish

      indeed ;-)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        For a non-infinite iterator (with a known end target), tqdm or a typical progress bar library can show the progress percentage, e.g.:

         20%|###        | 200/1000 (00:03<00:09 98.3it/s)

        For an infinite iterator, like in the case of this command-line (tqdm is also available as a CLI):

        % seq 999999 | tqdm

        the progress library could just show something like:

        203495 (00:03<00:09 99988.3it/s)

        or:

         ??%|      ###  | 203495 (00:03<00:09 99988.3it/s)

        where the progress bar is just animated with a running ### bar, like you would see in various UI.

        A circular iterator wouldn't be problematic, the progress bar would just be reset and start again accordingly. But of course normally the user will not attach a circular iterator if he/she wants to display a progress bar for a process that ends after a certain time.