http://qs1969.pair.com?node_id=75871

ybiC has asked for the wisdom of the Perl Monks concerning the following question:

I write my Perl scripts on Debian and have started testing them on ActivePerl, in hopes of releasing them to Win32-only cow-orkers.

The snippet below shows the first obstacle in my cross-platform efforts - no SIGARLRM on Win32.   This shouldn't be a show-stopper, as there *must* be alternatives for user-input timeout.   So far I've found Term::Prompt.   Any monks used that module?   What other methods should I consider for timing-out on user input?

BTW, thanks to converter, mothra, dws and Petruchio for suggestions in CB of *cough* dubious practicality {grin}

    cheers,
    Don
    striving toward Perl Adept
    (it's pronounced "why-bick")

#!/usr/bin/perl -w # alarum.pl # 2001-04-26 use strict; use Term::ReadKey; my $pass; print "\n", " Prompting for password\n", " (*not* echoed to screen nor written to disk)\n\n", ' Enter password:'; ReadMode('noecho'); eval { local $SIG{ALRM} = sub { die "ALARUM" }; alarm(60); chomp($pass = <STDIN>); alarm(0); }; if ($@ =~ /ALARUM/) { print "\n\n", " Sorry - you waited too long before entering a password.\n", " Try again, if you want.\n\n"; ReadMode(0); exit; } ReadMode(0);

Replies are listed 'Best First'.
Re: cross-platform timeout on user input
by THRAK (Monk) on Apr 27, 2001 at 00:14 UTC
    I haven't used Term::Prompt, but I did throw this little demo thing together for a former coworker many moons ago. It was written for use on a Unix but should run on WIN (using a WIN cmd obviously) if Term::ReadKey and Term::ReadLine are installed. Note this was written many moons ago, thus no "-w" or "use strict". Hope this might be of some use to you.
    #!/usr/bin/perl use Term::ReadLine; use Term::ReadKey; $in = new Term::ReadLine 'LABEL'; $inval = ""; $prompt = " Enter a Unix command. Enter 'quit' to bailout.\n\n -> "; while ( $inval eq "" ) { $inval = $in->readline($prompt); } if ($inval ne "quit") { #Do this print "You entered: $inval\n"; system("$inval"); } else { print "You said to quit.\n\nBye!\n"; $quit = confirm("quit"); if ($quit eq "n") { print "Decided not to quit I see.\n"; } else { print "OK, OK, I'm quitting...\n"; } } pressanykey(); ################################################################## ################################################################## # SUB: Global Sub-Routines ####################################### ################################################################## ################################################################## ##################### # SUB: Clear Screen # ##################### sub clear { system("clear"); } ###################### # SUB: Press Any Key # ###################### sub pressanykey { print "\n Press any key to continue...\n"; ReadMode ('cbreak'); while (1) { $key = ReadKey(0); last; } ReadMode 'restore'; } ##################### # SUB: Confirmation # ##################### sub confirm { my($action, $key) = @_; clear(); print "\n Are you sure you want to $action? (y/n)"; ReadMode ('cbreak'); while (1) { $key = lc(ReadKey(0)); last unless defined $key; last if (($key eq "y") || ($key eq "n" )); } ReadMode 'restore'; clear(); return($key); }


    -THRAK
    www.polarlava.com
Re: cross-platform timeout on user input
by repson (Chaplain) on Apr 27, 2001 at 06:08 UTC
    It is true that Activestate perls have no capability and if you absolutly must use Activestate then alarm is no supported. However on the perl that comes with the cygwin system (running Win98) I ran your code and other basic uses of an alarm with exactly the same behaviour as on my linux system.

    In case you don't know it, cygwin runs on most windows platforms and has a pile of modular components including the a good selection of those utilities missing from a non unix system: perl, grep, bash, gcc, tar, wget, and many more.

    Cygwin also allows you to compile many perl modules without depending on ppm like Activestate, though in using it some modules avalible in the ppm archives are very difficult/impossible to get to work.

    If you are going to be installing perl on many systems anyway then cygwin may be a good option to explore.

(code) Re: cross-platform timeout on user input (Grrr!! "not supported on this platform")
by ybiC (Prior) on Apr 27, 2001 at 15:25 UTC
    Thanks for the ideas, THRAK and repson.   8^)
    But they don't quite address my quest for knowledge of Win32 perl for timing-out on user input.

    Anyone?   Bueller?   {grin}

    Update:
    Thanks to good monk tye for a polite RTFM   8^)   perldoc Term::ReadKey indicates that it just *might* support timeout internally.   I'm still experimenting with the syntax - will post results.

    Update:
    No go.   "Non-blocking ReadLine is not supported on this architecture" with ActivePerl 5.6 on Win2k, but runs fine on Debian 2.2r3

    $!/usr/bin/perl -w use strict; use Term::ReadKey; use Term::ReadLine; ReadMode('noecho'); print 'enter password:'; my $pass = ReadLine(5); ReadMode('restore'); unless (defined($pass)) { print "\n\nSorry, you waited too long.\n\n"; exit; }

        muttering to himself...
        Don
        striving toward Perl Adept
        (it's pronounced "why-bick")

Re: cross-platform timeout on user input
by isotope (Deacon) on Apr 27, 2001 at 21:27 UTC
    Just a quick suggestion re the following code:
    print "\n", " Prompting for password\n", " (*not* echoed to screen nor written to disk)\n\n", ' Enter password:'; ReadMode('noecho');
    May I suggest turning off echo before prompting for the password? This makes life much better when using things like Expect, which will type the password immediately after the prompt, possibly before you disable echo.

    --isotope
    http://www.skylab.org/~isotope/