Jon Portnoy > Bot-Cobalt-0.014 > Bot::Cobalt::Frontend::Utils

Download:
Bot-Cobalt-0.014.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.014   Source   Latest Release: Bot-Cobalt-0.016002

NAME ^

Bot::Cobalt::Frontend::Utils - Helper utils for Bot::Cobalt frontends

SYNOPSIS ^

  use Bot::Cobalt::Frontend::Utils qw/ :all /;
  
  my $do_something = ask_yesno(
    prompt  => "Do some stuff?"
    default => 'y',
  );
  
  if ($do_something) {
    ## Yes
  } else {
    ## No
  }

  ## Ask a question with a default answer
  ## Keep asking until validate => returns undef
  my $answer = ask_question(
    prompt  => "Tastiest snack?"
    default => "cake",
    validate => sub {
      $_[0] ~~ ['cake', 'pie', 'cheese'] ?
        undef : "Snack options are cake, pie, cheese"
    },
  );

DESCRIPTION ^

This module exports simple helper functions for use by Bot::Cobalt frontends.

The exported functions are fairly simplistic; take a gander at Term::UI if you're looking for a rather more solid terminal/user interaction module.

EXPORTED ^

ask_yesno

Prompt the user for a yes or no answer.

A default 'y' or 'n' answer must be specified:

  my $yesno = ask_yesno(
    prompt  => "Do stuff?"
    default => "n"
  );

Returns false on a "no" answer, true on a "yes."

ask_question

Prompt the user with a question, possibly with a default answer, and optionally with a code reference to validate.

  my $ans = ask_question(
    prompt  => "Color of the sky?"
    default => "blue",
    validate => sub {
      $_[0] ~~ [qw/blue pink orange red/] ?
        undef : "Valid colors: blue, pink, orange, red"
    },
    die_if_invalid => 0,
  );

If a validation coderef is specified, it should return undef to signify successful validation or an error string describing the problem.

If die_if_invalid is specified, an invalid answer will die() out rather than asking again.

AUTHOR ^

Jon Portnoy <avenj@cobaltirc.org>

http://www.cobaltirc.org

syntax highlighting: