Handling user input and program busy

M

mike

Hi,

I am used to shell script ( bourne) and wonder how you can do the
following in perl:

(I stick to the command line input no fancy gui).

* if user has a Y/N question and Y is default and the user only
presses "Enter". How can I set a default answer to be used.

* Also I have been wondering how I can show " dots" on the command
line when an activity is ongoing ( to let the user know something is
going on. Any functions that is easy to implement?

cheers,

//mike
 
J

Jürgen Exner

mike said:
(I stick to the command line input no fancy gui).

* if user has a Y/N question and Y is default and the user only
presses "Enter".

Then the read line will be the empty line.
How can I set a default answer to be used.

Just check against empty line or Y.
* Also I have been wondering how I can show " dots" on the command
line when an activity is ongoing ( to let the user know something is
going on. Any functions that is easy to implement?

Just print them. However you may have to turn off output buffering.

jue
 
S

smallpond

* Also I have been wondering how I can show " dots" on the command
line when an activity is ongoing ( to let the user know something is
going on. Any functions that is easy to implement?

cheers,

//mike


Here's one method of showing dots while other stuff is running:

#!/usr/bin/perl
use warnings;

use threads;
use threads::shared;
my ($done, $thr);
share $done;

# Show dots at 1-second intervals while sub executes
sub pdots {
while (! $done) {
sleep 1;
print ".";
};
}

# start printing
sub showdots {
$|=1;
$done = 0;
$thr = threads->create( \&pdots );
}

# stop printing
sub stopdots {
$done = 1;
$thr->join;
}

showdots( );

print "Starting 5 second task ";
# do your long-running task here
sleep 5;

stopdots();

print " Done\n";
 
J

Jürgen Exner

smallpond said:
Here's one method of showing dots while other stuff is running:

#!/usr/bin/perl
use warnings;

use threads;
use threads::shared; [...]
showdots( );
print "Starting 5 second task ";
# do your long-running task here
sleep 5;
stopdots();

From a user experience this is a very poor design.
It will keep printing those dots even if the main task got stuck a long time
ago, tricking the user into believing that there is still something useful
going on.
It is much better to give an actual progress indicator, e.g. if you have to
process 10 million entries, then print one dot for every 10000 actually
processed.

jue
 
X

xhoster

smallpond said:
Here's one method of showing dots while other stuff is running:

#!/usr/bin/perl
use warnings;

use threads;
use threads::shared;
my ($done, $thr);
share $done;

# Show dots at 1-second intervals while sub executes
sub pdots {
while (! $done) {
sleep 1;
print ".";
};
}

Ugh. That just tells me that time has not ceased to exist as far as the
computer is concerned. That is not a problem I've ever run into. I
want a progress meter to tell me that progress is actually occurring on
the relevant task, not that the arrow of time is still in place and
functioning.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
T

Ted Zlatanov

x> Ugh. That just tells me that time has not ceased to exist as far as the
x> computer is concerned. That is not a problem I've ever run into. I
x> want a progress meter to tell me that progress is actually occurring on
x> the relevant task, not that the arrow of time is still in place and
x> functioning.

It is useful, however, to separate progress indications from the actual
worker thread. Then the progress thread can show the worker thread is
blocked, for instance (and even do something about it). I usually put
counters in shared memory; that way I can also access them for graphical
display in Cacti and I don't need threads. The Tie::ShareLite CPAN
module makes it all trivial.

Ted
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top