The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Ask::Question - an object overloading coderefification to call Ask

SYNOPSIS

  use Ask::Question;
  use Types::Standard qw( ArrayRef Int );
  
  my $question = Ask::Question->new(
    text    => 'Enter numbers',
    type    => ArrayRef[Int],
    default => sub { [0..9] },
  );
  
  my $arrayref_of_numbers = $question->();
  my $more_numbers        = $question->ask();  # alternative way to call

These overloaded objects work nicely as Moose and Moo defaults:

  package Foo {
    use Moo;
    use Ask;
    
    has numbers => (
      is       => 'lazy',
      type     => ArrayRef[Int],
      default  => Ask::Q(
        text     => 'Enter numbers',
        type     => ArrayRef[Int],
        default  => sub { [0..9] },
      ),
    );
  }

Note Ask::Q(...) is a shortcut for Ask::Question->new(...).

DESCRIPTION

Ask::Question provides an alternative approach to using Ask to request information from a user.

Ask::Question provides a fairly standard Moose-like constructor taking a hash of attributes. There's also a shortcut for it in the Ask package. If there are an odd number of arguments passed to the constructor, it is assumed the first one if the text attribute.

  my $question  = Ask::Question->new( $text, %attributes );
  my $question  = Ask::Question->new(        %attributes );
  my $question  = Ask::Question->new(       \%attributes );
  my $question  = Ask::Q( $text, %attributes );
  my $question  = Ask::Q(        %attributes );
  my $question  = Ask::Q(       \%attributes );

Attributes

text Str|CodeRef

The text of the question. If a coderef is given, that coderef will be forwarded any of the arguments to $question->(...).

backend Object

A blessed object implementing Ask::API. Defaults to the result of Ask->detect.

title Str

A title to use for question prompts, used by certain Ask backends.

type TypeTiny

A type constraint to check answers against. If the answer provided by the user fails a type check (after coercion, if the type has a coercion), they will be prompted to answer again.

spec HashRef

If this Ask::Question is being used as the default for an attrbute spec, this can be used to hold the specification hash for the attribute, and Ask::Question will attempt to find missing information like type from the spec hash.

multiple Bool

Indicates that you want to return an arrayref of answers. If type is a subtype of ArrayRef, this will be inferred.

choices ArrayRef[Str]

List of valid choices if the question has a list of valid answers.

  my $question = Ask::Question->new(
    "What size t-shirt would you like?",
    choices => [qw( XS S M L XL XXL )],
  );

If type is a parameterize Enum or ArrayRef[Enum], then this will be automatic.

coderef CodeRef

Generating this coderef is the entire point of Ask::Question. You cannot provide this to the constructor.

default CodeRef|~Ref

A fallback to use if no interaction with the user is possible.

method Str

The method name to call on the backend, such as question or entry. See Ask::API. Generally speaking, Ask::Question will figure this out by itself.

Methods

ask()

$question->ask(...) is a shortcut for $question->coderef->(...).

BUGS

Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Ask.

SEE ALSO

Ask.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2020 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.