use POSIX qw( WNOHANG ); use Time::HiRes qw( time sleep ); sub min { $_[0] < $_[1] ? $_[0] : $_[1] } sub max { $_[0] > $_[1] ? $_[0] : $_[1] } sub twirling_baton { my ($twitch_interval, $poll_interval, $poll_func) = @_; my $af = ($| = 1); my $tcount = 0; my @baton = qw( | / - \ ); print($baton[0]); my $time = time; my $twitch_time = $time + $twitch_interval; my $poll_time = $time + $poll_interval; for (;;) { sleep(max(min($twitch_time, $poll_time) - $time, 0.250)); # Sleep is not precise. $time = time; if ($time >= $poll_time) { last if &$poll_func(); $time = time; # In case the call is slow. $poll_time = $time + $poll_interval; } if ($time >= $twict_time) { $tcount = ($tcount + 1) % @baton; print("\b", $baton[$tcount]); $twitch_time = $time + $twitch_interval; } } $| = $af; } $pid = fork; if (not defined $pid) { die("Unable to create child process: $!\n"); } if (not $pid) { exec("$program < $temp_file"); die("Unable to launch application: $!\n"); } twirling_baton( 0.250, # Twitch every 500ms 0.500, # Poll every 500ms sub { waitpid($pid, WNOHANG) != 0 }, );