Gerard's Perl Page
Index
Home
About
Old News
Contribute



Portfolio
metamail
rifle
ubh
whatpix
ydecode

Links
use Perl;
Perl Monks
www.perl.com
ActiveState
CPAN
Perl User Groups
Perl Mongers
Powered By Perl
Template Toolkit
Emacs

Spinning Progess Indicator
by gerard on Thu Feb 1 00:00:00 PST 2001

Here is one of those little spinning progress indicators, to amuse the user while your program is doing something that takes a long time.

    
    #!/usr/bin/perl -w
    
    use strict;
    
    
    
    package Spinner;
    
    
    
    sub new
    
    {
    
        my ($class) = @_;
    
    
    
        bless {
    
            _position => 0,
    
            _picture  => [ '|', '/', '-', '\\', '|', '/', '-', '\\' ]
    
            }, $class;
    
    }
    
    
    
    sub spin()
    
    {
    
        $_[0]->{_position} = ($_[0]->{_position} + 1) % 8;
    
        print $_[0]->{_picture}[$_[0]->{_position}];
    
        print "\r";
    
    }
    
    
    
    
    
    package main;
    
    
    
    $| = 1;
    
    
    
    my $spinner = Spinner->new;
    
    
    
    foreach (1..20) {
    
        $spinner->spin;
    
        sleep 1;
    
    }