!unlike has asked for the wisdom of the Perl Monks concerning the following question:
After a while, and much learning, I realised I could shorten this to print STDERR $spinner[$i++ % @spinner]. I then went away happy thinking that was the best anyone could do. Until the other day. Out of the blue the following code "popped" into my head:my $i = 0; my @spinner = ("|\r", "/\r", "-\r", "\\\r"); while (<>) { # or any other looping structure print STDERR $spinner[$i]; $i = $i++ % @spinner; # do stuff }
I see this as an improvement on my last attempt for the following reasons:sub spinner { my $i; my @chars = defined @_ ? @_ : ("|\r", "/\r", "-\r", "\\\r"); return sub { "$chars[$i++ % @chars]" }; } my $spinner = spinner(); while (<>) { print STDERR $spinner->(); # do stuff }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Way of the Spinner (repost)
by Callum (Chaplain) on Jan 17, 2003 at 12:04 UTC | |
|
Re: Way of the Spinner (repost)
by demerphq (Chancellor) on Jan 17, 2003 at 16:23 UTC | |
by TheDamian (Vicar) on Jan 18, 2003 at 19:06 UTC | |
by !unlike (Beadle) on Jan 23, 2003 at 09:08 UTC | |
by demerphq (Chancellor) on Jan 20, 2003 at 16:08 UTC | |
by HamNRye (Monk) on Jan 17, 2003 at 22:07 UTC | |
by Anonymous Monk on Jan 18, 2003 at 03:59 UTC | |
by joeface (Pilgrim) on Jan 18, 2003 at 04:09 UTC | |
by demerphq (Chancellor) on Jan 20, 2003 at 15:55 UTC | |
|
Re: Way of the Spinner (repost)
by BrowserUk (Patriarch) on Jan 17, 2003 at 16:55 UTC | |
by Aristotle (Chancellor) on Jan 18, 2003 at 16:52 UTC | |
by demerphq (Chancellor) on Jan 20, 2003 at 16:02 UTC | |
|
Re: Way of the Spinner (repost)
by Anonymous Monk on Jan 20, 2003 at 18:57 UTC |