Help language development. Donate to The Perl Foundation

IO::Prompt cpan:WBIKER last updated on 2017-10-08


IO::Prompt -- Interactive validating terminal prompt

=for HTML <a href=""><img src=""></a>


    use v6;
    use IO::Prompt;

    my $a = ask( "Defaults to 42, type Num?", 42, :type(Num) );
    say $a.perl;
    say '------------------------------';

    $a = ask( "Defaults to false?", Bool::False );
    say $a.perl;
    say '------------------------------';

    $a = ask( "No default but type Bool?", :type(Bool) );
    say $a.perl;
    say '------------------------------';

    ## OO style ##
    my $prompt =;

    $a = $prompt.ask( "Dot notation?", Bool::False );
    say $a.perl;
    say '------------------------------';

    ## You can override the IO methods for testing purposes
    class IO::Prompt::Testable is IO::Prompt {
        method !do_say( Str $question ) returns Bool {
            say "Testable saying    '$question'";
            say 'Please do not continue questioning';
            return Bool::False; # do not continue
        method !do_prompt( Str $question ) returns Str {
            say "Testable saying    '$question'";
            say "Testable answering 'daa'";
            return 'daa';

    my $prompt_test =;
    $a = $prompt_test.ask_yn( "Testable, defaults to false?", Bool::False );
    say $a.perl;
    say '------------------------------';

    ## You can override the language class attributes
    class IO::Prompt::Finnish is IO::Prompt {
        our $.lang_prompt_Yn        = 'K/e';
        our $.lang_prompt_yN        = 'k/E';
        our $.lang_prompt_yn        = 'k/e';
        our $.lang_prompt_yn_retry  = 'Sano kyllä tai ei';
        our $.lang_prompt_match_y   = m/ ^^ <[kK]> /;
        our $.lang_prompt_match_n   = m/ ^^ <[eE]> /;
        our $.lang_prompt_int       = 'Int';
        our $.lang_prompt_int_retry = 'Anna kokonaisluku';
        our $.lang_prompt_num       = 'Num';
        our $.lang_prompt_num_retry = 'Anna luku';
        our $.lang_prompt_str       = 'Str';
        our $.lang_prompt_str_retry = 'Anna merkkijono';

    my $prompt_fi =;
    $a = $prompt_fi.ask( "Suomeksi Bool?", :type(Bool) );
    say $a.perl;
    say '------------------------------';
    $a = $prompt_fi.ask( "Suomeksi Num?", :type(Num) );
    say $a.perl;
    say '------------------------------';
    $a = $prompt_fi.ask( "Suomeksi Str?", :type(Str) );
    say $a.perl;
    say '------------------------------';


This is a generic module for interactive prompting
from the console. 

The build-in function prompt is great, but sometimes it is convenient
to have default values and/or the control over the return type.

IO::Prompt provides both. 

Original written bei pnu.


IO::Prompt provides method ask to set the question string, a default answer and/or
a return type.


    use IO::Prompt;

    my $answer = ask("What do you want for Christmas?", "Perl6");
    $answer = Whatever the user entered or if just Enter was pressed the string Perl6

=head2 OO

IO::Prompt provides also a OO interface.


    use IO::Prompt;

    my $asker =;
    my $answer = $asker.ask("Wanna some money?", 2e9, :type(Num));


Written by pnu, maintained by wbiker