I have been working on a command line script that makes several system calls, some of which take a few seconds (minutes?) to return. The long pauses give the impression that the script has hung, which isn't great. I figured I could address this by adding some kind of visual "still processing" feedback, like an hourglass or pinwheel. Because it's just a command line script, I do it with dots.
My code is below. I would appreciate any feedback or suggestions for improvement. Thank you.

use strict; use threads; use threads::shared; sub SystemCall { my $call = join ' ', @_; local $| = 1; # run hot pipes print "Executing system call '$call'"; # Start a thread to print out dots indicating that Perl hasn't hun +g my $thread_beat = 0.1; # Should be small, but not so small as to f +lood the screen with dots. my $stop :shared = 0; my $thread = threads->create( {'context' => 'void'}, sub { while ( not $stop ) { print '.'; select( undef,undef,undef, $t +hread_beat ) } }); $thread->detach(); # Run the command my $result; eval { chomp( $result = `$call` ) }; warn $@ if $@; # eval failed. # Signal the thread to end. $stop = 1; # And wait one beat for the thread to end. select( undef,undef,undef, $thread_beat + 0.01 ); print $/; # Present error message status, if any. warn "\$? = '$?'\n" if defined $? and $?; warn "\$! = '$!'\n" if defined $! and $!; warn "\$@ = '$@'\n" if defined $@ and $@; warn "\$^E = '$^E'\n" if defined $^E and $^E; warn "\$^W = '$^W'\n" if defined $^W and $^W; if ( $result ) { print $result, $/ } return defined $result ? $result : ''; }

In reply to Using a thread to provide visual feedback. by Anonymous Monk

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.