Home-built Twirling Bar Between Multiple System Calls

F

foolishbrat

Dear all,
AFAIK, progress/twirling bar can only be applied under loops. For
example we can use Term::progressBar or Smart::Comments. But is there
away to do it for system call like this:

__BEGIN__
#!/usr/bin/perl
system("code1.out param1 > output.txt");
system("code2.out param2 output.txt > output_final.txt");
__END__

How can I show progress/twirling Bar between first (code1) system call
and the second (code2) system call? Especially we don't know how long
each system call will take.

Preferrably not using external CPAN module (except pre-installed
module).
 
M

Martijn Lievaart

Dear all,
AFAIK, progress/twirling bar can only be applied under loops. For
example we can use Term::progressBar or Smart::Comments. But is there
away to do it for system call like this:

__BEGIN__
#!/usr/bin/perl
system("code1.out param1 > output.txt"); system("code2.out param2
output.txt > output_final.txt"); __END__

How can I show progress/twirling Bar between first (code1) system call
and the second (code2) system call? Especially we don't know how long
each system call will take.

Preferrably not using external CPAN module (except pre-installed
module).

Easy. Fork to do the system call. In the parent show the progress bar and
check with wait_pid if the child is ready.

May or may not work on Windows, though, no idea really.

HTH,
M4
 
J

Joe Smith

Martijn said:
Easy. Fork to do the system call. In the parent show the progress bar and
check with wait_pid if the child is ready.

May or may not work on Windows, though, no idea really.

#!/usr/bin/perl
use strict; use warnings;
$|++;

our $child_pid; # Holds PID while child is running
$SIG{CHLD} = sub { $child_pid = 0; }; # When child has exited
# That works for POSIX-like systems (Linux, Cygwin), but not Windows-XP

my $cmd = 'sleep 12'; # Example for code1.out
if ($child_pid = fork) {
print "Parent waiting for child $child_pid to finish: ";
my $count;
while ($child_pid) {
printf "%s%4d\b\b\b\b\b", substr('-/|\\',++$count%4,1), $count;
sleep 1;
}
print "Done!\n";
} else {
print "Child $$ running `$cmd`\n";
exec $cmd; # code1.out runs here
}
system "echo code2.out runs here";
print "Finished\n";

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top