| 1 | # -*- perl -*- | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # Module::TestConfig::Question - question interface | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | # $Id: Question.pm,v 1.7 2003/08/28 21:02:14 jkeroes Exp $ | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | package Module::TestConfig::Question; | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 8 | require 5.005_62; | |||||||||||||||||||
| 10 | 8 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 8 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | #------------------------------------------------------------ | ||||||||||||||||||||
| 14 | # Methods | ||||||||||||||||||||
| 15 | #------------------------------------------------------------ | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 27 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||
| 18 | 27 | my $proto = shift; | |||||||||||||||||||
| 19 | 27 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 20 | 27 | my $self = bless {}, $class; | |||||||||||||||||||
| 21 | 27 | $self->init( @_ ); | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 27 | ▶ | sub init { | ||||||||||||||||||
|
Subroutine
init: called CC=9 SCAR=22.2
Pod
undocumented
| |||||||||||||||||||||
| 26 | 27 | my $self = shift; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 27 | ▶ | if ( ref $_[0] eq "ARRAY" ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 24 | my @q = @{+shift}; | |||||||||||||||||||
| 30 | 24 | ▶ | $self->msg( shift @q ) if @q; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | 24 | ▶ | $self->name( shift @q ) if @q; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | 24 | ▶ | $self->def( shift @q ) if @q; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | 24 | ▶ | $self->opts( shift @q ) if @q; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | } else { | ||||||||||||||||||||
| 35 | 3 | ▶ | my %args = ref $_[0] eq "HASH" ? %{$_[0]} : @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 3 | ▶ | while ( my ( $method, $args ) = each %args ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 11 | ▶ | if ( $self->can( $method ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 11 | $self->$method( $args ); | |||||||||||||||||||
| 39 | } else { | ||||||||||||||||||||
| 40 | 0 | croak "Can't handle arg: '$method'. Aborting"; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 27 | return $self; | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 115 | ▶ | sub msg { | ||||||||||||||||||
|
Subroutine
msg: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 49 | 115 | my $self = shift; | |||||||||||||||||||
| 50 | 115 | ▶ | $self->{msg} = shift if @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 115 | return $self->{msg}; | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 198 | ▶ | sub name { | ||||||||||||||||||
|
Subroutine
name: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 55 | 198 | my $self = shift; | |||||||||||||||||||
| 56 | 198 | ▶ | $self->{name} = shift if @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 198 | $self->{name}; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 64 | ▶ | sub def { | ||||||||||||||||||
|
Subroutine
def: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 61 | 64 | my $self = shift; | |||||||||||||||||||
| 62 | 64 | ▶ | $self->{default} = shift if @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 64 | $self->{default}; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 9 | ▶ | sub opts { | ||||||||||||||||||
|
Subroutine
opts: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||
| 67 | 9 | my $self = shift; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 9 | ▶ | if ( @_ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 9 | ▶ | my %args = ref $_[0] eq "HASH" ? %{ $_[0] } : @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 9 | ▶ | while ( my ( $method, $args ) = each %args ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 17 | ▶ | if ( $self->can( $method ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 17 | $self->$method( $args ); | |||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||
| 76 | 0 | croak "Can't handle opts arg: '$method'. Aborting"; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | return wantarray | ||||||||||||||||||||
| 82 | ? ( skip => $self->{skip}, | ||||||||||||||||||||
| 83 | validate => $self->{validate}, | ||||||||||||||||||||
| 84 | noecho => $self->{noecho}, | ||||||||||||||||||||
| 85 | ) | ||||||||||||||||||||
| 86 | : { skip => $self->{skip}, | ||||||||||||||||||||
| 87 | validate => $self->{validate}, | ||||||||||||||||||||
| 88 | noecho => $self->{noecho}, | ||||||||||||||||||||
| 89 | 9 | ▶ | }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 27 | ▶ | sub skip { | ||||||||||||||||||
|
Subroutine
skip: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 93 | 27 | my $self = shift; | |||||||||||||||||||
| 94 | 27 | ▶ | $self->{skip} = shift if @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 27 | return $self->{skip}; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 102 | ▶ | sub validate { | ||||||||||||||||||
|
Subroutine
validate: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 99 | 102 | my $self = shift; | |||||||||||||||||||
| 100 | 102 | ▶ | $self->{validate} = shift if @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 102 | return $self->{validate}; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 34 | ▶ | sub noecho { | ||||||||||||||||||
|
Subroutine
noecho: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 105 | 34 | my $self = shift; | |||||||||||||||||||
| 106 | 34 | ▶ | $self->{noecho} = shift if @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 34 | return $self->{noecho}; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | # Aliases | ||||||||||||||||||||
| 111 | 8 | *default = \&def; | |||||||||||||||||||
| 112 | 8 | *question = \&msg; | |||||||||||||||||||
| 113 | 8 | *options = \&opts; | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 8 | 1; | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =head1 NAME | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | Module::TestConfig::Question - question interface | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | =head1 SYNOPSIS | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | use Module::TestConfig::Question; | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | my $question = Module::TestConfig::Question->new( | ||||||||||||||||||||
| 126 | name => 'toes', | ||||||||||||||||||||
| 127 | msg => 'How many toes do you have?', | ||||||||||||||||||||
| 128 | def => 10, | ||||||||||||||||||||
| 129 | opts => { | ||||||||||||||||||||
| 130 | noecho => 0, | ||||||||||||||||||||
| 131 | validate => { ... }, | ||||||||||||||||||||
| 132 | skip => sub { ... }, | ||||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =head1 PUBLIC METHODS | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =over 2 | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =item new() | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | Args: See L<"SYNOPSIS"> | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | Returns: an object | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | =item msg() | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =item question() | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | Required. The question we ask of a user. A string. Tends | ||||||||||||||||||||
| 151 | to look best when there's a '?' or a ':' on the end. | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | Args: a question to ask the user | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | Returns: that question | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | =item name() | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | The name an answer is saved as. Basically a hash key. | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | Args: the question's name | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | Returns: that name | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =item def() | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =item default() | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | A question's default answer. | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | Args: a default | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | Returns: that default | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | =item opts() | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | =item options() | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | See L<"skip()">, L<"validate()"> and L<"noecho()">. | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | Args: A hash or hashref of options. | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | Returns: the hashref in scalar context, a hash in list context. | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | =item skip() | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | Criteria used to skip the current question. Either a scalar or | ||||||||||||||||||||
| 188 | a coderef. If either evalutes to true, the current question | ||||||||||||||||||||
| 189 | ought to be skipped. | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | Args: a scalar or coderef | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | Returns: the current scalar or coderef | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | =item validate() | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | Args to be passed directly to Params::Validate::validate() or another | ||||||||||||||||||||
| 198 | validation subroutine. | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | Args: a hashref by default | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | Returns: the current hashref | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | =item noecho() | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | Do we echo the user's typing? | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | Args: 1 or 0 | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | Returns: the current value | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | =back | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =head1 AUTHOR | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | Joshua Keroes E<lt>jkeroes@eli.netE<gt> | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | Copyright 2003 by Joshua Keroes E<lt>jkeroes@eli.netE<gt> | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 223 | it under the same terms as Perl itself. | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | =head1 SEE ALSO | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | L<Module::TestConfig> | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | =cut | ||||||||||||||||||||