For interactive text programs, creates a spinning bar to let the user know the program is still working away. Sample use while slowly processing through a file:
$| = 1; # autoflush required while (<$fh>) { spinner if !($. % 100); # process line }
{ my @spin = qw(- \\ | /); my $time = time; sub spinner { if ($time != time) { ++$time; my $spin = shift @spin; print STDOUT "\r$spin"; push @spin, $spin; } } }

Replies are listed 'Best First'.
Re: spinner to keep the patiently waiting user patient
by jweed (Chaplain) on Dec 08, 2003 at 05:08 UTC
Re: spinner to keep the patiently waiting user patient
by atcroft (Abbot) on Dec 08, 2003 at 04:05 UTC

    Interesting... are whirleygigs and spinners also one of the rites of passage as one becomes more adept at perl? (Just curious...)

    As to the code, I don't think I've seen one that waited for the timer-tick like that. Would the use of "\r" be an issue? (I would have suspected the use of the backspace character "\b" instead, but I haven't tried it myself yet... might also depend on the terminal, though.)

    Personally, I tend to do something like the following (adapted here from a previous posting), but that's just me:

    #!/usr/bin/perl -w use strict; $| = 1; my @spinner = ('|', '/', '-', '\\'); my $bigscale = 100_000; my $scale = 100; my $index = 0; # replace the following two lines # with a loop appropriate to the project my $in_loop = 1; while ($in_loop) { # Do work. print $spinner[($index++ / $scale) % 4], "\b"; print '.' unless ($index % $bigscale); # And for completeness, for demonstration purposes... $in_loop = 0 if ($index > (($scale / 2) * $bigscale)); }

    Updated: (08 Dec 2003) Corrected use of "rites" instead of "rights." (Thank you, liz, for the correction.)

Re: spinner to keep the patiently waiting user patient
by RMGir (Prior) on Dec 08, 2003 at 17:17 UTC
    That's a cool way to do it. Easy to follow, too, which is a big plus.

    One thing you have to keep in mind, though, is that shift and push are expensive. It's a lot cheaper to index++ % @spin.

    It doesn't matter for this code, since you're only going to shift/push once per second, but you might want to keep it in mind if you ever need to do this kind of array looping in a tighter loop.

    ObBenchmark:

    On a perl 5.8 Linux system, using an index clocks in 133% faster than using shift/push.


    Mike
      For longer running code in a tight loop, you also need to be aware that at some point $ndx++ stops working. For instance, with normal IEEE doubles:
      $ perl -we'$x = $y = 2**53-1; ++$x; print $x-$y' 1 $ perl -we'$x = $y = 2**53; ++$x; print $x-$y' 0
      On my computer, in a tight loop, you could reach this point in just 30 years :)
        Oh well, if your loop is a bit looser, Y2038 could get you before 2^53 could. :)

        Mike
      You have a point, but.. Running your benchmark on my machine gives me a rate of over 900,000 shift spins per sec. That is about 45,000 times faster than I need it.

      Makeshifts last the longest.