Ask

 view release on metacpan or  search on metacpan

lib/Ask/Question.pm  view on Meta::CPAN

    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 L<Moose> and L<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 C<< Ask::Q(...) >> is a shortcut for C<< Ask::Question->new(...) >>.

=head1 DESCRIPTION

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

L<Ask::Question> provides a fairly standard Moose-like constructor taking
a hash of attributes. There's also a shortcut for it in the L<Ask> package.
If there are an odd number of arguments passed to the constructor, it is
assumed the first one if the C<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 );

=head2 Attributes

=over

=item C<< text >> I<< Str|CodeRef >>

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

=item C<< backend >> I<< Object >>

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

=item C<< title >> I<< Str >>

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

=item C<< type >> I<< 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.

=item C<< spec >> I<< 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 C<type> from
the spec hash.

=item C<< multiple >> I<< Bool >>

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

=item C<< choices >> I<< 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 C<type> is a parameterize B<< Enum >> or B<< ArrayRef[Enum] >>, then
this will be automatic.

=item C<< coderef >> I<< CodeRef >>

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

=item C<< default >> I<< CodeRef|~Ref >>

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

=item C<< method >> I<< Str >>

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

=back

=head2 Methods

=over

=item C<< ask() >>

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

=back

=head1 BUGS

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

=head1 SEE ALSO

L<Ask>.



( run in 1.649 second using v1.01-cache-2.11-cpan-0b5f733616e )