That seems like a straight translation from C, if the printf still lost in there is any hint. That hypothesis would also explain doing this using a heavily copypasted switch construct, which is a common notion in C culture. But this is Perl, not C, so code smarter, not harder.

sub rotate_line { my $interval = 1; # Sleep time between twirls my $tcount = 0; # For each tcount the line twirls one incremen +t my @baton = ( '-', '\\', '|', '/' ); while ($interval) { $tcount++; print "$tcount\n"; print $baton[ $tcount % @baton ]; sleep $interval; } }

(Actually you can trivially do the same thing in C if you use a string to store the baton characters and then address single characters with a bit of pointer math, but I haven’t seen this a lot. C programmers rather like their switches for some reason.)

Your loop doesn’t terminate either, but it’s obviously only example code so I suppose it doesn’t matter.

There was also a CPAN module to provide a tied scalar that returns the next character in progression on every read, but its name escapes me and neither search.cpan.org nor Google are any help right now.

Makeshifts last the longest.


In reply to Re: Twirling baton progress indicator in Perl by Aristotle
in thread Twirling baton progress indicator in Perl by axl163

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.