Click here to get back home

Handling user input and program busy

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Handling user input and program busy mike 02-05-2008
Get Chitika Premium
Posted by mike on February 5, 2008, 2:24 pm
Please log in for more thread options
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

Posted by Jürgen Exner on February 5, 2008, 2:33 pm
Please log in for more thread options
>(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

Posted by Jim Gibson on February 5, 2008, 3:29 pm
Please log in for more thread options
In article

> 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).

Here is one way (untested):

query:
print "Prompt [Y]: ";
my $ans = <STDIN>;
chomp($ans);
$ans = 'Y' unless $ans;
if( $ans =~ /^ty/i ) {
...
}elsif( $ans =~ /^fn/i ) {
...
}else{
print "\"$ans\" is not a valid answer\n";
goto query;
}

>
> * 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?

$|++;
for( $i=0; $i<30; $i++ ){
print ".";
sleep(1)
}
print "\n";

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Posted by smallpond on February 5, 2008, 5:20 pm
Please log in for more thread options

> * 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";

Posted by Jürgen Exner on February 5, 2008, 5:32 pm
Please log in for more thread options
>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


Similar ThreadsPosted
localtime - user input - output in same format October 7, 2004, 1:27 am
perl/expect.pm manual user input February 19, 2006, 7:33 pm
Perl output Javascript to validate user input April 4, 2006, 2:50 pm
How to make input user and password in perl script started on web? May 29, 2008, 4:15 pm
Get name of user invoking program? August 4, 2006, 8:18 am
interacting with another program that requires input May 5, 2008, 2:37 pm
Using a Perl program to send different images depending upon the user April 20, 2007, 5:06 am
Running and Feeding Input to Interactive Program in Windows from Perl Script November 26, 2006, 12:30 pm
SOLVED: "Text File Busy" And Other Frustrating NFS/Perl Errors September 12, 2005, 3:36 pm
Looking for a module/program author, or help with program... September 7, 2005, 6:00 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap