package Cisco::Utils::Internal::GetFromUser; $VERSION = 0.05.20; @EXPORT = qw(GetFromUser); use strict; use base 'Exporter'; use Term::ReadKey; use Term::ReadLine; sub GetFromUser { my $echo = shift(@_); my $timeout = shift(@_); my @prompt = @_; my $pass; $echo = 0 unless $echo == 1; $timeout = 60 unless(($timeout =~ /\d+/) and ($timeout >= 1)); @prompt = '' unless defined @prompt; ReadMode('noecho') if $echo == 0; print $_ for @prompt; if ($^O eq 'MSWin32') { chomp ($pass = ); } else { $pass = ReadLine($timeout); } ReadMode('restore') if $echo == 0; unless(defined($pass)) { return 'Sorry, you waited too long to enter something.'; } chomp $pass; return $pass; } 1; =head1 NAME GetFromUser =head1 SYNOPSIS GetFromUser is intended for use by Cisco::Utils modules. It's not meant for use directly from Perl scripts, and it's interface may change at any time without notice. GetFromUser accepts 2 scalars and 1 array for input values: 1 - echo-to-screen: 1 for yes, 0 for no (default of no) 2 - timeout seconds: integer greater than zero (default of 60) 3 - prompt: text to prompt user to provide input (default of '') User prompt is here so echo disabled *before* user is prompted for input. Term::ReadKey provides no-echo while user typing password. Term::ReadLine provides timeout if wait too long. Win32 doesn't support timeout, 2nd arg still required before prompt text. #!/usr/bin/perl -w use strict; use Cisco::Utils::Internal::GetFromUser; my $echo = 0; my $timeout = 120; my $favColor = GetFromUser( $echo, $timeout, "\nThe old man at the ropebridge asks\n", 'Wots yer favorite color? ', ); if $favColor eq 'Sorry, you waited too long to enter something.' { print "\nWooooaaahh!!\n\n" } else { print "\n$favColor, eh? You may pass.\n\n"; } =head1 UPDATE 2001-11-18 22:20 CDT Original working code. =head1 TODO Investigate Term::Prompt for cross-platform no-echo. Figure out enough h2xs to package this module for distribution. =head1 BUGS None that I know of. =head1 REQUIRES Term::ReadKey Term::ReadLine =head1 AUTHOR ybiC =head1 CREDITS Thanks to Petruchio for kickstarting me into modulitazing my Ciscoey scripts. And to vroom for PerlMonks. =cut