Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make my twirlling baton work! 3

Status
Not open for further replies.

nawlej

Programmer
Mar 26, 2004
380
US
Hey everyone! I have done it again....Run into a problem. Im trying to create a "Twirlling baton" progress indicator, but all the output from this script comes up after it runs, and prints to STDOUT> Can anyone tell me how to fix this to make it appear to be a twirlling baton?

#!/usr/local/bin/perl -w

use strict;

my $timer = 180;

printf(STDOUT "Please wait.\n This could take several minutes.\n");

#initialize the time to 1 and the twirl to 1
my $time = 1;
my $count = 1;

while($time != $timer){

if ($count == 5){$count = 1;}

for ($count){
/1/ and do { printf(STDOUT "-\b"); };
/2/ and do { printf(STDOUT "\\\b"); };
/3/ and do { printf(STDOUT "|\b"); };
/4/ and do { printf(STDOUT "\/\b"); };
}# - for ($count)
sleep 1;
$count++; #increment the count and sleep timer.
$time++;
}

print "Count done.";
 
nawlej

you don't need the use Filehandle or the flush STDOUT as [red]$|=1[/red] takes care of this

Code:
my $curr_app = "Test";
my $curr_node = "testnode";
my $timer = 180;
print "Please wait. This could take several minutes.\n";
$|=1;
my @twirl = ("-","\\","|","\/");
for (my $time=1; $time<$timer; $time++)
{
  print "\b$twirl[$time % [blue]scalar @twirl[/blue]]";
  sleep 1;
}
print "\nCount done.";

and the scalar @twirl will count the elements in the array - neater as if you alter the number of elements the script will still work


Kind Regards
Duncan
 
rather than spending precious time bickering with me why don't you grasp what i'm trying to say? They both perform the same task so take out the $|=1 and leave in the use Filehandle and flush STDOUT then!


Kind Regards
Duncan
 
nawlej,

what version of perl on what OS are you using?

--Paul
 
rather than spending precious time bickering with me why don't you grasp what i'm trying to say? They both perform the same task so take out the $|=1 and leave in the use Filehandle and flush STDOUT then!

Please dont take that as bickering....I was just informing you, I had to flush STDOUT.
 
I just thought that mebbe it might have been a limitation on an older version of Perl, but 5.8 is fairly current

--Paul
 
Thought there might be a mention of it in perl-porters somewhere, but I didn't find one. Maybe you're just special. :)

Using FileHandle or IO::File or whatever is generally slower if for no other reason, you're parsing, compiling, and loading those external modules instead of using a built-in. Different systems handle buffering differently, and for whatever reason, yours doesn't like the standard dollar-pipe. I wouldn't fret over it.

________________________________________
Andrew - Perl Monkey
 
Thought there might be a mention of it in perl-porters somewhere, but I didn't find one. Maybe you're just special.

Yeah. Something on that one box is flukey. I ran the same script on my SPARC at home. It works without the flush. Odd.
 
A little bit more sophisticated attempt for building twirling baton.
Code:
#!/usr/bin/perl -w
use strict;
use FileHandle;


# ======================================================
# Twirling baton
# ======================================================

{
   #
   # Pid of twirling baton subprocess;
   #

   use sigtrap;
   my $twirlId = 0;
   
   #
   # stopTwirl() - Stop the twirling baton
   # 

   sub stopTwirl {
      kill ('TERM', $twirlId) if $twirlId;
      waitpid($twirlId,0);
      $twirlId = 0;
   }
   
   #
   # startTwirl($duration, $wait) - Start the twirling baton
   # $duration = Duration in seconds (Def 0=infinite)
   # $wait = Wait twirle baton 0=No (Def)
   #
   # If nowait, a subprocess is created
   #

   sub startTwirl {
   
      my ($duration, $wait) = @_; 
      my @twirl = ("-","\\","|","\/");
      my $done = 0;
   
      if (! defined($duration)) { $duration = 0 };
      if (! defined($wait))     { $wait     = 0 };
      $| = 1;
   
      stopTwirl;
      if (! $wait) {
         $twirlId = fork;
         return if $twirlId;
      }
   
      $SIG{TERM} = sub { $done = 1 } unless $wait;
      $SIG{CHLD} = sub { $done = 1 } unless $wait;

      do {
        print "\b$twirl[$duration % scalar @twirl]";
        sleep 1;
      } until (--$duration == 0 or $done);
      print "\b \b";
   
      if (! $wait) { exit };
   }
}

# ======================================================
# Main
# ======================================================

#
# Start a 5 seconds twirling baton with wait
#

print "startTwirl(5)  ";
startTwirl(5,1);
print "Done.\n";

#
# Start an infinite asynchronous twirling baton
# Stop it after 15 seconds
#

print "startTwirl     ";
startTwirl;
sleep 15;
stopTwirl;
print "Done.\n";

exit;
On my AIX system, the CHLD signal isn't sent to child processes when the parent process exits.

Jean Pierre.
 
A minor variation on the declaration:
Code:
@frames = split(//, q{-/-\\-});
$| = 1;
print "Processing ...  ";
while (1) {
    print "\b$frames[$frame++ % $#frames]";
    sleep 1;
}
cheers, Neil
 
nice solution toolkit ... but it didn't work!

I shall definitely be picking up some hints from you in the future though... ;-)

Code:
@frames = split(//, q{-/-\\-});
$| = 1;
print "Processing ...  ";
while (1) {
    print "\b$frames[$frame[red]s[/red]++ % $#frames]";
    sleep 1;
}


Kind Regards
Duncan
 
Sorry, didn't use use strict so the first time through the while loop, the $frame scalar evaluates to 0, then gets incremented.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top