in reply to Way of the Spinner (repost)

A slight variation. Use a string and substr rather than an array. I also use a backspace rather than \r so that you can hang the spinner on the end of a line with out having to reprint the whole line each time. Stops the whole line flickering.

#! perl -sw use strict; $|++; sub spinner{ my ($s, $n) = (shift,0); return sub{ substr( $s, $n++ % length $s,1) . "\cH"; } } my $spinner = spinner '-\|/'; print "Working "; print $spinner->() for 1 .. 10000;

Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Replies are listed 'Best First'.
Re^2: Way of the Spinner (repost)
by Aristotle (Chancellor) on Jan 18, 2003 at 16:52 UTC
    TMTOWTDI:
    #!/usr/bin/perl -w use strict; sub make_spinner{ my $s = shift; my $i = 0; my $l = length $s; return sub{ unpack("x".($i++ % $l) . "A1", $s) . "\cH"; } } my $spinner = make_spinner '-\|/'; $|++; print "Working "; print $spinner->() for 1 .. 10000; print "\n";

    Makeshifts last the longest.

Re: Re: Way of the Spinner (repost)
by demerphq (Chancellor) on Jan 20, 2003 at 16:02 UTC
    Hmm.

    I have to admit that while this solution is ok when the spinner is a single char I still prefer the other approach as being more flexible

    use Tie::Cycle; tie my $spinner, 'Tie::Cycle', [map "\b$_",qw(> -> --> ---> ----> ---> + --> -> >)]; print $spinner while (1);
    Aint animation fun!

    :-)

    --- demerphq
    my friends call me, usually because I'm late....