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

Download:
Bot-Cobalt-0.016002.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.016002   Source  

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 {
      my ($value) = @_;
      return "No value specified" unless defined $value;
      (grep { $_ eq $value } qw/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 {
      my ($value) = @_;

      return "No value specified" unless defined $value;

      return undef if grep { $_ eq $value } qw/blue pink orange red/;

      return "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: