IO::Prompt::Simple - provide a simple user input
# foo.pl use IO::Prompt::Simple; my $answer = prompt 'some question...'; print "answer: $answer\n"; # display prompt message, and wait your input. $ foo.pl some question: foo[Enter] answer: foo
IO::Prompt::Simple is porting ExtUtils::MakeMaker's prompt() function.
Added a few more useful features.
THIS MODULE IS ALPHA LEVEL INTERFACE!!
Display prompt message and wait your input.
$answer = prompt $message;
Sets default value:
$answer = prompt 'sets default', 'default'; is $answer, 'default';
or
$answer = prompt 'sets default', { default => 'default' }; is $answer, 'default';
Display like are:
sets default [default]: [Enter] ...
supported options are:
Sets default value.
Choose any one.
$answer = prompt 'choose', { anyone => [qw/y n/] };
choose (y/n) : [Enter] # Please answer `y` or `n` choose (y/n) : y[Enter] ...
If you specify HASHREF, returned value is HASHREF's value.
$answer = prompt 'choose', { anyone => { y => 1, n => 0 } }; is $answer, 1; # when you input is 'y'
And, when you specify the verbose option, you can tell the user more information.
$answer = prompt 'choose your homepage', { anyone => { google => 'http://google.com/', yahoo => 'http://yahoo.com/', bing => 'http://bing.com/', }, verbose => 1, };
# bing => http://bing.com/ # google => http://google.com/ # yahoo => http://yahoo.com/ choose your homepage : [Enter] # Please answer `bing` or `google` or `yahoo` choose your homepage : google[Enter] ...
If you want preserve the order of keys, you can use Hash::MultiValue.
$answer = prompt 'foo', { anyone => { b => 1, c => 2, a => 4 } }; # prompring => `foo (a/b/c) : ` $answer = prompt 'foo', { anyone => Hash::MultiValue->new(b => 1, c => 2, a => 4) }; # prompring => `foo (b/c/a) : `
Or, you can use REF-ARRAYREF.
$answer = prompt 'foo', { anyone => \[b => 1, c => 2, a => 4] };
Alias of anyone
anyone
Returned multiple answers. Your answer are evaluated separated by space.
use Data::Dumper; @answers = prompt 'choices', { choices => [qw/a b c/], multi => 1, }; print Dumper \@answers;
choices (a/b/c) : c a[Enter] $VAR1 = [ 'c', 'a' ];
Or, you can specify HASHREF:
use Data::Dumper; @answers = prompt 'choices', { choices => { google => 'http://google.com/', yahoo => 'http://yahoo.com/', bing => 'http://bing.com/', }, verbose => 1, multi => 1, }; print Dumper \@answers;
# bing => http://bing.com/ # google => http://google.com/ # yahoo => http://yahoo.com/ choices: google yahoo[Enter] $VAR1 = [ 'http://google.com/', 'http://yahoo.com/' ];
Sets regexp for answer.
$answer = prompt 'regexp', { regexp => '[0-9]{4}' };
regexp : foo[Enter] # Please answer pattern (?^:[0-9{4}]) regexp : 1234 ...
It regexp and anyone is exclusive (anyone is priority).
regexp
Ignore case for anyone or regexp.
# passed `Y` or `N` $answer = prompt 'ignore_case', { anyone => [qw/y n/], ignore_case => 1, };
Shortcut of { anyone => \[ y => 1, n => 0 ], ignore_case => 1 }.
{ anyone => \[ y => 1, n => 0 ], ignore_case => 1 }
$answer = prompt 'are you ok?', { yn => 1 };
are you ok? (y/n) : y[Enter]
Force using for default value. If not specified defaults to an empty string.
$answer = prompt 'use default', { default => 'foo', use_default => 1, }; is $answer, 'foo';
I think, CLI's --force like option friendly.
--force
Sets input file handle (default: STDIN)
$answer = prompt 'input from DATA', { input => *DATA }; is $answer, 'foobar'; __DATA__ foobar
Sets output file handle (default: STDOUT)
$answer = prompt 'output for file', { output => $fh };
Sets encoding. If specified, returned a decoded string.
Sets prompt color. Using Term::ANSIColor.
$answer = prompt 'colored prompting', { color => [qw/red on_white/] };
If prompt() detects that it is not running interactively and there is nothing on $input or if the $ENV{PERL_IOPS_USE_DEFAULT} is set to true or use_default option is set to true, the $default will be used without prompting.
$input
$ENV{PERL_IOPS_USE_DEFAULT}
use_default
$default
This prevents automated processes from blocking on user input.
xaicron <xaicron {at} gmail.com>
Copyright (C) 2011 Yuji Shimada (@xaicron).
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
ExtUtils::MakeMaker IO::Prompt
To install IO::Prompt::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm IO::Prompt::Simple
CPAN shell
perl -MCPAN -e shell install IO::Prompt::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.